近期因为生产环境需要调整memcached的缓存模式,之前也考虑换redis,因为可能会涉及到应用程序的调整,所以暂时先考虑上memcached repcached模式。
主节点:10.10.10.164 CentOS6.7_x64
从节点:10.10.10.165 CentOS6.7_x64
1)安装依赖关系(时间服务器也要检查下)
[[email protected] src]# yum -y install install g++ make libevent-devel
2)配置memcached(主从节点,同样的操作)
[[email protected] ~]# cd /usr/local/src [[email protected] src]# tar -xf memcached-1.4.24.tar.gz [[email protected] memcached-1.4.24]# cd .. [[email protected] src]# cd memcached-1.4.24 [[email protected] memcached-1.4.24]# make [[email protected] memcached-1.4.24]# make install
3)配置memcached-repcached(主从节点,同样的操作)
上传memcached-repached包到/usr/local/src下
[[email protected] src]# tar xf memcached-1.2.8-repcached-2.2.tar.gz [[email protected] src]# cd memcached-1.2.8-repcached-2.2 [[email protected] memcached-1.2.8-repcached-2.2]# ./configure --enable-replication [[email protected] memcached-1.2.8-repcached-2.2]# make [[email protected] memcached-1.2.8-repcached-2.2]# make install
4)启动主节点
[[email protected] ~]# memcached -v -d -p 11211 -l 10.10.10.164 -u root -P /tmp/memcached1.pid [[email protected] ~]# replication: listen 说明:accept为启动正常的状态
5)启动从节点
[[email protected] ~]# memcached -v -d -p 11211 -l 10.10.10.165 -u root -x 10.10.10.164 -P /tmp/memcached1.pid [[email protected] ~]# replication: connect (peer=10.10.10.164:11212) replication: marugoto copying replication: start
6)telnet进行验证
[[email protected] ~]# telnet 10.10.10.164 11211 Trying 10.10.10.164... Connected to 10.10.10.164. Escape character is ‘^]‘. set userId 0 0 5 12345 STORED [[email protected] ~]# telnet 10.10.10.164 11211 Trying 10.10.10.164... Connected to 10.10.10.164. Escape character is ‘^]‘. get userId VALUE userId 0 5 12345 END [[email protected] src]# replication: connect (peer=10.10.10.164:11212) replication: marugoto copying replication: start replication: close replication: listen replication: accept replication: marugoto start replication: marugoto 1 replication: marugoto owari replication: close replication: listen 说明:以上为连接状态 [[email protected] ~]# telnet 10.10.10.165 11211 Trying 10.10.10.165... Connected to 10.10.10.165. Escape character is ‘^]‘. get userId VALUE userId 0 5 12345 END 说明:根据上面的输出结果,可以判断userId的键值为12345
7)模拟故障(如主memcached服务挂掉了)
a、检查从节点上的,memcached储值状态
[[email protected] ~]# telnet 10.10.10.165 11211 Trying 10.10.10.165... Connected to 10.10.10.165. Escape character is ‘^]‘. get userId VALUE userId 0 5 12345 END 说明:从节点的键值仍旧存在
b、在从节点上添加新键值
[[email protected] ~]# telnet 10.10.10.165 11211 Trying 10.10.10.165... Connected to 10.10.10.165. Escape character is ‘^]‘. get userId VALUE userId 0 5 12345 END set userId2 0 0 6 123456 STORED get userId2 VALUE userId2 0 6 123456 END 说明:添加userId2的新键值,userId2的值为123456
c、恢复主有的节点
[[email protected] ~]# memcached -v -d -p 11211 -l 10.10.10.164 -u root -x 10.10.10.165 -P /tmp/memcached.pid [[email protected] ~]# replication: connect (peer=10.10.10.165:11212) replication: marugoto copying replication: start [[email protected] ~]# telnet 10.10.10.165 11211 Trying 10.10.10.165... Connected to 10.10.10.165. Escape character is ‘^]‘. get userId VALUE userId 0 5 12345 END get userId2 VALUE userId2 0 6 123456 END # 然后我们在此节点上添加一个新的键值userId3 set userId3 0 0 7 7654321 STORED get userId3 VALUE userId3 0 7 7654321 END
d、在另外一台节点上进行验证
[[email protected] ~]# telnet 10.10.10.165 11211 Trying 10.10.10.165... Connected to 10.10.10.165. Escape character is ‘^]‘. get userId VALUE userId 0 5 12345 END set userId2 0 0 6 123456 STORED get userId2 VALUE userId2 0 6 123456 END get userId3 VALUE userId3 0 7 7654321 END 说明:新添加的键值在此节点上,仍旧存在
e、在10.10.10.165上,设置新键值wanwan
set wanwan 0 0 8 wan08wan STORED get wanwan VALUE wanwan 0 8 wan08wan END
f、在10.10.10.164上进行查看
[[email protected] ~]# telnet 10.10.10.164 11211 Trying 10.10.10.164... Connected to 10.10.10.164. Escape character is ‘^]‘. get wanwan VALUE wanwan 0 8 wan08wan END
8)memcached小结:
a、Repcached 的 Memcached 主从,主从之间可以相互读写 b、由于memcached的主/从没有抢占功能,因此主恢复之后,只能作为现有主节点的从节点
时间: 2024-10-15 19:15:58