安装memecache

wget http://memcached.org/latest
tar -zxvf memcached-1.x.x.tar.gz
cd memcached-1.x.x
./configure && make && make test && sudo make install # make test可以省略
  • 报错处理:找不到libevent
wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz
tar zxvf libevent-2.1.8-stable.tar.gz -C /usr/local/src/
cd /usr/local/src/
./configure --prefix=/usr/local/
make && make install
  • 编译时指定libevent
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/
make && make install

启动memecache

[root@Node1 bin]# ./memcached -d -m 10 -u root -l 127.0.0.1 -p 12000 -c 256 -P /tmp/memcache.pid

# 参数说明:
-p 指定端口号(默认11211)
-m 指定最大使用内存大小(默认64MB)
-t 线程数(默认4)
-l 连接的IP地址, 默认是本机
-d 以后台守护进程的方式启动
-c 最大同时连接数,默认是1024
-P 制定memecache的pid文件
-h 打印帮助信息
[root@Node1 bin]# ps aux|grep memecache
root 116022 0.0 0.0 103320 892 pts/0 S+ 09:16 0:00 grep memecache [root@Node1 bin]# netstat -nlp|grep 12000
tcp 0 0 127.0.0.1:12000 0.0.0.0:* LISTEN 116011/./memcached
udp 0 0 127.0.0.1:12000 0.0.0.0:* 116011/./memcached
[root@Node1 bin]# netstat -nlp|grep memcache
tcp 0 0 127.0.0.1:12000 0.0.0.0:* LISTEN 116011/./memcached
udp 0 0 127.0.0.1:12000 0.0.0.0:* 116011/./memcached

连接memcache

telnet 127.0.0.1 12000

python 连接memcache

import memcache

mc = memcache.Client(['11.11.11.11:12001'],debug=True)
print(mc.get('k1')) mc = memcache.Client(['11.11.11.11:12001'],debug=True)
print(mc.get('k1')) mc = memcache.Client(['11.11.11.11:12001'],debug=True)
print(mc.get('k1'))

python 操作memcache 集群

import memcache

mc = memcache.Client([('11.11.11.11:12000', 1), ('11.11.11.11:12001', 2), ('11.11.11.11:12002',1)], debug=True)
mc.set('k1', 'v1')
print(mc.get('k1'))
05-11 17:49