1. download
http://www.monkey.org/~provos/libevent-2.0.13-stable.tar.gz
http://memcached.googlecode.com/files/memcached-1.4.7.tar.gz
2. install
#tar zxvf libevent-2.0.13-stable.tar.gz
#cd libevent-2.0.13-stable
#./configure (--prefix=/opt/apps_install/libevent)
#make
#make install
# ls -al /opt/apps_install/libevent/lib | grep libevent
#tar zxvf memcached-1.4.7.tar.gz
#cd memcached-1.4.7
#./configure ( --prefix=/opt/apps_install/memcache --with-libevent=/opt/apps/libevent/)
#make
#make install
# ls -al /opt/apps_install/memcache/bin/mem*
3. manage memcached
# /opt/apps_install/memcache/bin/memcached -d -m 100 -u root -l 192.168.160.129 -p 11211 -c 256 -P /tmp/memcached.pid
-d選項是啓動一個守護進程,
-m是分配給Memcache使用的內存數量,單位是MB,我這裏是100MB,
-u是運行Memcache的用戶,我這裏是root,
-l是監聽的服務器IP地址,如果有多個地址的話,我這裏指定了服務器的IP地址192.168.36.200,
-p是設置Memcache監聽的端口,我這裏設置了11211,最好是1024以上的端口,我們這裏統一使用11211
-c選項是最大運行的併發連接數,默認是1024,我這裏設置了256,按照你服務器的負載量來設定。
-P是設置保存Memcache的pid文件,我這裏是保存在/tmp/memcached.pid
#netstat -anp | grep 1121
#kill `cat /tmp/memcached.pid`
4. use memcached
1>java client
http://www.whalin.com/memcached
http://code.google.com/p/spymemcached/
http://code.google.com/p/xmemcached/
參考: http://koda.iteye.com/blog/471570
2>python client
3>cmd client
a) stats(slabs5|items6|malloc) version quit flush_all
b) set add replace get delete
command <key> <flags> <expiration time> <bytes>
<value>
參數 用法
key key 用於查找緩存值
flags 可以包括鍵值對的整型參數,客戶機使用它存儲關於鍵值對的額外信息
expiration time 在緩存中保存鍵值對的時間長度(以秒爲單位,0 表示永遠)
bytes 在緩存中存儲的字節點
value 存儲的值(始終位於第二行)
5. illustrate:
1> gcc -V
2> close firewall
#永久性生效,重啓後不會復原
開啓: chkconfig iptables on
關閉: chkconfig iptables off
#即時生效,重啓後復原
開啓: service iptables start
關閉: service iptables stop
#修改/etc/sysconfig/iptables文件,添加
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT
3>建議設定memcache的telnet訪問限制
4>memcached分配內存(allocator): memcached ->slab(1:n)->page(1:n)->chunk(1:n)
5> 存在問題:
/usr/local/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
解決:
#vim /etc/profile
add:export LD_LIBRARY_PATH=/usr/local/lib::$LD_LIBRARY_PATH
#source /etc/profile
6>運行監控:
http://lveyo.iteye.com/blog/240154
參考資料:
http://memcached.org/
http://code.google.com/p/memcached/wiki
http://code.jellycan.com/memcached/
http://zhaohaolin.iteye.com/blog/999235
http://lveyo.iteye.com/blog/240146