Linux下Memcache服务器的安装
环境需求:
安装Memcached需要libevent库的支持,所以请在安装Memcached之前检查有没有安装libevent。测试环境还需要PHP的支持,本文假设PHP已经安装到/usr/local/php目录下。
服务器端主要是安装memcache服务器端,Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent。
memcached下载地址:http://www.danga.com/memcached/
libevent下载地址: http://monkey.org/~provos/libevent/
安装配置:
下载libevent:
mkdir /home/oldboy/tools -p
cd /home/oldboy/tools/
wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz
安装libevent:
tar zxf libevent-1.4.13-stable.tar.gz
cd libevent-1.4.13-stable
./configure –-prefix=/usr ;echo $?
make && make install ;echo $?
cd ..
检查libevent是否安装成功:
[root@jason tools]# ls -al /usr/lib |grep libevent
lrwxrwxrwx 1 root root 21 Feb 20 10:00libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rwxr-xr-x 1 root root 373222 Feb 20 10:00 libevent-1.4.so.2.1.3
-rw-r--r-- 1 root root 604082 Feb 20 10:00 libevent.a
-rwxr-xr-x 1 root root 831 Feb 20 10:00libevent.la
lrwxrwxrwx 1 root root 21 Feb 20 10:00libevent.so -> libevent-1.4.so.2.1.3
lrwxrwxrwx 1 root root 26 Feb 20 10:00libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rwxr-xr-x 1 root root 137345 Feb 20 10:00 libevent_core-1.4.so.2.1.3
-rw-r--r-- 1 root root 229088 Feb 20 10:00 libevent_core.a
-rwxr-xr-x 1 root root 866 Feb 20 10:00libevent_core.la
lrwxrwxrwx 1 root root 26 Feb 20 10:00libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx 1 root root 27 Feb 20 10:00libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 295876 Feb 20 10:00 libevent_extra-1.4.so.2.1.3
-rw-r--r-- 1 root root 469194 Feb 20 10:00 libevent_extra.a
-rwxr-xr-x 1 root root 873 Feb 20 10:00libevent_extra.la
lrwxrwxrwx 1 root root 27 Feb 20 10:00libevent_extra.so -> libevent_extra-1.4.so.2.1.3
下载安装memcached,同时指定libevent的位置:
cd /home/oldboy/tools
wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
tar zxf memcached-1.4.13.tar.gz
cd memcached-1.4.13
./configure --with-libevent=/usr ;echo $?
make && make install ;echo $?
cd ..
安装完成后会在/usr/local/bin/目录下发现mencached
[root@jason tools]# ls -al /usr/local/bin/
total 33548
drwxr-xr-x. 2 root root 4096 Feb 20 10:02 .
drwxr-xr-x. 16 root root 4096 Feb 19 13:31 ..
-rwxr-xr-x. 1 root root 7975624 Feb 18 11:00 ccmake
-rwxr-xr-x. 1 root root 7860667 Feb 18 11:00 cmake
-rwxr-xr-x. 1 root root 8421359 Feb 18 11:00 cpack
-rwxr-xr-x. 1 root root 9779615 Feb 18 11:01 ctest
-rwxr-xr-x 1 root root 297114 Feb 20 10:02 memcached
启动:
[root@jason tools]# memcached -p 11211 -u root -m 16m -c 10240 -P/var/run/memcached_11211.pid -d
[root@jason tools]# memcached -p 11212 -u root -m 16m -c 10240 -P /var/run/memcached_11212.pid-d
[root@jason tools]# lsof -i:11211
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
memcached 14960 root 26u IPv4 19716 0t0 TCP *:memcache (LISTEN)
memcached 14960 root 27u IPv6 19717 0t0 TCP *:memcache (LISTEN)
memcached 14960 root 28u IPv4 19720 0t0 UDP *:memcache
memcached 14960 root 29u IPv6 19721 0t0 UDP *:memcache
[root@jason tools]# lsof -i:11212
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
memcached 14967 root 26u IPv4 19743 0t0 TCP *:11212 (LISTEN)
memcached 14967 root 27u IPv6 19744 0t0 TCP *:11212 (LISTEN)
memcached 14967 root 28u IPv4 19747 0t0 UDP *:11212
memcached 14967 root 29u IPv6 19748 0t0 UDP *:11212
如:/usr/local/bin/memcached -d -m 200 -u root -l 192.168.1.91 -p 12000 -c1000 -P /tmp/memcached.pid
相关解释如下:
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,这里是200MB
-u是运行Memcache的用户,我这里是root
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.1.91
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口
-c选项是最大运行的并发连接数,默认是1024,我这里设置了1000
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid
停止Memcache进程:
kill `cat /tmp/memcached.pid`
加入开机自启动:
cat >>/etc/rc.local<<EOF
#memcached start along with sys by lisp
memcached -p 11211 -u root -m 16m -c 10240 -P/var/run/memcached_11211.pid -d
memcached -p 11212 -u root -m 16m -c 10240 -P/var/run/memcached_11212.pid -d
EOF