解析:Memcached是什么? 
Memcached是由Danga Interactive开发的,高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。

一、软件版本

    libevent 稳定版

1
wget http: //monkey .org/~provos /libevent-1 .4.14b-stable. tar .gz

    memcached 稳定版

1
wget  http: //memcached .googlecode.com /files/memcached-1 .4.5. tar .gz

二、软件安装

    Libevent安装

1
2
3
4
5
[root@jw-test01 software] # tar zxvf libevent-1.4.14b-stable.tar.gz
[root@jw-test01 software] # cd libevent-1.4.14b-stable
[root@jw-test01 libevent] # ./configure --prefix=/usr/local/libevent/
[root@jw-test01 libevent] # make
[root@jw-test01 libevent] # make install

   Memcached安装 

1
2
3
4
5
[root@jw-test01 software] # tar -zxvf memcached-1.4.5.tar.gz
[root@jw-test01 software] # cd memcached-1.4.5
[root@jw-test01 memcached] # ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[root@jw-test01 memcached] # make
[root@jw-test01 memcached] # make install

三、编写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
#!/bin/bash
# author:kuangl
# date:2013-05-30
# description: Starts and stops the Memcached services.
# pidfile: /tmp/memcached1.pid
# config:  /usr/local/memcached
# chkconfig: - 55 45
# source function library
/etc/rc .d /init .d /functions
memcached= "/usr/local/memcached/bin/memcached"
[ -e $memcached ] ||  exit  1
start()
{
echo  "Starting memcached:"
daemon $memcached -d -m 1000 -u root -l 127.0.0.1 -p 11211 -c 1500 -P  /tmp/memcached1 .pid
}
stop()
{
echo  "Shutting down memcached"
killproc memcached
}
case  "$1"  in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep  3
start
;;
*)
echo  $ "Usage: $0 {start|stop|restart}"
exit  1
esac
exit  $?

四、将脚本复制到init.d目录下

1
[root@jw-test01 scripts] # cp memcached.sh /etc/init.d/memcached

五、将memcached加入系统启项

1
2
[root@jw-test01 scripts] # chkconfig  --add memcached
[root@jw-test01 scripts] # chkconfig --level 35 memcached on

六、启动memcached 

1
2
3
4
5
[root@jw-test01 scripts] # service memcached restart
Shutting down memcached       [确定]
Starting memcached:           [确定]
[root@jw-test01 scripts] # ps -ef |grep memcached
root     27616     1  0 22:18 ?        00:00:00  /usr/local/memcached/bin/memcached  -d -m 1000 -u root -l 127.0.0.1 -p 11211 -c 1500 -P  /tmp/memcached1 .pid

七、Memcached常用参数

参数说明
-p <num> 设置端口号(默认不设置为: 11211)
-U <num> UDP监听端口(默认: 11211, 0 时关闭)
-l <ip_addr> 绑定地址(默认:所有都允许,无论内外网或者本机更换IP,有安全隐患,若设置为127.0.0.1就只能本机访问)
-d独立进程运行
-u <username> 绑定使用指定用于运行进程<username>
-m <num> 允许最大内存用量,单位M (默认: 64 MB)
-P <file> PID写入文件<file>,这样可以使得后边进行快速进程终止需要与-d 一起使用