系统:CentOS6.x_x64,memcached-1.5.3。memcached官方下载地址 http://www.memcached.org/files/memcached-1.5.3.tar.gz。libevent 库:https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
1>首先yum安装gcc
yum install -y wget gcc
2>安装源码libevent库
#wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
#tar zxvf libevent-2.0.21-stable.tar.gz
#./configure --prefix=/usr/local/libevent
#make && make install
检查是否安装成功
#输入: ls -al /usr/local/libevent/lib | grep libevent
#如果输出为下所示则表示安装成功
lrwxrwxrwx 1 root root 21 Dec 4 18:08 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9
-rwxr-xr-x 1 root root 968442 Dec 4 18:08 libevent-2.0.so.5.1.9
-rw-r--r-- 1 root root 1571130 Dec 4 18:08 libevent.a
lrwxrwxrwx 1 root root 26 Dec 4 18:08 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9
-rwxr-xr-x 1 root root 585057 Dec 4 18:08 libevent_core-2.0.so.5.1.9
-rw-r--r-- 1 root root 977914 Dec 4 18:08 libevent_core.a
-rwxr-xr-x 1 root root 985 Dec 4 18:08 libevent_core.la
lrwxrwxrwx 1 root root 26 Dec 4 18:08 libevent_core.so -> libevent_core-2.0.so.5.1.9
lrwxrwxrwx 1 root root 27 Dec 4 18:08 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x 1 root root 404772 Dec 4 18:08 libevent_extra-2.0.so.5.1.9
-rw-r--r-- 1 root root 593288 Dec 4 18:08 libevent_extra.a
-rwxr-xr-x 1 root root 992 Dec 4 18:08 libevent_extra.la
lrwxrwxrwx 1 root root 27 Dec 4 18:08 libevent_extra.so -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x 1 root root 950 Dec 4 18:08 libevent.la
lrwxrwxrwx 1 root root 29 Dec 4 18:08 libevent_openssl-2.0.so.5 -> libevent_openssl-2.0.so.5.1.9
-rwxr-xr-x 1 root root 94209 Dec 4 18:08 libevent_openssl-2.0.so.5.1.9
-rw-r--r-- 1 root root 131836 Dec 4 18:08 libevent_openssl.a
-rwxr-xr-x 1 root root 1021 Dec 4 18:08 libevent_openssl.la
lrwxrwxrwx 1 root root 29 Dec 4 18:08 libevent_openssl.so -> libevent_openssl-2.0.so.5.1.9
lrwxrwxrwx 1 root root 30 Dec 4 18:08 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.9
-rwxr-xr-x 1 root root 18462 Dec 4 18:08 libevent_pthreads-2.0.so.5.1.9
-rw-r--r-- 1 root root 18702 Dec 4 18:08 libevent_pthreads.a
-rwxr-xr-x 1 root root 1013 Dec 4 18:08 libevent_pthreads.la
lrwxrwxrwx 1 root root 30 Dec 4 18:08 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.9
lrwxrwxrwx 1 root root 21 Dec 4 18:08 libevent.so -> libevent-2.0.so.5.1.9
3>下载安装memcached
#wget
#tar -zxvf memcached-1.5.3.tar.gz
#./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
#make && make install
#ln -s /usr/local/memcached/bin/memcached /usr/local/bin #建立软连接
1>脚本
vim cat /etc/init.d/memcached
#!/bin/sh
#
# memcached: MemCached Daemon
#
# chkconfig: - 90 25
# description: MemCached Daemon
# Source function library.
# Author: pkey san 2015/12/07
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
prog=memcached
pidfile=${PIDFILE-/var/run/memcached.pid}
start()
{
echo -n $"Starting memcached: "
daemon --pidfile=${pidfile} /usr/local/bin/memcached -u daemon -d -m 512 -l 127.0.0.1 -c 4096 -p 11211
echo `ps aux |grep memcached |grep 11211 |grep -v grep |awk '{print $2}'` >$pidfile
echo
}
stop()
{
echo -n $"Shutting down memcached: "
killproc $prog
echo
}
rh_status(){
status -p ${pidfile} $prog
}
[ -f /usr/local/bin/memcached ] || exit 0
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
rh_status
;;
restart|reload)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|}"
exit 1
esac
exit 0
2>解释说明
daemon --pidfile=${pidfile} /usr/local/bin/memcached -u daemon -d -m 512 -l 127.0.0.1 -c 4096 -p 11211
-p memcached监听的TCP端口
-l 监听的ip地址,127.0.0.1是本机,当然也可以写上你的服务器IP,如:10.0.0.10,这是我服务器的IP地址,如果你需要多个服务器都能够读取这台memcached的缓存数据,那么就必须设定这个ip
-d 以daemon方式运行,将程序放入后台
-u memcached的运行用户
-P memcached的pid文件路径
-m memcached可以使用的最大内存数量
-c memcached同时可以接受的最大的连接数
如果你希望以socket方式来访问memcached,那么在启动的时候就必须去掉 -l和-p参数,并加上-s参数:
-s memcached的socket文件路径
3>添加开机启动
#chmod +x /etc/init.d/memcached
#chkconfig --add memcached
#chkconfig memcached on
#service memcached start|stop|restart|reload|status
#安装好之后
ln -s /usr/local/memcached/bin/memcached /usr/bin/memcached
/bin/cp scripts/memcached.sysv /etc/init.d/memcached
sed -i 's@^USER=.*@USER=root@' /etc/init.d/memcached
sed -i 's@chown@#chown@' /etc/init.d/memcached
sed -i 's@/var/run/memcached/memcached.pid@/var/run/memcached.pid@' /etc/init.d/memcached
sed -i 's@^prog=.*@prog="/usr/local/memcached/bin/memcached"@' /etc/init.d/memcached #前面有软链接,这里可以省略
chmod +x /etc/init.d/memcached
chkconfig --add memcached
chkconfig memcached on
此种方式也可以加到启动项里。