CentOS7编译安装memcached

[size=large][color=brown]紧接上一篇http://assen.iteye.com/admin/blogs/2352416,本篇主要是memcached的安装与配置(建立在上一篇的系统配置上),安装包版本memcached-1.4.31.tar.gz。[/color][/size]

[size=large][b]第一步:安装libevent函数库 [/b][/size]
下载地址:https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz,上传服务器后解压。定位到解压后的根目录,执行如下编译命令:
#./configure -prefix=/usr/local/mysoft/libevent-2.0.21
[color=red]说明:-prefix指定到libevent函数库需要安装到的目录[/color]
编译完成之后,解压包的根目录底下执行安装命令:
# make && make install &&
# echo "安装libevent完毕!"
检验是否安装成功,定位到安装后的目录,执行如下命令:
# ls -al lib | grep libevent
或者
# ls -al /usr/local/mysoft/libevent-2.0.21/lib | grep libevent
能看到如下信息表示安装成功

lrwxrwxrwx. 1 root root 21 Jan 12 10:54 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 1140035 Jan 12 10:54 libevent-2.0.so.5.1.9
-rw-r--r--. 1 root root 2222058 Jan 12 10:54 libevent.a
lrwxrwxrwx. 1 root root 26 Jan 12 10:54 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 690230 Jan 12 10:54 libevent_core-2.0.so.5.1.9
-rw-r--r--. 1 root root 1394122 Jan 12 10:54 libevent_core.a
-rwxr-xr-x. 1 root root 1003 Jan 12 10:54 libevent_core.la
lrwxrwxrwx. 1 root root 26 Jan 12 10:54 libevent_core.so -> libevent_core-2.0.so.5.1.9
lrwxrwxrwx. 1 root root 27 Jan 12 10:54 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 471855 Jan 12 10:54 libevent_extra-2.0.so.5.1.9
-rw-r--r--. 1 root root 828008 Jan 12 10:54 libevent_extra.a
-rwxr-xr-x. 1 root root 1010 Jan 12 10:54 libevent_extra.la
lrwxrwxrwx. 1 root root 27 Jan 12 10:54 libevent_extra.so -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 968 Jan 12 10:54 libevent.la
lrwxrwxrwx. 1 root root 30 Jan 12 10:54 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 23963 Jan 12 10:54 libevent_pthreads-2.0.so.5.1.9
-rw-r--r--. 1 root root 21942 Jan 12 10:54 libevent_pthreads.a
-rwxr-xr-x. 1 root root 1031 Jan 12 10:54 libevent_pthreads.la
lrwxrwxrwx. 1 root root 30 Jan 12 10:54 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.9
lrwxrwxrwx. 1 root root 21 Jan 12 10:54 libevent.so -> libevent-2.0.so.5.1.9


[size=large][b]第二步:安装memcached [/b][/size]
下载地址:https://github.com/memcached/memcached/wiki/ReleaseNotes,下载版本memcached-1.4.31.tar.gz解压,定位到解压后的根目录,执行如下编译命令:
#./configure -prefix=/usr/local/mysoft/memcached
#--with-libevent=/usr/local/mysoft/libevent-2.0.21
[color=red]说明:-prefix指定到memcached需要安装到的目录,
--with-libevent就是第一步libevent函数库安装的目录
[/color]
编译完成之后,解压包的根目录底下执行安装命令:
# make && make install &&
# echo "安装memcached完毕!"
安装完后/usr/local/mysoft/memcached目录底下有bin、include、share三个文件夹,此时已安装成功。

[size=large][b]第三步:设置启动脚本、开机自启memcached: [/b][/size]
Xshell进入系统根目录,执行如下命令 vi /etc/init.d/memcached,创建脚本,目录需指定到/etc/init.d/底下。脚本命令:

#! /bin/bash
# memcached: MemCached Daemon
# chkconfig: - 90 25
# description: MemCached Daemon

#端口
PORT=11211
#启动用户
USER=root
#最大连接数
MAXCONN=65535
# 最大内存使用
# 留空将自动计算内存值;物理内存-512MB
CACHESIZE=512
OPTIONS=""
#memcache安装目录
memcached_location="/usr/local/mysoft/memcached"

RETVAL=0
prog="memcached"


# 留空将自动计算内存值;物理内存-512MB
if [ ".${CACHESIZE}" == "." ] ; then
mem_size=`free -m | grep "^Mem:" | awk '{print $2}'`
CACHESIZE=$(( ${mem_size} - 512 ))
fi

start () {
echo -n $"Starting $prog: "
if [ ! -f "/var/run/memcached.pid" ] ; then
touch /var/run/memcached.pid
chown ${USER}.${USER} /var/run/memcached.pid
fi
$memcached_location/bin/memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached.pid $OPTIONS
RETVAL=$?
echo
[ ! -d "/var/lock/subsys" ] && mkdir -p /var/lock/subsys/
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
}

stop () {
echo -n $"Stopping $prog: "
if [ ! -f "/var/run/memcached.pid" ] ; then
echo "memcached not running!"
exit 1 ;
fi
kill `cat /var/run/memcached.pid`
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
;;
restart|reload)
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload}"
exit 1
esac

exit $?



[color=red]参数说明:
PORT:memcached端口,默认11211
USER:系统用户,默认root
MAXCONN:最大可连接数,根据实际配置
CACHESIZE:缓存大小,根据实际设置
memcached_location:memcached安装成功的目录
memcached.pid文件建议放在/var/run/底下
[/color]

[size=large][b]第四步:添加到服务 [/b][/size]
依次执行如下命令:
chmod a+x /etc/init.d/memcached
chkconfig --add memcached
chkconfig nginx on
/etc/init.d/memcached start

最后使用命令:
$ service memcached start
$ service memcached stop
$ service memcached restart
$ service memcached reload

$ /etc/init.d/memcached start
$ /etc/init.d/memcached stop
$ /etc/init.d/memcached restart
$ /etc/init.d/memcached reload

你可能感兴趣的:(笔记)