搭建memcached repcached

本文详细介绍了一种memcached的主从配置方案,包括安装、配置memcached-replication、启动主从节点及验证过程。并通过模拟故障场景,展示了系统的稳定性和数据一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

近期因为生产环境需要调整memcached的缓存模式,之前也考虑换redis,因为可能会涉及到应用程序的调整,所以暂时先考虑上memcached repcached模式。

主节点:10.10.10.164    CentOS6.7_x64

从节点:10.10.10.165    CentOS6.7_x64

1)安装依赖关系(时间服务器也要检查下)

1
[root@tomcat01 src]# yum -y install install g++ make libevent-devel

2)配置memcached(主从节点,同样的操作)

1
2
3
4
5
6
[root@tomcat01 ~]# cd /usr/local/src
[root@tomcat01 src]# tar -xf memcached-1.4.24.tar.gz 
[root@tomcat01 memcached-1.4.24]# cd ..
[root@tomcat01 src]# cd memcached-1.4.24
[root@tomcat01 memcached-1.4.24]# make
[root@tomcat01 memcached-1.4.24]# make install

3)配置memcached-repcached(主从节点,同样的操作)

上传memcached-repached包到/usr/local/src下

1
2
3
4
5
[root@tomcat01 src]# tar xf memcached-1.2.8-repcached-2.2.tar.gz 
[root@tomcat01 src]# cd memcached-1.2.8-repcached-2.2
[root@tomcat01 memcached-1.2.8-repcached-2.2]# ./configure --enable-replication
[root@tomcat01 memcached-1.2.8-repcached-2.2]# make
[root@tomcat01 memcached-1.2.8-repcached-2.2]# make install

4)启动主节点

1
2
3
[root@tomcat01 ~]# memcached -v -d -p 11211 -l 10.10.10.164 -u root -P /tmp/memcached1.pid 
[root@tomcat01 ~]# replication: listen
说明:accept为启动正常的状态

5)启动从节点

1
2
3
4
5
[root@tomcat02 ~]# memcached -v -d -p 11211 -l 10.10.10.165 -u root -x 10.10.10.164 -P
 /tmp/memcached1.pid 
[root@tomcat02 ~]# replication: connect (peer=10.10.10.164:11212)
replication: marugoto copying
replication: start

6)telnet进行验证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[root@tomcat01 ~]# 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
[root@tomcat01 ~]# 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
[root@tomcat02 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
说明:以上为连接状态
[root@tomcat02 ~]# 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储值状态

1
2
3
4
5
6
7
8
9
[root@tomcat02 ~]# 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、在从节点上添加新键值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@tomcat02 ~]# 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、恢复主有的节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@tomcat01 ~]# memcached -v -d -p 11211 -l 10.10.10.164 -u root -x 10.10.10.165 -P 
/tmp/memcached.pid 
[root@tomcat01 ~]# replication: connect (peer=10.10.10.165:11212)
replication: marugoto copying
replication: start
[root@tomcat01 ~]# 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、在另外一台节点上进行验证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@tomcat02 ~]# 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

1
2
3
4
5
6
7
set wanwan 0 0 8
wan08wan
STORED
get wanwan
VALUE wanwan 0 8
wan08wan
END

f、在10.10.10.164上进行查看

1
2
3
4
5
6
7
8
[root@tomcat01 ~]# 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小结:

1
2
a、Repcached 的 Memcached 主从,主从之间可以相互读写
b、由于memcached的主/从没有抢占功能,因此主恢复之后,只能作为现有主节点的从节点









本文转自 冰冻vs西瓜 51CTO博客,原文链接:http://blog.51cto.com/molewan/1956420,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值