一:安装libevent
由于memcached安装时,需要使用libevent类库,所以先安装libevent
1.官网下载:http://libevent.org/
#wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
2.解压缩
#tar xzfv libevent-2.0.12-stable.tar.gz
3.进入目录
#cd libevent-2.0.12-stable
4. 编译,安装
# ./configure
# make
# make install
注:默认安装到/usr/local/lib/目录
二:安装Memcached
http://memcached.org/ 是Memcached的官方网站
1.下载
# wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
2.解压缩
#tar xzfv memcached-1.4.5.tar.gz
3.进入目录
#cd memcached-1.4.5
4. 编译,安装
./configure --prefix=/local/memcached
make
make install
安装完成后,会在 /local/memcached 出现 bin和share目录
进行 bin目录,启动 memcache
方法如下:
./memcached -d -u nobody -m 512 127.0.0.1 -p 11211
此时,会报一个异常
error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
原因是找不到libevent-2.0.so.5类库,解决办法如下:
使用LD_DEBUG=help ./memcached -v来确定 加载的类库路径,方法如下:
LD_DEBUG=libs ./memcached -v 2>&1 > /dev/null | less
出现(END)可以直接输入p退出
#ln -s /usr/local/lib/libevent-2.0.so.5 /lib/libevent-2.0.so.5
现在可以正常启动memcached了
./memcached -d -u nobody -m 512 127.0.0.1 -p 11211
到这里,看到memcached已经启动,说明安装成功。
三:安装memcache PHP模块
扩展官网:http://pecl.php.net/package/memcache
#wget http://pecl.php.net/get/memcache-2.2.7.tgz
# tar zxvf memcache-2.2.4.tgz
# cd memcache-2.2.4
找 phpize
#whereis phpize
本机是/usr/bin/phpize
# /usr/bin/phpize
# ./configure --with-php-config=/usr/bin/php-config
# make
# make install
在php.ini文件添加一行
php.ini在/etc下
extension=memcache.so
重启httpd
#service httpd restart
php里使用phpinfo()看到如下信息说明memcached扩展才算安装好!
<?php //连接Memcache $mem = new Memcache; $mem->connect("localhost", 11211); //保存数据 $mem->set('key1', 'This is first value', 0, 60); $val = $mem->get('key1'); echo "Get key1 value: " . $val ."<br>"; //替换数据 $mem->replace('key1', 'This is replace value', 0, 60); $val = $mem->get('key1'); echo "Get key1 value: " . $val . "<br>"; //保存数组数据 $arr = array('aaa', 'bbb', 'ccc', 'ddd'); $mem->set('key2', $arr, 0, 60); $val2 = $mem->get('key2'); echo "Get key2 value: "; print_r($val2); echo "<br>"; //删除数据 $mem->delete('key1'); $val = $mem->get('key1'); echo "Get key1 value: " . $val . "<br>"; //清除所有数据 $mem->flush(); $val2 = $mem->get('key2'); echo "Get key2 value: "; print_r($val2); echo "<br>"; //关闭连接 $mem->close(); ?>
在一台或者多台机器启用一个或者多个进程,这里是在一台机器启
<?php //连接Memcache $mem = new Memcache; $mem->addServer("localhost", 11211); $mem->addServer("localhost", 11212); //保存数据 $mem->set('key1', 'This is first value', 0, 60); $val = $mem->get('key1'); echo "Get key1 value: " . $val ."<br>"; //保存数组数据 $arr = array('aaa', 'bbb', 'ccc', 'ddd'); $mem->set('key2', $arr, 0, 60); $val2 = $mem->get('key2'); echo "Get key2 value: "; print_r($val2); echo "<br>"; //删除数据 $mem->delete('key1'); $val = $mem->get('key1'); echo "Get key1 value: " . $val . "<br>"; //关闭连接 $mem->close(); ?>
网上流行的PHP安装Memcached第二种方法安装和配置
需要把对应版本改为最新即可
1、安装Memcached
# tar vxzf memcached-1.1.12.tar.gz
# cd memcached-1.1.12
# ./configure --prefix=/usr/local/memcached
# make
# make install
安装完之后要启动服务
# cd /usr/local/memcached/bin
# ./memcached -d -m 50 -p 11211 -u root
参数说明 -m 指定使用多少兆的缓存空间;-p 指定要监听的端口; -u 指定以哪个用户来运行
2、安装memcache PHP模块
# tar vxzf memcache-1.5.tgz
# cd memcache-1.5
# /usr/local/php/bin/phpize
//注意下面不一样
# ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
# make
# make install
安装完后会有类似这样的提示:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/
把这个记住,然后修改php.ini,把
extension_dir = "./"
修改为
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/"
并添加一行
extension=memcache.so