安装memcached之前需要安装libevent
libevent从http://jaist.dl.sourceforge.net/project/levent/libevent/libevent-2.0/libevent-2.0.22-stable.tar.gz下载
安装libevent
(1)解压libevent-2.0.22-stable.tar.gz
tar -xvf libevent-2.0.22-stable.tar.gz
(2)编译libevent
cd libevent-2.0.22-stable
sudo ./configure -prefix=/usr/local/libevent
(3)安装libevent
sudo make
sudo make install
安装完libevent
接下来安装memcached
下载memcached
下载地址:http://www.danga.com/memcached/dist/memcached-1.2.7.tar.gz
(1)解压memcached-1.2.7.tar.gz
tar -xvf memcached-1.2.7.tar.gz
(2)编译memcached-1.2.7
sudo ./configure -with-libevent=/usr/local/libevent/
(3)安装memcached
sudo make
sudo make install
(4)启动memcached服务
/usr/local/bin/memcached -d -m 10 -u root -l 192.168.141.64 -p 12000 -c 256 -P /tmp/memcached.pid
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,默认64M
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,默认是本机
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,默认是11211
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,
-h 打印帮助信息
-v 输出警告和错误信息
-vv 打印客户端的请求和返回信息
如果要结束Memcache进程,执行:
kill 'cat /tmp/memcached.pid'
也可以启动多个守护进程,不过端口不能重复。
shell脚本检测memcached进程是否启动
#!/bin/sh
#check memcached process and restart if down
PATH=$PATH:/opt/env/memcache/bin/memcached
DATE=`date -d "today" +"%Y-%m-%d-%H:%M"`
#用ps命令查看memcached进程
MM=`ps -aux |grep "memcached" |grep "11211" |grep -v "grep" |wc -l`
#if语句判断进程是否存在,如果不存在,输出日志记录并重启memcached服务
if [ "$MM" == "0" ]; then
echo "$DATE The memcached is problem and restart" >> /root/sh/memcached_check.logs
/opt/env/memcache/bin/memcached -t 8 -d -m 2048 -p 11211 -u nobody
else
#echo "$DATE The memcached is ok" >>/root/sh/memcached_check.logs
fi