准备工作
服务器 ip |
角色 |
192.168.20.1 |
主 |
192.168.20.2 |
从 |
192.168.20.3 |
从 |
-
# docker 安装 redis
docker pull redis
-
# 运行
分别从三台服务器运行 redis 镜像,注意映射不同外端口
1
| docker run -p 6380:6379 --name redis -v /mydata/redis/data/redis.conf:/etc/redis/redis.conf -v /mydata/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes --daemonize no --masterauth #如果主节点设置了密码,请输入主服务密码
|
docker exec -it redis_master /bin/bash
-
# 查看角色
# 主:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 127.0.0.1:6379> auth #你的密码 OK 127.0.0.1:6379> info replication # Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:f28e9097e4c8cd3f67292181be12955909afd88e master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6379>
|
# 从 01:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 127.0.0.1:6379> info replication # Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:48a7e3866afc4b6784ef49353b57fbc979ee2935 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6379>
|
# 从 02:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 127.0.0.1:6379> info replication # Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:de30809041f22f2dd6abc9cb34536f26df97e647 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6379>
|
-
# 在两台从服务器上执行命令 replicaof 192.168.20.1 6379
1 2 3
| 127.0.0.1:6379> replicaof 192.168.20.1 6379 OK 127.0.0.1:6379>
|
-
# 查看主服务信息
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 37
| 127.0.0.1:6379> info replication # Replication role:master # connected_slaves:2 slave0:ip=172.17.0.3,port=6379,state=online,offset=1456,lag=1 slave1:ip=172.17.0.4,port=6379,state=online,offset=1456,lag=0 # master_failover_state:no-failover master_replid:9b8c2ef4539809505fa5bc1dd779c4500298011d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:1456 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:1456 127.0.0.1:6379>
127.0.0.1:6379> info replication # Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:f28e9097e4c8cd3f67292181be12955909afd88e master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6379>
|
-
# 测试:
主服务器上:
1 2 3
| 127.0.0.1:6379> set name zs OK 127.0.0.1:6379>
|
# 从服务器也可以查看到信息
1 2 3 4 5
| 127.0.0.1:6379> replicaof 192.168.20.1 6379 OK 127.0.0.1:6379> get name "zs" 127.0.0.1:6379>
|
# 异常统计:
可能遇到的 BUG:MASTER aborted replication with an error: NOAUTH Authentication required.
场景:在配置主从后发现两个从节点的 info replication 中出现:master_link_status:down
原因:大部分原因是因为主节点配置了密码
解决:在从节点的配置文件中加入 masterauth 你的主节点密码 即可
# 关于我
Brath 是一个热爱技术的 Java 程序猿,公众号「InterviewCoder」定期分享有趣有料的精品原创文章!
非常感谢各位人才能看到这里,原创不易,文章如果有帮助可以关注、点赞、分享或评论,这都是对我的莫大支持!