Memcached高速缓存服务器部署及应用(二、Memcached在Linux平台下安装与配置)

时间:2022-02-14 07:59:29

一、下载并安装Memcache服务器端


服务器端主要是安装memcache服务器端.
下载:http://code.google.com/p/memcached/downloads/list

另外,Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent,libevent的最新版本是libevent-2.0.21。(如果你的系统已经安装了libevent,可以不用安装,)
官网:http://www.libevent.org

下载:https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz

用wget指令直接下载这两个东西.下载回源文件后。
先安装libevent。这个东西在配置时需要指定一个安装路径,即./configure –prefix=/usr;然后make;然后make install;

再安装memcached,只是需要在配置时需要指定libevent的安装路径即./configure –with-libevent=/usr;然后make;然后make install;
这样就完成了Linux下Memcache服务器端的安装。详细的方法如下:

1.分别把memcached和libevent下载回来,放到 /web/software 目录下(个人习惯)

# cd /web/software
# wget http://memcached.org/latest
# wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz

 

 

2.先安装libevent

# tar zxvf libevent-2.0.21-stable.tar.gz
# cd libevent-2.0.21-stable/
# ./configure –prefix=/usr
# make
# make install

 

3.测试libevent是否安装成功

# ls -al /usr/lib | grep libevent

 

Memcached高速缓存服务器部署及应用(二、Memcached在Linux平台下安装与配置)

 

OK!安装成功

 

4.安装memcached,同时需要安装中指定libevent的安装位置

# cd ../
# tar zxvf memcached-1.4.15.tar.gz
# cd memcached-1.4.15
# ./configure –-with-libevent=/usr
# make
# make install

如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。
安装完成后会把memcached放到 /usr/local/bin/memcached ,

5.测试是否成功安装memcached

# ls -al /usr/local/bin/mem*

 

Memcached高速缓存服务器部署及应用(二、Memcached在Linux平台下安装与配置)

至此, 在linux环境下memcache服务端就已安装OK了

 

二、配置memcached自启动服务

1.编写memcached自启动脚本或直接使用memcached安装包中的脚本(位置如下,本文使用官方自带的脚本)


Memcached高速缓存服务器部署及应用(二、Memcached在Linux平台下安装与配置)

 

2、将此脚本呢cp到/etc/init.d目录下并改名为memcached,然后执行chmod a+x memcached,如下

# cp memcached.sysv /etc/init.d/memcached
# chmod 755 /etc/init.d/memcached


3、修改自启动脚本(红色部分标识)

 

# vi /etc/init.d/memcached


 

#! /bin/sh
#
# chkconfig: - 55 45
# description: The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached

# Source function library.
. /etc/rc.d/init.d/functions

PORT=11211
USER=root
MAXCONN=1024
CACHESIZE=64
OPTIONS=""

if [ -f /etc/sysconfig/memcached ];then
. /etc/sysconfig/memcached
fi

# Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
exit 0
fi

RETVAL=0

start () {
echo "Starting memcached ..."
# insure that /var/run/memcached has proper permissions
chown $USER /usr/local/bin/memcached
/usr/local/bin/memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached.pid $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
}
stop () {
echo "Stopping memcached ..."
killproc memcached
RETVAL=$?
echo
if [ $RETVAL -eq 0 ] ; then
rm -f /var/lock/subsys/memcached
rm -f /var/run/memcached.pid
fi
}

restart () {
stop
start
}


# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status memcached
;;
restart|reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/memcached ] && restart || :
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
exit 1
esac

exit $?


4.将memcached加入系统服务

# cd /etc/init.d/
# chkconfig --add memcached# chkconfig memcached on


5.以服务方式运行memcached

# service memcached start


以上如成功则重启下系统后查看下memcached是否已随系统启动

 

友情链接:中视网