Linux上完美安装Memcache(含自启动)

一、安装依赖库libevent

1.进入/usr/lib目录,使用命令:ls -al libevent 查看是否已安装,如果已安装且版本低于2.0.21,则先通过:rpm -e libevent —nodeps 进行卸载。

2.上传libevent-2.0.21-stable.tar.gz至非usr目录下(因为libevent将安装到usr目录,所以最好换个目录存放安装文件,例如/home),然后解压。

3.切换到解压后的 libevent 主目录:cd libevent-2.0.21-stable

4.依次执行:

1) ./configure -prefix=/usr(或 ./configure --program-prefix=/usr

2) make

3) make install

注意:

1) 执行 make install 时可能需要 root 权限。

2) libevent会安装到 /usr/lib下

5.测试libevent是否安装成功:ls -al /usr/lib|grep libevent出现类似下图结果则表示安装成功:

Linux上完美安装Memcache(含自启动)

6.有时候,libevent会被安装目录到/usr/local/lib下(采用默认安装就会安装到这个目录),这种情况下除了按照1-5的步骤操作外,还需要建立libevent-2.0.so.5 到 /usr/lib 的软连接,这样其他程序运行时才能找到libevent库:

ln –s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5

7.在64位机器上还需做一个64位库的链接,否则运行memcache时会报找不到libevent的错误

ln -s/usr/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5

二、安装memcache(源代码安装)

1.上传memcached-1.4.15.tar.gz至非usr目录下(因为memcached-1.4.15将安装到usr目录,所以最好换个目录存放安装文件,例如/home),然后解压。

2.切换到解压后的 memcache主目录:cd memcached-1.4.15

3.依次执行:

1) ./configure -prefix=/usr/local/memcached --with-libevent=/usr

2) make

3) make install

注意:

1) 执行 make install时可能需要 root权限。

2) memcache会安装到 /usr/local/memcached

三、安装memcache(安装包安装)--待补充 四、配置为自启动服务

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

2.修改自启动脚本(红色部分标识,中文注释仅限本文提示用,安装包没有)

#! /bin/sh

#

# chkconfig: - 55 45

# description: The memcached daemon is a network memory cache service.

# processname: memcached

# config: /etc/sysconfig/memcached

# Source function library - for other linux

#. /etc/rc.d/init.d/functions

# Source function library - for SUSE linux

. /lib/lsb/init-functions

PORT=11211

USER=root

#最大连接数,根据实际需求修改

MAXCONN=1024

#最大内存量,单位M

CACHESIZE=128

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

prog="memcached"

start () {

echo -n $"Starting $prog: "

# insure that /usr/local/memcached has proper permissions

chown $USER /usr/local/memcached

/usr/local/memcached/bin/memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /usr/local/memcached/memcached.pid $OPTIONS

RETVAL=$?

echo

[ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached

}

stop () {

echo -n $"Stopping $prog: "

killproc memcached

RETVAL=$?

echo

if [ $RETVAL -eq 0 ] ; then

rm -f /var/lock/subsys/memcached

rm -f /usr/local/memcached/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 $?

 

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

shell > cp memcached.sysv /etc/init.d/memcached

shell > chmod 755 /etc/init.d/memcached

4.将memcached加入系统服务:

shell > chkconfig --add memcached

shell > chkconfig memcached on

5.以服务方式运行memcached
shell > service memcached start

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/19715.html