Linux操作系统安装memcached

时间:2022-08-18 09:43:57

进入下载目录

cd /usr/local 


安装libevent

wget https://github.com/libevent/libevent/archive/release-1.4.12-stable.tar.gz


由于memcached是基于libevent的,因此需要安装libevent,libevent-devel。

tar -zxvf libevent-1.4.12-stable.tar.gz

cd libevent-1.4.12

./configure --prefix=/usr/local/libevent

make

make install


安装memcached

wget http://www.memcached.org/files/memcached-1.4.25.tar.gz

tar -zxvf memcached-1.4.25.tar.gz

cd memcached-1.4.25

./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent --enable-64bit --enable-threads

make

make install


PS:

--enable-64bit     64位系统需要这个参数 

--enable-threads   支持多线程 


启动memcache服务

/usr/local/memcached/bin/memcached -d -m 200 -u root -l 127.0.0.1 -p 11211 -c 1000 -P /var/run/memcached.pid

相关解释如下:
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,这里是200MB
-u是运行Memcache的用户,如果当前为 root 的话,需要使用此参数指定用户。
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址127.0.0.1
-p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口
-c选项是最大运行的并发连接数,默认是1024,这里设置了256
-P是设置保存Memcache的pid文件,我这里是保存在 /var/run/memcached.pid
停止Memcache进程:
# kill `cat/var/run/memcached.pid`
也可以启动多个守护进程,但是端口不能重复

开机自启动

首先请登陆你的linux服务器在/etc/rc.d/init.d/目录下创建一个文件

touch /etc/init.d/memcached  


给启动脚本赋予执行权限

chmod +x /etc/init.d/memcached

接着用vi memcached来编辑这个文件
接着在memcached里面输入如下内容

#!/bin/bash  
# Startup script for the memcached
# chkconfig: 2345 55 45
# description: The memcached daemon is a network memory cache service.
# processname: memcached
# pidfile: /var/run/memcached.pid
EXEC=/usr/local/memcached/bin/memcached
PIDFILE=/var/run/memcached.pid
RETVAL=0
prog="memcached"
# Source function library.
. /etc/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $EXEC ] || exit 0
# Start memcached daemons functions.
start() {
if [ -e $PIDFILE ];then
echo "memcached already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $EXEC -d -p 11211 -u root -m 128 -c 1024 -P $PIDFILE
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/memcached
return $RETVAL
}
# Stop memcahced daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc memcached
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/memcached $PIDFILE
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
condrestart)
[ -f /var/lock/subsys/memcached ] && restart || :
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|condrestart}"
exit 1
esac
exit $RETVAL


添加服务

chkconfig --add memcached


设置开机启动

chkconfig --level 35 memcached on  


查看是否设置成功

chkconfig --list | grep memcached