Memcached安装部署

Memcached的安装过程和Redis安装差不多,是很十分简单。Memcached依赖libevent,安装Memcached需要先安装libevent。

安装libevent

libvent官网:http://libevent.org/

1
2
3
4
5
6
7
8
9
10
11
# 下载安装包
wget https://github.com/libevent/libevent/releases/download/release-2.1.10-stable/libevent-2.1.10-stable.tar.gz
# 解压
tar zxvf libevent-2.1.10-stable.tar.gz
cd libevent-2.1.10-stable
# 配置编译参数
./configure --prefix=/usr/local/libevent/
# 编译
make
# 安装
sudo make install

安装Memcached

memcached官网:https://memcached.org/

1
2
3
4
5
6
7
8
9
10
11
# 下载安装包
wget https://memcached.org/files/memcached-1.5.16.tar.gz(依赖libevent)
# 解压
tar zxvf memcached-1.5.16.tar.gz
cd memcached-1.5.16
# 配置编译参数
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
# 编译
make
# 安装
sudo make install

Memcached管理脚本

一般启动memcached命令如下(示例):

1
/usr/local/memcached/bin/memcached -d -m 25000 -u memcached -l 127.0.0.1 -p 12121 -u www-data -P /tmp/memcached.pid

如果每次都这样启动就比较麻烦,所以按照习惯还是写了个快速启动脚本,实现service start/stop这样的管理模式。

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
#!/bin/bash 
# 快速启动memcached脚本
#
EXEC=/usr/local/memcached/bin/memcached
PORT=11211
MEM=1024
IP=127.0.0.1
USER=www-data
PIDFILE=/usr/local/memcached/tmp/memcached.pid

case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting memcached ..."
$EXEC -d -m $MEM -u $USER -l $IP -p $PORT -P $PIDFILE
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
kill $PID
rm -f $PIDFILE
while [ -x /proc/${PID} ]
do
echo "Waiting for memcached to shutdown ..."
sleep 1
done
echo "memcached stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac

将脚本命名为memcached,保存到/usr/sbin目录下,后面我们就可以通过如下命令管理memcached了:

  • 启动: sudo memcached start
  • 关闭: sudo memcached stop
有用就打赏一下作者吧!