CentOS下安装生产环境可用的Memcached

2019-08-23 10:15:33 浏览数 (1)

(WJW)安装Memcached

[1] yum安装(也可以源码安装,在"make依赖库"目录下):

代码语言:javascript复制
yum install automake autoconf libtool

[2] 安装libevent

代码语言:javascript复制
tar -zxvf libevent-1.4.13-stable.tar.gz
cd libevent-1.4.13-stable
./configure --prefix=/usr/local
make uninstall
make clean
make
make install
cd ..

[3] 安装memcached

注意: 安装Memcached前flush_all的修改

查找:memcached.c文件,把 } else if (ntokens >= 2 && ntokens <= 4 && (strcmp(tokens[COMMAND_TOKEN].value, "flush_all") == 0)) { 修改成: } else if (ntokens >= 2 && ntokens <= 4 && (strcmp(tokens[COMMAND_TOKEN].value, "xflush_all") == 0)) {

如果是Memcached-Repcached版本,还要把replication.c里的: char *s = "flush_all "; 修改成: char *s = "xflush_all ";

代码语言:javascript复制
tar zxvf ./memcached-1.4.22.tar.gz
cd ./memcached-1.4.22
./configure --with-libevent=/usr/local/lib/ --enable-64bit
make uninstall
make clean
make
make install
cd ..

注意: 如果make的时候报错

memcached.c: 在函数'add_iov'中: memcached.c:696:30: 错误: 'IOV_MAX'未声明(在此函数内第一次使用) memcached.c:696:30: 附注: 每个未声明的标识符在其出现的函数内只报告一次 make[2]: *** [memcached-memcached.o] 错误 1

需要修改memcached.c文件:

代码语言:javascript复制
/* FreeBSD 4.x doesn't have IOV_MAX exposed. */
#ifndef IOV_MAX
#if defined(__FreeBSD__) || defined(__APPLE__)
# define IOV_MAX 1024
#endif
#endif

改成:

代码语言:javascript复制
/* FreeBSD 4.x doesn't have IOV_MAX exposed. */
#ifndef IOV_MAX
# define IOV_MAX 1024
#endif

注意: 如果运行/usr/local/bin/memcached -h的时候报错:找不到libevent** 执行: ln -s /usr/local/lib/libevent-1.4.so.2 /usr/lib64/libevent-1.4.so.2 技巧: 执行ldd /usr/local/bin/memcached,看缺少那些库!

[4] 终端调试运行:

执行: /usr/local/bin/memcached -m 100 -u root -p 11411 也可以启动多个守护进程,但是端口不能重复 启动参数说明:

-d 选项是启动一个守护进程, -m 是分配给Memcache使用的内存数量,单位是MB,默认64MB -M return error on memory exhausted (rather than removing items) -u 是运行Memcache的用户,如果当前为root 的话,需要使用此参数指定用户. -l 是监听的服务器IP地址,默认为所有网卡. -p 是设置Memcache的TCP监听的端口,最好是1024以上的端口 -c 选项是最大运行的并发连接数,默认是1024 -P 是设置保存Memcache的pid文件 -f chunk size growth factor (default: 1.25) -I Override the size of each slab page. Adjusts max item size(1.4.2版本新增)

[X] 附录:

memcached启动停止脚本:/etc/rc.d/init.d/memcached(注意:文件格式一定要是unix的)

vi /etc/rc.d/init.d/memcached

代码语言:javascript复制
#!/bin/sh
#
# memcached: MemCached Daemon
#
# chkconfig: - 90 25
# description: MemCached Daemon
#
# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
#[ ${NETWORKING} = "no" ] && exit 0
#[ -r /etc/sysconfig/dund ] || exit 0
#. /etc/sysconfig/dund
#[ -z "$DUNDARGS" ] && exit 0

start() {
  echo -n $"Starting memcached: "

  daemon $MEMCACHED -u daemon -d -m 100 -p 11411 -P ${PID_FILE}

  echo
}

stop() {
  echo -n $"Shutting down memcached: "
  killproc -p ${PID_FILE} memcached
  echo
}

MEMCACHED="/usr/local/bin/memcached"
PID_FILE="/tmp/memcached_repcached.pid"

[ -f $MEMCACHED ] || exit 1

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    sleep 3
    start
    ;;
  status)
    status -p ${PID_FILE} memcached
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 1
esac

exit 0

添加可执行: chmod x /etc/rc.d/init.d/memcached

0 人点赞