Memcache的部署文档
下载libevent和memcached
cd /usr/local/src/
解压路径/usr/local/,并做软连接
先安装libevent-2.1.8-stable.tar.gz(./configure && make && make install)
再安装 memcached-1.4.35.tar.gz(./configure && make && make install)
wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz
wget http://memcached.org/files/memcached-1.4.35.tar.gz
一、配置ld.so.conf 路径,防止启动memcached时报错
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig ß生效
二、启动memcached
/usr/local/bin/memcached -p 11211 -u root -m l6m -c 10240 -d
-p 指定监听的端口,默认是11211
-l 指定监听的地址,默认本机所有网卡的地址
-d demon模式运行在后台运行
-m 给它分配的最大内存区,默认是64M
-c 最大能承受多大并发,默认是1024
-u 指定用户
-P pid放到指定文件中。
-vv 调试信息输出到控制台
三、memcached启动脚本
#! /bin/bash
# memcached: MemCached Daemon
# chkconfig: - 90 25
# description: MemCached Daemon
### BEGIN INIT INFO
# Provides: memcached
# Required-Start: $syslog
# Required-Stop: $syslog
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: memcached - Memory caching daemon
# Description: memcached - Memory caching daemon
### END INIT INFO
IP=19.168.88.136
PORT=11211
USER=root
MAXCONN=1024
CACHESIZE=64
OPTIONS=""
RETVAL=0
prog="memcached"
start () {
echo -n $"Starting $prog: "
/usr/local/memcached/bin/memcached -d -l $IP -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached.pid $OPTIONS
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
touch /var/lock/subsys/memcached
echo " done"
fi
}
stop () {
echo -n $"Stopping $prog: "
if [ ! -e /var/run/$prog.pid ]; then
echo -n $"$prog is not running."
exit 1
fi
kill `cat /var/run/memcached.pid`
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
rm -f /var/lock/subsys/memcached
rm -f /var/run/memcached.pid
echo " done"
fi
}
restart () {
$0 stop
sleep 2
$0 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 $?
四、下载memcache客户端,并编译
cd /usr/local/src/
http://pecl.php.net/package/memcache
wget http://pecl.php.net/get/memcache-3.0.8.tgz
tar -xzf memcache-3.0.8.tgz
cd memcache-3.0.8
#编译
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
这时会在
/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226
目录下生成一个 memcache.so 的文件
备注:这个目录每一台机器都不一样
五、将 memcache.so 加入到php.ini的文件中
六.重启php-fpm,并测试memcache和php的连接情况
<?php
$mem = new Memcache;
$mem->connect("10.47.61.142", 11211);//11211上上面设置的监听端口
$mem->set('key', 'it does work!');
$val = $mem->get('key');
echo $val;
?>