Memcache配置手册

snow chuai汇总、整理、撰写---2020/1/31
最后更新日期---2020/09/28


1. 安装及配置Memcache
[root@node1 ~]# yum install memcached -y
[root@node1 ~]# vim /etc/sysconfig/memcached
# 设定监听端口
PORT="11211"
# 设定进程的属主
USER="memcached"
# 设定最大连接数
MAXCONN="1024"
# 设定最大缓存M数
CACHESIZE="64"
# 设定监听的IP
OPTIONS="-l 0.0.0.0"
[root@node1 ~]# systemctl enable --now memcached
[root@node1 ~]# firewall-cmd --add-port=11211/tcp --permanent success [root@node1 ~]# firewall-cmd --reload success
2. 基本使用
[root@node1 ~]# yum install telnet -y
[root@node1 ~]# telnet localhost 11211 Trying ::1... telnet: connect to address ::1: Connection refused Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. stats # 显示Memcache状态 STAT pid 28849 STAT uptime 202 STAT time 1580478019 STAT version 1.5.6 STAT libevent 2.0.21-stable STAT pointer_size 64 STAT rusage_user 0.025928 ...... ......
设定键、值保存在内存中 语法:设置[键][标志][有效期(秒)][数据大小(字节)] 标志:0=关闭压缩,1=打开压缩 有效期=0表示不确定 输入上述命令后,输入一个键的值 set test_key 0 0 10 test_value STORED
get test_key VALUE test_key 0 10 test_value END
replace test_key 0 0 11 # 改变test_key的值 test_value2 STORED
get test_key VALUE test_key 0 11 test_value2 END
append test_key 0 0 5 # 追加的值 ,test STORED
get test_key VALUE test_key 0 16 test_value2,test END
prepend test_key 0 0 6 # 预置值 test1, STORED
get test_key VALUE test_key 0 22 test1,test_value2,test END
delete test_key # 删除key DELETED
# 对指定key增量其值 set mycounter 0 0 1 1 STORED
incr mycounter 1 2 get mycounter VALUE mycounter 0 1 2 END
# 对指定key递减其值 decr mycounter 1 1 get mycounter VALUE mycounter 0 1 1 END
flush_all # 删除内存中所有缓存的数据 OK
# CAS (Check And Set)操作 # 使用CAS命令,更新数据.CAS ID为12 cas语法格式:cas [键] [标记] [有效期(sec)] [数据大小(byte)] [CAS ID] gets test_key VALUE test_key 0 10 12 test_value END
cas test_key 0 0 11 12 test2_value STORED
gets test_key VALUE test_key 0 11 13 test2_value END
quit # 退出Telnet
3. 支持Python
1) 安装Python Memcached客户端
[root@node1 ~]# yum install python-memcached -y
2) 测试 [root@node1 ~]# vim use-memcached.py #!/usr/bin/env python
import memcache
client = memcache.Client(["127.0.0.1:11211"], cache_cas=True)
# 设置并获取一个Key client.set("key01", "value01") print "key01.value :", client.get("key01")
# 追加并确认 Key client.append("key01", ",value02") print "key01.value :", client.get("key01")
client.set("key02", 1)
# 增量 client.incr("key02", 100) print "key02.value :", client.get("key02")
# 递减 client.decr("key02", 51) print "key02.value :", client.get("key02")
# CAS操作 client.set("key03", "value03") print "key03.value :", client.gets("key03") print "key03.casid :", client.cas_ids["key03"] client.cas("key03", "value04") print "key03.value :", client.gets("key03")

[root@node1 ~]# python use-memcached.py key01.value : value01 key01.value : value01,value02 key02.value : 101 key02.value : 50 key03.value : value03 key03.casid : 26 key03.value : value04
4. 支持PHP
1) 安装Python Memcached客户端
[root@node1 ~]# yum --enablerepo=epel install php-pecl-memcached -y
2) 测试 [root@node1 ~]# vim use-memcached.php <?php $memcache = new Memcached(); $memcache->addServer('localhost', 11211); $memcache->setOption(Memcached::OPT_COMPRESSION, false); # 设置并获取一个Key $memcache->set('key01', 'value01'); print 'key01.value : ' . $memcache->get('key01') . "\n";
# 追加并确认 Key $memcache->append('key01', ',value02'); print 'key01.value : ' . $memcache->get('key01') . "\n";
$memcache->set('key02', 1); print 'key02.value : ' . $memcache->get('key02') . "\n";
# 增量 $memcache->increment('key02', 100); print 'key02.value : ' . $memcache->get('key02') . "\n";
# 递减 $memcache->decrement('key02', 51); print 'key02.value : ' . $memcache->get('key02') . "\n";
$memcache->set('key03', 'value03'); print 'key03.value : ' . $memcache->get('key03') . "\n";
# CAS操作 $memcache->get('key03', null, $cas); $memcache->replace('key03', 'value04'); if ($memcache->getResultCode() == Memcached::RES_NOTFOUND) { $memcache->add('key03', 'value03'); } else { $memcache->cas($cas, 'key03', 'value05'); } print 'key03.value : ' . $memcache->get('key03') . "\n"; ?>

[root@node1 ~]# php use-memcached.php key01.value : value01 key01.value : value01,value02 key02.value : 1 key02.value : 101 key02.value : 50 key03.value : value03 key03.value : value04
5. Magent实现
1) magent说明
Magent是一款开源的Memcached代理服务器软件,采用 Magent 缓存代理,防止单点现象,缓存代理也可以做备份,通过客户端连接到缓存代理服务器,缓存代理服务器连接缓存服务器,缓存代理服务器可以连接多台Memcached机器;
2) 前期准备 1. 至少2台Memcached服务存在,并处于star状态。本示例为srv3及srv4两台服务器开启memcached服务
2. 拓扑: +----------+ | magent | +-----+----+ eth0|192.168.10.11/24 | +------------+ | +------------+ | Backend1 |192.168.10.13|192.168.10.14| Backend2 | | memcached +-------------+-------------+ memcached | | srv3 | | srv4 | +------------+eth0 eth0+------------+ 3) 下载magent [root@srv1 ~]# mkdir -v magent [root@srv1 ~]# cd -v magent [root@srv1 magent]# wget \ https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/memagent/magent-0.6.tar.gz
4) 编译并安装magent [root@srv1 magent]# tar xvfz magent-0.6.tar.gz [root@srv1 magent]# yum install gcc libevent-devel -y [root@srv1 magent]# make # 错误1: 如果出现以下错误 magent.c: In function ‘writev_list’: magent.c:729:17: error: ‘SSIZE_MAX’ undeclared (first use in this function) if (toSend > SSIZE_MAX || ^ magent.c:729:17: note: each undeclared identifier is reported only once for each function it appears in make: *** [magent.o] Error 1
# 解决办法 [root@srv1 magent]# vim ketama.h # 在头部加入以下代码 #ifndef SSIZE_MAX #define SSIZE_MAX 32767 #endif
# 错误2: 如果出现以下错误 gcc: error: /usr/lib64/libevent.a: No such file or directory gcc: error: /usr/lib64/libm.a: No such file or directory make: *** [magent] Error 1
# 解决办法 [root@srv1 magent]# ln -s /usr/lib64/libm.so /usr/lib64/libm.a [root@srv1 magent]# ln -s /usr/lib64/libevent.so /usr/lib64/libevent.a
# 编译成功后,复制已经编译好的magent命令 [root@srv1 magent]# cp magent /usr/bin/
[root@srv1 magent]# scp magent root@srv3.1000y.cloud:/usr/bin/ [root@srv1 magent]# scp magent root@srv4.1000y.cloud:/usr/bin/
[root@srv1 magent]# cd [root@srv1 ~]#
5) 启动magent # magent命令参数说明 -u: 指定运行用户 -n: 最大的连接数,默认为4096 -l: 小写L,magent监听的ip地址 -p: magent监听的端口 -s: 设置memcached主缓存的ip地址和端口 -b: 设置memcached备缓存的ip地址和端口
[root@srv1 ~]# magent -u root -n 51200 -l 192.168.10.11 -p 12000 -s 192.168.10.13:11211 -b 192.168.10.14:11211
6) 测试magent [root@srv1 ~]# yum install telnet -y
[root@srv1 ~]# telnet 192.168.10.11 12000 Trying 192.168.10.11... Connected to 192.168.10.11. Escape character is '^]'.
set test_key 0 0 10 test_value STORED
get test_key VALUE test_key 0 10 test_value END
quit Connection closed by foreign host.
7) 测试memcached两个节点是否存在test_key (1) srv3节点测试操作 [root@srv3 ~]# yum install telnet -y [root@srv3 ~]# telnet localhost 11211 Trying ::1... telnet: connect to address ::1: Connection refused Trying 127.0.0.1... Connected to localhost. Escape character is '^]'.
get test_key VALUE test_key 0 10 test_value END
quit Connection closed by foreign host.
(2) srv4节点测试操作 [root@srv4 ~]# yum install telnet -y [root@srv4 ~]# telnet localhost 11211 Trying ::1... telnet: connect to address ::1: Connection refused Trying 127.0.0.1... Connected to localhost. Escape character is '^]'.
get test_key VALUE test_key 0 10 test_value END
quit Connection closed by foreign host.
8) 关闭srv3的memcached服务,并测试magent节点可以继续读取到test_key [root@srv3 ~]# systemctl stop memcached
[root@srv1 ~]# telnet 192.168.10.11 12000 Trying 192.168.10.11... Connected to 192.168.10.11. Escape character is '^]'.
get test_key VALUE test_key 0 10 test_value END
quit Connection closed by foreign host.
# 当再次开启srv3的memcached时,如果出现取不到前面设置的所有值,因为重启以后,主节点数据全部清空,虽然备份节点有数据,但是主节点在备份节点不会生效。即主节点无法同步备份节点的数据
6. KeepAlived+Magent
1) 拓扑
                      +----------+
                      |  client  |
                      +-----+----+
                        eth1|192.168.10.15/24
                            |
       |-----------VIP:192.168.10.250 ------------|
+------+------+             |             +-------+-----+
| Keepalived1 |192.168.10.11|192.168.10.12| KeepAlived2 |
|    magent   +-------------+-------------+    magent   |
|     srv1    |             |             |     srv2    |
+-------------+eth0         |         eth0+-------------+
                            |
+------------+              |            +------------+
|  Backend1  |192.168.10.13|192.168.10.14|  Backend2  |
|  memcached +-------------+-------------+  memcached |
|    srv3    |                           |    srv4    |
+------------+eth0                   eth0+------------+
2) 前提条件 1. 两个节点[srv1/srv2]均安装magent及keepalived 2. 两个节点[srv3/srv4]均安装memcached
3) 为srv2安装magent [root@srv1 ~]# scp /usr/bin/magent root@192.168.10.12:/usr/bin/magent
4) 为srv1及srv2安装keepalived [root@srv1 ~]# yum install keepalived procps-ng -y [root@srv2 ~]# yum install keepalived procps-ng -y
5) 配置srv1的keepalived [root@srv1 ~]# mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@srv1 ~]# vim /etc/keepalived/keepalived.conf global_defs { router_id srv1.1000y.cloud }
vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 50 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.10.250 } # 设定当前节点为Master时,所执行的脚本 notify_master "/usr/bin/magent-start.sh" # 设定当前节点为Backup时,所执行的脚本 notify_backup "/usr/bin/magent-stop.sh" # 设定当前节点为失败时,所执行的脚本 notify_fault "/usr/bin/magent-stop.sh" # 如果无论什么状态都想执行指定脚本可按以下方式操作 # notify "$PATH/xxx.sh" }

[root@srv1 ~]# vim /usr/bin/magent-start.sh #! /bin/bash magent -u root -n 51200 -l 192.168.10.222 -p 12000 -s 192.168.10.13:11211 -b 192.168.10.14:11211
[root@srv1 ~]# vim /usr/bin/magent-stop.sh #! /bin/bash pkill magent
[root@srv2 ~]# chmod 755 /usr/bin/magent-start.sh [root@srv2 ~]# chmod 755 /usr/bin/magent-stop.sh
[root@srv1 ~]# systemctl enable --now keepalived
6) 配置srv2的keepalived [root@srv2 ~]# mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@srv2 ~]# vim /etc/keepalived/keepalived.conf global_defs { router_id srv2.1000y.cloud }
vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 50 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.10.250 } # 设定当前节点为Master时,所执行的脚本 notify_master "/usr/bin/magent-start.sh" # 设定当前节点为Backup时,所执行的脚本 notify_backup "/usr/bin/magent-stop.sh" # 设定当前节点为失败时,所执行的脚本 notify_fault "/usr/bin/magent-stop.sh" }

[root@srv2 ~]# vim /usr/bin/magent-start.sh #! /bin/bash magent -u root -n 51200 -l 192.168.10.222 -p 12000 -s 192.168.10.13:11211 -b 192.168.10.14:11211
[root@srv2 ~]# vim /usr/bin/magent-stop.sh #! /bin/bash pkill magent
[root@srv2 ~]# chmod 755 /usr/bin/magent-start.sh [root@srv2 ~]# chmod 755 /usr/bin/magent-stop.sh
[root@srv2 ~]# systemctl enable --now keepalived
7) 测试keepalived (1) 客户端测试 [root@client ~]# telnet 192.168.10.222 12000 Trying 192.168.10.222... Connected to 192.168.10.222. Escape character is '^]'.
get test_key END
set test_key 0 0 10 test_value STORED
get test_key VALUE test_key 0 10 test_value END
quit Connection closed by foreign host.
(2) 停止srv1的keepalived服务[模拟节点故障] [root@srv1 ~]# systemctl stop keepalived
(3) 客户端测试HA机制生效 [root@client ~]# telnet 192.168.10.222 12000 Trying 192.168.10.222... Connected to 192.168.10.222. Escape character is '^]'.
get test_key END
set test_key 0 0 10 test_value STORED
get test_key VALUE test_key 0 10 test_value END
quit Connection closed by foreign host.

 

如对您有帮助,请随缘打个赏。^-^

gold