您的位置:首页 > 数据库 > Memcache

缓存服务器memcached的安装配置

2011-08-02 18:56 429 查看
一 软件下载
http://monkey.org/~provos/libevent-2.0.12-stable.tar.gz http://danga.com/memcached/dist/memcached-1.4.0.tar.gz
二 编译安装

2.1 安装gcc

[root@server002 ~]# yum install gcc

2.2 安装

解压到相应的目录,然后执行一下脚本,完成安装

[root@server002 ~]# ./configure --prefix=/usr/ && make && make install

三 配置

3.1 防火墙配置

要实现永久有效,所以要修改配置文件

vim /etc/sysconfig/iptables

在最后一行添加以下内容

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT

四 启动memcached服务器

/usr/bin/memcached -m 512 -u root -l 192.168.25.252 -p 11211 -P /tmp/memcached.pid -d

参数说明:

[root@server002 ~]# memcached -help

memcached 1.4.0

-p <num> TCP port number to listen on (default: 11211)

-U <num> UDP port number to listen on (default: 11211, 0 is off)

-s <file> UNIX socket path to listen on (disables network support)

-a <mask> access mask for UNIX socket, in octal (default: 0700)

-l <ip_addr> interface to listen on (default: INADDR_ANY, all addresses)

-d run as a daemon

-r maximize core file limit

-u <username> assume identity of <username> (only when run as root)

-m <num> max memory to use for items in megabytes (default: 64 MB)

-M return error on memory exhausted (rather than removing items)

-c <num> max simultaneous connections (default: 1024)

-k lock down all paged memory. Note that there is a

limit on how much memory you may lock. Trying to

allocate more than that would fail, so be sure you

set the limit correctly for the user you started

the daemon with (not for -u <username> user;

under sh this is done with 'ulimit -S -l NUM_KB').

-v verbose (print errors/warnings while in event loop)

-vv very verbose (also print client commands/reponses)

-vvv extremely verbose (also print internal state transitions)

-h print this help and exit

-i print memcached and libevent license

-P <file> save PID in <file>, only used with -d option

-f <factor> chunk size growth factor (default: 1.25)

-n <bytes> minimum space allocated for key+value+flags (default: 48)

-D <char> Use <char> as the delimiter between key prefixes and IDs.

This is used for per-prefix stats reporting. The default is

":" (colon). If this option is specified, stats collection

is turned on automatically; if not, then it may be turned on

by sending the "stats detail on" command to the server.

-t <num> number of threads to use (default: 4)

-R Maximum number of requests per event, limits the number of

requests process for a given connection to prevent

starvation (default: 20)

-C Disable use of CAS

-b Set the backlog queue limit (default: 1024)

-B Binding protocol - one of ascii, binary, or auto (default)

停掉服务

>pkill memcached.

五 memcached的安全设置

我们上面的Memcache服务器端都是直接通过客户端连接后直接操作,没有任何的验证过程,这样如果服务器是直接暴露在互联网上的话是比较危险,轻则数 据泄露被其他无关人员查看,重则服务器被入侵,因为Mecache是以root权限运行的,况且里面可能存在一些我们未知的bug或者是缓冲区溢出的情 况,这些都是我们未知的,所以危险性是可以预见的。为了安全起见,我做两点建议,能够稍微的防止黑客的入侵或者数据的泄露。

内网访问

最好把两台服务器之间的访问是内网形态的,一般是Web服务器跟Memcache服务器之间。普遍的服务器都是有两块网卡,一块指向互联网,一块指向内 网,那么就让Web服务器通过内网的网卡来访问Memcache服务器,我们Memcache的服务器上启动的时候就监听内网的IP地址和端口,内网间的 访问能够有效阻止其他非法的访问。

# memcached -d -m 1024 -u root -l 192.168.10.150 -p 11211 -c 1024 -P /tmp/memcached.pid

Memcache服务器端设置监听通过内网的192.168.10.150的ip的11211端口,占用1024MB内存,并且允许最大1024个并发连接

设置防火墙

防火墙是简单有效的方式,如果却是两台服务器都是挂在网的,并且需要通过外网IP来访问Memcache的话,那么可以考虑使用防火墙或者代理程序来过滤非法访问。

一般我们在Linux下可以使用iptables或者FreeBSD下的ipfw来指定一些规则防止一些非法的访问,比如我们可以设置只允许我们的Web服务器来访问我们Memcache服务器,同时阻止其他的访问。

# iptables -F

# iptables -P INPUT DROP

# iptables -A INPUT -p tcp -s 192.168.25.11 –dport 11211 -j ACCEPT

# iptables -A INPUT -p udp -s 192.168.25.11 –dport 11211 -j ACCEPT

上面的iptables规则就是只允许192.168.25.11这台Web服务器对Memcache服务器的访问,能够有效的阻止一些非法访问,相应的也可以增加一些其他的规则来加强安全性,这个可以根据自己的需要来完成配置

java对memcachedserver的应用,将在以后的文章中完成,敬请关注。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: