memcached 安装配置

2099 查看

依赖:

linevent 获取: http://libevent.org/
编译(旧版本可能会被其他软件依赖, 此处建议安装在独立目录):

# ./configure --prefix=/usr/local/libevent
# make && make install
# echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf
# ldconfig 

编译:

memcached 获取: https://memcached.org/

# ./configure --prefix=/usr/local/memcached \
# --with-libevent=/usr/local/libevent #若是编译安装libevent, 需要指定libevent的目录
# make && make install

提供memcached 服务脚本:

#!/bin/bash
#
# Init file for memcached
#
# chkconfig: - 86 14
# description: Distributed memory caching daemon
#
# processname: memcached
# config: /etc/sysconfig/memcached
 
. /etc/rc.d/init.d/functions
 
## Default variables
PORT="11211"
USER="nobody"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
 
[ -f /etc/sysconfig/memcached ] && . /etc/sysconfig/memcached
 
RETVAL=0
prog="/usr/local/memcached/bin/memcached"
desc="Distributed memory caching"
lockfile="/var/lock/subsys/memcached"
 
start() {
        echo -n $"Starting $desc (memcached): "
        daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch $lockfile
        return $RETVAL
}
 
stop() {
        echo -n $"Shutting down $desc (memcached): "
        killproc $prog
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f $lockfile
        return $RETVAL
}
 
restart() {
        stop
        start
}
 
reload() {
        echo -n $"Reloading $desc ($prog): "
        killproc $prog -HUP
        RETVAL=$?
        echo
        return $RETVAL
}
 
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  condrestart)
        [ -e $lockfile ] && restart
        RETVAL=$?
        ;;      
  reload)
        reload
        ;;
  status)
        status $prog
        RETVAL=$?
        ;;
   *)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        RETVAL=1
esac
 
exit $RETVAL

memcached的常用选项说明:

-l <ip_addr>:指定进程监听的地址;
-d: 以服务模式运行;
-u <username>:以指定的用户身份运行memcached进程;
-m <num>:用于缓存数据的最大内存空间,单位为MB,默认为64MB;
-c <num>:最大支持的并发连接数,默认为1024;
-p <num>: 指定监听的TCP端口,默认为11211;
-U <num>:指定监听的UDP端口,默认为11211,0表示关闭UDP端口;
-t <threads>:用于处理入站请求的最大线程数,仅在memcached编译时开启了支持线程才有效;
-f <num>:设定Slab Allocator定义预先分配内存空间大小固定的块时使用的增长因子;
-M:当内存空间不够使用时返回错误信息,而不是按LRU算法利用空间;
-n: 指定最小的slab chunk大小;单位是字节;
-S: 启用sasl进行用户认证;

启动memcached 服务:

# chmod +x /etc/init.d/memcached
# chkconfig --add memcached
# service memcached start

给PHP 安装memcached 扩展:

# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
# make && make install

编辑php.ini 文件:

extension=memcache.so

查看模块加载情况:

[root@bogon ~]# php -m
[PHP Modules]
bz2
Core
ctype
date
dom
ereg
fileinfo
filter
hash
iconv
json
libxml
mbstring
mcrypt
memcache <-- 在这
mysql
mysqli
openssl
pcre
PDO
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
sockets
SPL
sqlite3
standard
tokenizer
XCache
XCache Cacher
xml
xmlreader
xmlwriter
zlib
 
[Zend Modules]
XCache
XCache Cacher

轻量级Memcached管理监控工具 --> memadmin:

获取: http://www.junopen.com/memadmin/
基于PHP 的Web 管理程序, 配置很简单
编辑config.php 配置账户密码之后直接访问登录即可