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