CentOS-6.4安装Memcached

由于项目上线后需要考虑分布式,SESSION准备放在Memcache来同步,下面是一些配置及随手的记录。

1、安装libevent

1
2
3
4
5
6
[root@PHPHa ~]# wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
[root@PHPHa ~]# tar zxvf libevent-1.4.14b-stable.tar.gz
[root@PHPHa ~]# cd libevent-1.4.14b-stable
[root@PHPHa ~]# ./configure --prefix=/usr/local/libevent/
[root@PHPHa ~]# make && make install
[root@PHPHa ~]# ln -s /usr/local/libevent/lib/libevent-1.4.so.2 /lib/libevent-1.4.so.2

2、安装Memcached

1
2
3
4
5
[root@PHPHa ~]# wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
[root@PHPHa ~]# tar zxvf memcached-1.4.15.tar.gz
[root@PHPHa ~]# cd memcached-1.4.15
[root@PHPHa ~]# ./configure --prefix=/usr/local/memcached/ --with-libevent=/usr/local/libevent/
[root@PHPHa ~]# make && make install

3、启动Memcached

1
[root@PHPHa ~]# /usr/local/memcached/bin/memcached -d -m 64 -u root -l 127.0.0.100 -p 11211 -c 128 -P /tmp/memcached.pid

4、为了方便管理,写个SHELL脚本吧。

1
2
3
# http://blog.phpha.com
# 以下内容摘自互联网
[root@PHPHa ~]# vi /etc/rc.d/init.d/memcached
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/sh
#
# memcached:    MemCached Daemon
#
# chkconfig:    - 90 25
# description:  MemCached Daemon
#
# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
#[ ${NETWORKING} = "no" ] && exit 0
#[ -r /etc/sysconfig/dund ] || exit 0
#. /etc/sysconfig/dund
#[ -z "$DUNDARGS" ] && exit 0
start()
{
        echo -n $"Starting memcached: "
        daemon $MEMCACHED -u daemon -d -m 64 -l 127.0.0.100 -p 11211 -c 128 -P /tmp/memcached.pid
        echo
}
stop()
{
        echo -n $"Shutting down memcached: "
        killproc memcached
        echo
}
MEMCACHED="/usr/local/memcached/bin/memcached"
[ -f $MEMCACHED ] || exit 1
# See how we were called.
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                stop
                sleep 3
                start
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart}"
                exit 1
esac
exit 0

5、添加Memcached开机启动

1
2
3
4
5
[root@PHPHa ~]# cd /etc/rc.d/init.d/
[root@PHPHa ~]# chmod 777 memcached
[root@PHPHa ~]# chkconfig --add memcached
[root@PHPHa ~]# chkconfig --level 235 memcached on
[root@PHPHa ~]# chkconfig --list | grep memcached

6、Memcached使用 经过上面的步骤,可以如下进行开启、停止、重启等操作。

1
2
3
[root@PHPHa ~]# service memcached start
[root@PHPHa ~]# service memcached stop
[root@PHPHa ~]# service memcached restart