Memcached 服務器(192.168.220.169) memcached-1.5.6.tar.gz、libevent-2.1.8-stable.tar.gzphp
Memcache 客戶端(192.168.220.131) memcache-2.2.7.tgz 、LAMP(httpd、mysql、php)mysql
(1)安裝依賴環境包: yum install gcc gcc-c++ make -y (2)解壓軟件包: tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/ tar zxvf memcached-1.5.6.tar.gz -C /opt/ (3)編譯安裝 libevent : cd /opt/libevent-2.1.8-stable ./configure --prefix=/usr/local/libevent make && make install (4)編譯安裝 memcached : cd /opt/memcached-1.5.6/ ./configure \ --prefix=/usr/local/memcached \ --with-libevent=/usr/local/libevent/ make && make install (5)方便操做能夠建立一個軟連接: ln -s /usr/local/memcached/bin/* /usr/local/bin (6)開啓 memcached 服務: memcached -d -m 32m -p 11211 -u root //-d守護進程 ;-m緩存大小32M ;-p端口11211 (7)查看端口是否正常開發: netstat -natp | grep memcached
mysql -u root -p //進入數據庫 CREATE DATABASE sky; //建立一個數據庫爲 sky GRANT all ON sky.* TO 'skyuser'@'%' IDENTIFIED BY 'admin123'; //提權 flush privileges; //刷新數據庫
vim /usr/local/httpd/htdocs/index.php <?php $link=mysql_connect('192.168.220.131','skyuser','admin123'); if($link) echo "<h1>Success!!</h1>"; else echo "Fail!!"; mysql_close(); ?>
(1)安裝依賴包: yum install autoconf -y (2)解壓: tar zvxf memcache-2.2.7.tgz -C /opt/ (3)編譯(使用PHP的phpize腳本生成配置腳本configure,再進行配置編譯): cd /opt/memcache-2.2.7 /usr/local/php5/bin/phpize ./configure \ --enable-memcache \ --with-php-config=/usr/local/php5/bin/php-config (4)安裝: make && make install (5)安裝完成後,會出現一行,這是共享文件的位置,後面要用到: /usr/local/php5/lib/php/extensions/no-debug-zts-20131226/
vim /usr/local/php5/php.ini //搜索並修改下面一行,再新增一行 extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/" extension = memcache.so
vim /usr/local/httpd/htdocs/index.php <?php $memcache=new Memcache(); $memcache->connect('192.168.220.169',11211); ##鏈接Memcached服務器地址 $memcache->set('key','Memcache test Successfull!',0,60); $result=$memcache->get('key'); unset($memcache); echo$result; ?>
yum install telnet -y telnet 127.0.0.1 11211 //鏈接本地用 127.0.0.1就能夠了,鏈接其餘主機使用對應IP地址便可 1
add username 0 0 7 //添加數據(兩個0表示:不進行壓縮和序列化標識,數據過時時間爲永不過時;標識號是7就須要輸入7位數。) allways //輸入一個7位數
get username //查詢數據 gets username
set username 0 0 10 //更新信息,若鍵名不存在,則自行添加 everything replace username 0 0 8 //更新信息,若鍵名不存在,則報錯 12345678
gets username //檢測更新 VALUE username 0 8 4 12345678
append username 0 0 7 //鍵值後追加數據 example prepend username 0 0 2 //鍵值前追加數據 un
delete username //清除指定的鍵值數據 flush_all //清除全部緩存數據 OK
stats //顯示狀態信息 stats items //返回全部鍵值對的統計信息 stats cachedump 1 0 //返回指定存儲空間的鍵值對 stats slabs //顯示各個slab的信息 stats sizes //輸出全部item的大小和個數 stats reset //清空統計數據
quit