通过在内存里维护一个统一的巨大的Hash表,能够用来存储各种格式的数据
* 查看当前的memcache连接数
# netstat -n | grep :11211 | wc -l
* 查看默认允许的连接数,这里设置的11211端口默认连接数是40960
# cat /etc/rc.local
* 如何查看memcache服务器端版本
# ./memcached -h
* Linux下的Memcache安装
# cd /usr/local/src
装yum扩展源:# yum install -y epel-release
memcache模块:# yum install -y php-pecl-memcached
下载Memcache:# wget http://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz
下载libevent:# wget http://www.monkey.org/~provos/libevent-1.2.tar.gz
先装libevent:# tar zxvf libevent-1.2.tar.gz
# cd libevent-1.2
# ./configure --prefix=/usr/local/libevent
# make
# make install
* 测试libevent是否安装成功
# ls -al /usr/local/libevent/lib | grep libevent
lrwxrwxrwx 1 root root 21 Apr 14 13:01 libevent-1.2.so.1 -> libevent-1.2.so.1.0.3
-rwxr-xr-x 1 root root 264056 Apr 14 13:01 libevent-1.2.so.1.0.3
-rw-r--r-- 1 root root 430388 Apr 14 13:01 libevent.a
-rwxr-xr-x 1 root root 820 Apr 14 13:01 libevent.la
lrwxrwxrwx 1 root root 21 Apr 14 13:01 libevent.so -> libevent-1.2.so.1.0.3
安装memcached # cd /usr/local/src
# yum install -y libevent-devel
# tar zxvf memcached-1.2.0.tar.gz
# cd memcached-1.2.0
# ./configure --with-libevent=/usr/local/memcached
# make
# make install
* 测试是否成功安装memcached
# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 113084 Apr 14 13:23 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 117439 Apr 14 13:23 /usr/local/bin/memcached-debug
* 安装Memcache的PHP扩展
1.在http://pecl.php.net/package/memcache,选择想要下载的memcache版本
2.安装PHP的memcache扩展
# tar zxvf memcache-2.2.1.tgz
# cd memcache-2.2.1
# /usr/local/php5.6/bin/phpize
运行这个命令就会生成一个脚本,然后编译扩展
# ./configure --enable-memcache --with-php-config=/usr/local/php5.6/bin/php-config --with-zlib-dir
# make
# make install
# vim /usr/local/php5.6/etc/php.ini
extension_dir = "/usr/local/php5.6/lib/php/extensions/no-debug-non-zts-20131226/"
extension=memcache.so
* memcached的基本设置
1)启动Memcache的服务器端
# /usr/local/bin/memcached -d -m 100 -u root -l 127.0.0.1 -p 12000 -c 300 -P /tmp/mem cached.pid
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
2)如果要结束Memcache进程,执行:
# kill `cat /tmp/memcached.pid`
3)重启apache,service httpd restart
4)Memcache环境测试
运行下面的php文件,如果有输出This is a test! 就表示环境搭建成功
# cd /var/www/html
# vim hjt.php
<?php
$mem = new Memcache;
$mem->connect("127.0.0.1", 11211);
$mem->set('key', 'This is a test!', 0, 60);
$val = $mem->get('key');
echo $val;
?>
原文:http://blog.51cto.com/amazing61/2063883