首先需要安装libevent:
1、下载
wget -O libevent-2.0.21-stable.tar.gz https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
2、解压
tar zxvf libevent-2.0.21-stable.tar.gz
3、配置安装路径
cd libevent-2.0.21-stable
./configure -prefix=/usr
4、编译并安装
make
make install
5、测试libevent是否安装成功:
# ls -al /usr/lib | grep libevent
一、检查libevent
首先检查系统中是否安装了libevent
Shell代码
- rpm -qa|grep libevent
如果安装了则查看libevent的安装路径,后续安装时需要用到
安装完libevent后正式开始安装memcached
1.首先是下载memcached的安装包
Shell代码
- wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
2.修改安装包的执行操作权限
Shell代码
- chmod 777 memcached-1.4.15.tar.gz
3.解压安装包到指定目录
Shell代码
- tar zxvf memcached-1.4.15.tar.gz -C /usr/local
4.进入解压后的目录
Shell代码
- cd /usr/local/memcached-1.4.15/
5.配置、编译、安装
注:configure时需要指定libevent的安装路径
Shell代码
- ./configure -with-libevent=/usr/libevent/ -prefix=/usr/local/memcached
- make
- make install
6.安装成功后启动一下看安装是否成功
Shell代码
- /usr/local/memcached/bin/memcached -d -m 2048 -p 11211 -u root
启动参数介绍如下:
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,这里是10MB,
-u是运行Memcache的用户,这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口,这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,这里设置了256,按照服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,也可以启动多个守护进程,不过端口不能重复。
如果要给memcached上配置日志,参见:http://chenzhou123520.iteye.com/blog/1925209
PS:附上一个memcached启动的shell脚本
Shell代码
- #!/bin/sh
- echo "Start to start memcached server $(date)..."
- MEMCACHED=/usr/local/memcached/bin/memcached
- usage()
- {
- echo "usage: `basename $0` port"
- }
- if [ -n "$1" ]
- then
- {
- pid=`ps aux|grep memcached|grep "$1" |grep -v grep|awk '{print $2}'`
- if [ -n "$pid" ]
- then
- {
- sleep 2
- echo "kill memcached which port is $1 begin"
- echo "pid:$pid"
- kill -9 $pid
- echo "kill memcached which port is $1 end"
- sleep 2
- }
- fi
- echo "begin to start memcached in port $1"
- LOG_FILE=/var/log/memcached/memcached_$1.log
- rm -f $LOG_FILE
- $MEMCACHED -d -m 2048 -p $1 -u root -vv >> $LOG_FILE 2>&1
- echo "start memcached end"
- tail -f $LOG_FILE
- }
- else
- {
- usage
- exit 1
- }
- fi
把该脚本命名为startup.sh
执行:./startup.sh 11211
其中11211为memcached的启动端口,也可以通过自定义的端口来启动
启动/结束
memcached -d -m 10 -u root -l 192.168.0.122 -p 11200 -c 256 -P /tmp/memcached.pid
-d 选项是启动一个守护进程,
-m 是分配给Memcache使用的内存数量,单位是MB,这里是10MB
-u 是运行Memcache的用户,这里是root
-l 是监听的服务器IP地址,如果有多个地址的话,这里指定了服务器的IP地址192.168.0.122
-p 是设置Memcache监听的端口,这里设置了12000,最好是1024以上的端口
-c 选项是最大运行的并发连接数,默认是1024,这里设置了256,按照你服务器的负载量来设定
-P 是设置保存Memcache的pid文件
kill `cat /tmp/memcached.pid`
获取运行状态
echo stats | nc 192.168.1.123 11200
watch "echo stats | nc 192.168.1.123 11200" (实时状态)
注意:如果在安装中出现
安装memcache 时提示error while loading shared libraries: libevent-2.0解决办法安装memcache
启动服务时出现
error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
解决办法为:
>whereis libevent-2.0.so.5
libevent-2.0.so.5: /usr/local/lib/libevent-2.0.so.5
> ldd /usr/local/bin/memcached
libevent-2.0.so.5 => not found
libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b83fce0e000)
libc.so.6 => /lib64/libc.so.6 (0x00002b83fd029000)
librt.so.1 => /lib64/librt.so.1 (0x00002b83fd381000)
/lib64/ld-linux-x86-64.so.2 (0x00002b83fc9b0000)
> LD_DEBUG=libs ./memcached -v
找到默认路径 /usr/lib/
>sudo ln -s /usr/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
>sudo ldd /usr/local/bin/memcached
libevent-2.0.so.5 => /usr/lib64/libevent-2.0.so.5 (0x00002b83fcbcd000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b83fce0e000)
libc.so.6 => /lib64/libc.so.6 (0x00002b83fd029000)
librt.so.1 => /lib64/librt.so.1 (0x00002b83fd381000)
/lib64/ld-linux-x86-64.so.2 (0x00002b83fc9b0000)