安装Memcached缓存加速mysql

Memcached是mysql的K-V缓存插件,可以有效加速mysql,减轻mysql负担
但是这个插件是很占用内存的,小内存慎用.

1首先安装Libevent事件触发管理器。
Memcached依赖Libevent
centos可以直接 yum install libevent-devel libevent

编译可以去下面下载
https://github.com/nmathewson/Libevent/archive/release-2.1.7-rc.tar.gz

2.编译Memcached

wget http://memcached.org/files/memcached-1.4.33.tar.gz
tar zvxf memcached-1.4.33.tar.gz
cd memcached-1.4.33
./configure  #./configure -with-libevent=/usr/local/libevent   
make && make install

3 启动Memcached

/usr/local/bin/memcached -d -m 256 -l 127.0.0.1 -p 11211 -u root   
# (256为内存, 11211为端口,root为用户组)

4 开机/重启后生效,编辑 /etc/rc.d/rc.local 文件,添加以下内容。

echo "/usr/local/bin/memcached -d -m 256 -l 127.0.0.1 -p 11211 -u root" >>/etc/rc.d/rc.local 

5 检测
ps aux|grep memcached

来源: http://www.skyoy.com/directadmin-memcache.html

######为PHP添加memcached模块#############

1安装 libmemcached 库

wget  https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar -zxf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure --prefix=/usr/local/libmemcached --with-memcached        
#注意:--with-memcached这个选项一定要加上

2 安装完成后,开始安装memcached模块

wget http://pecl.php.net/get/memcached-2.2.0.tgz
tar -zxf memcached-2.2.0.tgz
cd memcached-2.2.0
#find / -name phpize     #找一下phpize 安装位置
#find / -name php-config    #找一下php-config安装位置, 注意替换下面的路径
/usr/local/php56/bin/phpize
#/usr/local/bin/phpize 
./configure --enable-memcached --with-php-config=/usr/local/php56/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached
#  ./configure --enable-memcached --with-php-config=/usr/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached
make && make install


/usr/local/php56/lib/php/extensions/no-debug-non-zts-20131226/
安装完后会有类似这样的提示:

Installing shared extensions: /usr/local/php56/lib/php/extensions/no-debug-non-zts-20131226/
把这个记住,然后修改php.ini,把

extension_dir = './'
修改为

extension_dir = '/usr/local/php56/lib/php/extensions/no-debug-non-zts-20131226/'
并添加一行
extension=memcached.so

php -m      #查看memcached模块是否安装

Leave a Comment