wget http://memcached.org/latest -O memcached.tar.gz tar -zxvf memcached.tar.gz cd memcached ./configure --prefix=/usr/local/memcached make && sudo make install ln -sf /usr/local/memcached/bin/memcached /usr/bin/memcached如果不存在`/var/lock/subsys`,則創建`mkdir -p /var/lock/subsys` 配置系統服務 添加文件:vim /etc/init.d/memcached
#! /bin/bash # # memcached: MemCached Daemon # # chkconfig: - 90 25 # description: MemCached Daemon # ### BEGIN INIT INFO # Provides: memcached # Required-Start: $syslog # Required-Stop: $syslog # Should-Start: $local_fs # Should-Stop: $local_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: memcached - Memory caching daemon # Description: memcached - Memory caching daemon ### END INIT INFO #如果IP為127.0.0.1則限制僅本機可訪問 #如果IP為本機的內網IP,則只能內網能訪問 #IP為應用程序訪問Memcached使用的IP,一般為127.0.0.1或本機內網IP或本機外網IP IP=127.0.0.1 PORT=11211 USER=root MAXCONN=1024 CACHESIZE=64 OPTIONS="" RETVAL=0 prog="memcached" start () { echo -n $"Starting $prog: " /usr/local/memcached/bin/memcached -d -l $IP -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached.pid $OPTIONS if [ "$?" != 0 ] ; then echo " failed" exit 1 else touch /var/lock/subsys/memcached echo " done" fi } stop () { echo -n $"Stopping $prog: " if [ ! -e /var/run/$prog.pid ]; then echo -n $"$prog is not running." exit 1 fi kill `cat /var/run/memcached.pid` if [ "$?" != 0 ] ; then echo " failed" exit 1 else rm -f /var/lock/subsys/memcached rm -f /var/run/memcached.pid echo " done" fi } restart () { $0 stop sleep 2 $0 start } # See how we were called. case "$1" in start) start ;; stop) stop ;; restart|reload) restart ;; *) echo $"Usage: $0 {start|stop|status|restart|reload}" exit 1 ;; esac exit $?
賦予可執行權限:chmod +x /etc/init.d/memcached 防火牆設置
#設置規則 vim /etc/sysconfig/iptables #如果限定某些機器能訪問可如下設置: -A INPUT -s 127.0.0.1,10.170.238.137,123.7.213.222/32 -p udp -m udp --dport 11211 -j ACCEPT -A INPUT -s 127.0.0.1,10.170.238.137,123.7.213.222/32 -p tcp -m tcp --dport 11211 -j ACCEPT -A INPUT -p tcp -m tcp --dport 11211 -j DROP -A INPUT -p udp -m udp --dport 11211 -j DROP #重啟iptables使得規則生效 service iptables restart
啟動 /etc/init.d/memcached start