1、安装libeven
memcached的使用需要libeven的支持,我们得先装上libeven。
官方网站:http://www.monkey.org/~provos/libevent/
libevent安装方式比较简单:
./configure && make
make install
检查是否 已经安装
ls -l /usr/local/lib/ | grep even
2、安装memcached
官方网站:http://www.danga.com/memcached/download.bml
memcached安装需要些参数:
以下是代码片段:
./configure
make
make install
3、测试
/usr/local/bin/memcached -h
正常ok
常见错误
/usr/local/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory
解决办法
LD_DEBUG=libs memcached -v
输出
32179: find library=libevent-1.4.so.2 [0]; searching
32179: search cache=/etc/ld.so.cache
32179: search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib(system search path)
32179: trying file=/lib/tls/i686/sse2/libevent-1.4.so.2
32179: trying file=/lib/tls/i686/libevent-1.4.so.2
32179: trying file=/lib/tls/sse2/libevent-1.4.so.2
32179: trying file=/lib/tls/libevent-1.4.so.2
32179: trying file=/lib/i686/sse2/libevent-1.4.so.2
32179: trying file=/lib/i686/libevent-1.4.so.2
32179: trying file=/lib/sse2/libevent-1.4.so.2
32179: trying file=/lib/libevent-1.4.so.2
32179: trying file=/usr/lib/tls/i686/sse2/libevent-1.4.so.2
32179: trying file=/usr/lib/tls/i686/libevent-1.4.so.2
32179: trying file=/usr/lib/tls/sse2/libevent-1.4.so.2
32179: trying file=/usr/lib/tls/libevent-1.4.so.2
32179: trying file=/usr/lib/i686/sse2/libevent-1.4.so.2
32179: trying file=/usr/lib/i686/libevent-1.4.so.2
32179: trying file=/usr/lib/sse2/libevent-1.4.so.2
32179: trying file=/usr/lib/libevent-1.4.so.2
32179:
memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory
默认安装完后,存在:/usr/local/lib/libevent-1.4.so.2
执行如下操作
ln -s /usr/local/lib/libevent-1.4.so.2 /usr/lib/libevent-1.4.so.2
4、启动
/usr/local/bin/memcached -d -m 1024 -p 11211 -u root
参数说明:
-d 启动为守护进程
-m <num> 分配给Memcached使用的内存数量,单位是MB,默认为64MB
-u <username> 运行Memcached的用户,仅当作为root运行时
-l <ip_addr> 监听的服务器IP地址,默认为环境变量INDRR_ANY的值
-p <num> 设置Memcached监听的端口,最好是1024以上的端口
-c <num> 设置最大并发连接数,默认为1024
-P <file> 设置保存Memcached的pid文件,与-d选择同时使用
5、安装memcache PHP模块
下载地址:http://pecl.php.net/package/memcache
# tar vxzf memcache-2.2.5.tgz
# cd memcache-2.2.5
# /usr/local/bin/phpize #使用phpize建立php扩展
#./configure
# make
# make install
提示:/usr/local/lib/php/extensions/no-debug-non-zts-20060613/
然后修改php.ini
把
#extension_dir = "/usr/lib/php/modules"
修改为
extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613/"
并添加一行
extension=memcache.so
6、测试程序
<?php
$memcache = new Memcache;
$memcache->connect('localhost',11211) or die ("Could not connect");
$test = str_repeat('jetwong',100000);
//压缩内容
$memcache->set('b',($test),MEMCACHE_COMPRESSED);
#echo $memcache->get('b');
?>