一:安裝libevent
由於memcached安裝時,需要使用libevent類庫,所以先安裝libevent
1.下載
#wget http://www.monkey.org/~provos/libevent-2.0.12-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.5.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模塊
#wget http://pecl.php.net/get/memcache-2.2.4.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 = 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; ?>常用操作
<?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