linux下安装memcache(php版本5.3)
所需软件
memcache-2.2.3.tgz (http://pecl.php.net/package/memcache)
memcached-1.2.6.tar.gz (http://www.danga.com/memcached/)
2. 安装libevent
# tar zxf libevent-1.4.6-stable.tar.gz
# cd libevent-1.4.6-stable
# ./configure --prefix=/usr/local/libevent
# make && make install
3. 安装memcached
# tar zxvf memcached-1.2.6.tar.gz
# cd memcached-1.2.6
# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
# make && make install
4. 运行memcached[红色的仔细看]
# /usr/local/memcached/bin/memcached -d -m 128 -l localhost -p 11211 -u root
如果遇到运行memcached时遇到问题,错误提示如下:
/usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory
则运行 LD_DUBUG=libs 就可以知道memcached启动时库的加载路径。具体如下:
# LD_DEBUG=libs /usr/local/memcached/bin/memcached -v
然后给libevent-1.4.so.2建立一个链接后再运行memcached:
# ln -s /usr/local/libevent/lib/libevent-1.4.so.2 /lib/libevent-1.4.so.2
5.安装PHP的memcache
PHP环境下使用memcached:第一种,PHP 有一个叫做
memcache 的扩展
,Linux 下编译时需要带上 ?enable-memcache[=DIR] 选项,Window 下则在 php.ini 中去掉 php_memcache.dll 前边的注释符,使其可用。
第二种是直接使用
php-memcached-client
但其效率会比扩展库稍差一些
这是memcache 的扩展
可以使用php自带的pecl安装程序
# /usr/local/php5/bin/pecl install memcache
# tar zxf memcache-2.2.3.tgz
# cd memcache-2.2.3
# /usr/local/php/bin/phpize
#./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config
#make
#make install
返回:Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
表示成功安装!
6.检查服务
(1)查看启动的memcache服务:
netstat -lp | grep memcached
![](http://images0.cnblogs.com/blog2015/428423/201504/081143462585371.png)
(2)查看memcache的进程号(根据进程号,可以结束memcache服务:“kill -9 进程号”)
ps -ef | grep memcached
![](http://images0.cnblogs.com/blog2015/428423/201504/081144284461382.png)
将memcache扩展加入到php.ini
extension=memcache.so
![](http://images0.cnblogs.com/blog2015/428423/201504/081144531961621.png)
测试:
phpinfo();
![](http://images0.cnblogs.com/blog2015/428423/201504/081145279305610.png)
程序测试:
<?php
$memcache = new Memcache; //创建一个memcache对象
$memcache->connect('localhost', 11211) or die ("Could not connect"); //连接Memcached服务器
$memcache->set('key', 'test'); //设置一个变量到内存中,名称是key 值是test
$get_value = $memcache->get('key'); //从内存中取出key的值
echo $get_value;
?>
![](http://images0.cnblogs.com/blog2015/428423/201504/081146072119108.png)