安装Memcached步骤:
1.编译安装Libevent
cd ~
wget https://github.com/downloads/libevent/libevent/libevent-2.0.19-stable.tar.gz
tar -zxvf libevent-2.0.19-stable.tar.gz
cd libevent-2.0.19-stable
./configure --prefix=/usr/local/libevent/
make
make install
ln -s /usr/local/libevent/lib/libevent-2.0.so.5 /lib64/libevent-2.0.so.5
2.安装Memcache
cd ~
wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
tar -zxvf memcached-1.4.13.tar.gz
cd memcached-1.4.13
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
make
make install
3.编写memcached服务启停脚本
# cd /etc/init.d
vi memcached
脚本内容如下:
#!/bin/sh
# Startup script for the server of memcached
#http://blog.rekfan.com/?p=172
# processname: memcached
# pidfile: /usr/local/memcached/memcached.pid
# logfile: /usr/local/memcached/memcached_log.txt
# memcached_home: /etc/memcached
# chkconfig: 35 21 79
# description: Start and stop memcached Service
# Source function library
. /etc/rc.d/init.d/functions
RETVAL=0
prog="memcached"
basedir=/usr/local/memcached
cmd=${basedir}/bin/memcached
pidfile="$basedir/${prog}.pid"
#logfile="$basedir/memcached_log.txt"
# 设置memcached启动参数
ipaddr="192.168.6.60" # 绑定侦听的IP地址
port="11211" # 服务端口
username="root" # 运行程序的用户身份
max_memory=128 # default: 64M | 最大使用内存
max_simul_conn=1024 # default: 1024 | 最大同时连接数
#maxcon=51200
#growth_factor=1.3 # default: 1.25 | 块大小增长因子
#thread_num=6 # default: 4
#verbose="-vv" # 查看详细启动信息
#bind_protocol=binary # ascii, binary, or auto (default)
start() {
echo -n $"Starting service: $prog"
$cmd -d -m $max_memory -u $username -l $ipaddr -p $port -c $max_simul_conn -P $pidfile
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}
stop() {
echo -n $"Stopping service: $prog "
run_user=`whoami`
pidlist=`ps -ef | grep $run_user | grep memcached | grep -v grep | awk '{print($2)}'`
for pid in $pidlist
do
# echo "pid=$pid"
kill -9 $pid
if [ $? -ne 0 ]; then
return 1
fi
done
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
#reload)
# reload
# ;;
restart)
stop
start
;;
#condrestart)
# if [ -f /var/lock/subsys/$prog ]; then
# stop
# start
# fi
# ;;
status)
status memcached
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit $RETVAL
4.设置脚本可被执行:
# chmod +x memcached
# chkconfig --add memcached
# chkconfig --level 35 memcached on
5.设置防火墙
vi /etc/sysconfig/iptables
将下面这行加入进去
-A RH-Firewall-l-INPUT -p tcp -m tcp --dport 11211 -j ACCEPT
注:将memcached加入到防火墙允许访问规则中
service iptables restart
注:防火墙重启
6.启动memcached
# service memcached start
//启动的时候实际上是调用了下面的这个命令,以守护进程的方式来启动memcached
/usr/local/memcached/bin/memcached -d -m 64 -u root -l 192.168.1.101
-p 11211 -c 1024 -P /usr/local/memcached/memcached.pid
7.查看memcached是否启动:
# ps -ef | grep memcached