检查服务器上是否已安装libevent的rpm包,具体操作如下:
# rpm –q
libevent
若没有,请在linux软件安装包中找到libevent-.rpm,上传至服务器,然后用命令rpm –u libevent-.rpm安装
使用root用户将memcached-
# cd
/usr/local
# tar zxvf memcached-
# cd
memcached-
# ./configure
# make
# make
install
查看memcached是否安装成功
# ls -al
/usr/local/bin/mem*
成功如下图
三:编写memcache系统服务
# cd
/etc/rc.d/init.d
# vi memcached
按i键,进入编辑模式,输入如下信息(不包含中文注释),其中MEMCACHED、MEMCACHED_PID、MEMCACHED_IP、MEMCACHED_USER、MEMCACHED_PORT、 MEMCACHED_MEMORY请按实际情况配置:
1.2.<span class="pln">#!/bin/sh </span>
3.<span class="pln"># </span>
4.<span class="pln"># memcached: MemCached Daemon </span>
5.<span class="pln"># </span>
6.<span class="pln"># chkconfig: - 90 25 </span>
7.<span class="pln"># description: MemCached Daemon </span>
8.<span class="pln"># </span>
9.<span class="pln"># Source function library.</span>
10.<span class="pln">. /etc/rc.d/init.d/functions</span>
11.<span class="pln">. /etc/sysconfig/network</span>
12.<span class="pln">RETVAL=0 </span>
13.<span class="pln">#memcached安装目录下的memcached脚本路径</span>
14.<span class="pln">#MEMCACHED=/root/memcached/bin/memcached</span>
15.<span class="pln">MEMCACHED=/usr/local/memcached-1.4.21 </span>
16.<span class="pln">#memcached_pid存放路径,可自行定义,但须确保存在该目录</span>
17.<span class="pln">MEMCACHED_PID=/root/logs/memcached.pid</span>
18.<span class="pln">#启动memcached的服务器IP</span>
19.<span class="pln">MEMCACHED_IP=10.1.195.88</span>
20.<span class="pln">#启动memcached的用户</span>
21.<span class="pln">MEMCACHED_USER=root</span>
22.<span class="pln">#启动memcached的端口</span>
23.<span class="pln">MEMCACHED_PORT=11211</span>
24.<span class="pln">#启动内存 单位(M)</span>
25.<span class="pln">MEMCACHED_MEMORY=8192m</span>
26.27.<span class="pln">start()</span>
28.<span class="pln">{</span>
29.<span class="pln"> if [ -e $MEMCACHED_PID];then</span>
30.<span class="pln"> echo "memcached already running...."</span>
31.<span class="pln"> exit 0</span>
32.<span class="pln"> fi</span>
33.<span class="pln"> echo -n $"Starting memcached: "</span>
34.<span class="pln"> daemon $MEMCACHED -u $MEMCACHED_USER -d -m $MEMCACHED_MEMORY -l $MEMCACHED_IP -p $MEMCACHED_PORT</span>
35.<span class="pln"> RETVAL=$?</span>
36.<span class="pln"> echo</span>
37.<span class="pln"> [ $RETVAL = 0 ] && touch $MEMCACHED_PID</span>
38.<span class="pln"> return $RETVAL</span>
39.<span class="pln">}</span>
40.<span class="pln">stop()</span>
41.<span class="pln">{</span>
42.<span class="pln"> echo -n $"Shutting down memcached: "</span>
43.<span class="pln"> killproc memcached</span>
44.<span class="pln"> RETVAL=$?</span>
45.<span class="pln"> echo</span>
46.<span class="pln"> [ $RETVAL = 0 ] && rm -f $MEMCACHED_PID</span>
47.<span class="pln">}</span>
48.<span class="pln">#[ -f $MEMCACHED] || exit 0</span>
49.<span class="pln"># See how we were called.</span>
50.<span class="pln">case "$1" in</span>
51.<span class="pln">start)</span>
52.<span class="pln"> start</span>
53.<span class="pln"> ;;</span>
54.<span class="pln">stop)</span>
55.<span class="pln"> stop</span>
56.<span class="pln"> ;;</span>
57.<span class="pln">restart)</span>
58.<span class="pln"> stop</span>
59.<span class="pln"> sleep 3</span>
60.<span class="pln"> start</span>
61.<span class="pln"> ;;</span>
62.<span class="pln">*)</span>
63.<span class="pln"> echo $"Usage: $0 {start|stop|restart}"</span>
64.<span class="pln"> exit 1</span>
65.<span class="pln">esac</span>
66.<span class="pln">exit $RETVAL</span>
按ESC键,退出编辑模式,输入命令:wq保存退出。
四:启动memcache服务
使用命令service memcached start启动memcached服务,出现如下信息即启动成功:
Service memcached: [OK]
在本地机器telnet memcached服务器的11211端口,能够连接上即表示服务正常启动。
五:常见问题记录
1. libevent没有安装
下载libevent安装包,编译安装
./configure –prefix=/usr
make
make install