说明:
本篇实现memcached的编译安装及将memcached添加为系统服务以实现自启动
实现:
一.编译安装memcached
# yum install libevent libevent-devel -y //memcached借助libevent与客户端通信是来实现的,所以需要先安装libevent,libevent-devel # cd /usr/local/src # wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz # tar -xvzf memcached-1.4.5.tar.gz # cd memcached-1.4.5 # ./configure --prefix=/usr/local/memcached # make && make install
至此,memcached就安装成功。可以以下方式开启memcached守护进程,使用默认端口11211
# /usr/local/memcached/bin/memcached -d -m 10 -u root -l 127.0.0.1 -p 11211 -c 64 -P /tmp/memcached.pid -d选项是启动一个守护进程, -m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB, -u是运行Memcache的用户,我这里是root, -l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200, -p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口, -c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定, -P是设置保存Memcache的pid文件,我这里是保存在/tmp/memcached.pid
二.添加memcached为系统服务,实现自启动
1.拷贝memcached源码包的memcached.sysv拷贝到/etc/init.d,做为memcached的启动脚本
# cp /usr/local/src/memcached-1.4.5/scripts/memcached.sysv /etc/init.d/memcached //将memcached源码包的memcached.sysv拷贝到/etc/init.d,做为memcached的启动脚本
2.修改/etc/init.d/memcached
# vim /etc/init.d/memcached //修改USER等,需要修改的都用//表示 #!/bin/sh # # chkconfig: - 55 45 # description: The memcached daemon is a network memory cache service. # processname: memcached # config: /etc/sysconfig/memcached # Source function library. . /etc/rc.d/init.d/functions PORT=11211 USER=root //更改用户为root MAXCONN=64 CACHESIZE=10 OPTIONS="" if [ -f /etc/sysconfig/memcached ];then . /etc/sysconfig/memcached fi # Check that networking is up. if [ "$NETWORKING" = "no" ] then exit 0 fi RETVAL=0 prog="memcached" start () { echo -n $"Starting $prog: " # insure that /var/run/memcached has proper permissions # chown $USER /usr/local/memcached //在此行前加#注释此行,感觉没用 daemon /usr/local/memcached/bin/memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /tmp/memcached.pid $OPTIONS //修改daemon memcached为daemon /usr/local/memcached/bin/memcached,不然/etc/init.d/memcached时会显示/bin/sh memcached command not found RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached } stop () { echo -n $"Stopping $prog: " killproc memcached RETVAL=$? echo if [ $RETVAL -eq 0 ] ; then rm -f /var/lock/subsys/memcached rm -f /var/run/memcached.pid fi } restart () { stop start } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status memcached ;; restart|reload) restart ;; condrestart) [ -f /var/lock/subsys/memcached ] && restart || : ;; *) echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}" exit 1 esac exit $?
3.添加系统服务
# chmod 755 /etc/init.d/memcached # chkconfig --add memcached //没看出这句作用,知道的请务必留言告知,谢谢! # chkconfig memcached on # service memcached start //开启memcached服务
附录:
附录1.在/etc/rc.d/rc.local中添加/usr/local/memcached/bin/memcached -d -m 10 -u root -l 127.0.0.1 -p 11211 -c 64 -P /tmp/memcached.pid也可以实现自启动