Redis 主从复制

2.2:Redis 主从复制

2.2.1:拓扑图

在这里插入图片描述

Master:192.168.1.201

Slave:192.168.1.202

2.2.2:临时实现主从复制

通过命令行实现的主从复制关系,在重启 Redis 后会失效;

Master 写入测试数据

shell 脚本:

[root@redis1 ~]# vim redis.sh 
#!/bin/bash
NUM=`seq 1 1000`
PASS='123456'
for i in ${NUM};do
  redis-cli -h 127.0.0.1 -a ${PASS} set key-${i} value-${i} &>/dev/null
done

echo "数据写入完成"

执行脚本,写入数据:

[root@redis1 ~]# bash redis.sh        
数据写入完成

查看 Master 数据:

127.0.0.1:6379> GET key-111
"value-111"
127.0.0.1:6379> GET key-999
"value-999"
Slave 命令行配置

指定 Master 为 192.168.1.201:

127.0.0.1:6379> SLAVEOF 192.168.1.201 6379

设置 Master 连接的认证密码:

127.0.0.1:6379> CONFIG SET masterauth 123456
验证主从复制

查看 Master 日志:

[root@redis1 ~]# tail -f /apps/redis/logs/redis_6379.log 
……
6538:M 04 Jan 11:04:25.156 * Slave 192.168.1.202:6379 asks for synchronization
6538:M 04 Jan 11:04:25.156 * Full resync requested by slave 192.168.1.202:6379
6538:M 04 Jan 11:04:26.684 * Starting BGSAVE for SYNC with target: slaves sockets
6538:M 04 Jan 11:04:26.686 * Background RDB transfer started by pid 7574
7574:C 04 Jan 11:04:26.694 * RDB: 6 MB of memory used by copy-on-write
6538:M 04 Jan 11:04:26.789 * Background RDB transfer terminated with success
6538:M 04 Jan 11:04:26.789 # Slave 192.168.1.202:6379 correctly received the streamed RDB file.
6538:M 04 Jan 11:04:26.789 * Streamed RDB transfer with slave 192.168.1.202:6379 succeeded (socket). Waiting for REPLCONF ACK from slave to enable streaming
6538:M 04 Jan 11:04:27.194 * Synchronization with slave 192.168.1.202:6379 succeeded

查看 Slave 日志:

[root@redis2 ~]# tail -f /apps/redis/logs/redis_6379.log
……
2511:S 04 Jan 11:04:25.146 * Connecting to MASTER 192.168.1.201:6379
2511:S 04 Jan 11:04:25.146 * MASTER <-> SLAVE sync started
2511:S 04 Jan 11:04:25.147 * Non blocking connect for SYNC fired the event.
2511:S 04 Jan 11:04:25.150 * Master replied to PING, replication can continue...
2511:S 04 Jan 11:04:25.156 * Partial resynchronization not possible (no cached master)
2511:S 04 Jan 11:04:26.685 * Full resync from master: d8b79f11217860aeb81a74da06408af954e306f4:0
2511:S 04 Jan 11:04:26.692 * MASTER <-> SLAVE sync: receiving streamed RDB from master
2511:S 04 Jan 11:04:26.696 * MASTER <-> SLAVE sync: Flushing old data
2511:S 04 Jan 11:04:26.696 * MASTER <-> SLAVE sync: Loading DB in memory
2511:S 04 Jan 11:04:26.699 * MASTER <-> SLAVE sync: Finished with success
2511:S 04 Jan 11:04:26.701 * Background append only file rewriting started by pid 2519
2511:S 04 Jan 11:04:26.771 * AOF rewrite child asks to stop sending diffs.
2519:C 04 Jan 11:04:26.771 * Parent agreed to stop sending diffs. Finalizing AOF...
2519:C 04 Jan 11:04:26.771 * Concatenating 0.00 MB of AOF diff received from parent.
2519:C 04 Jan 11:04:26.771 * SYNC append only file rewrite performed
2519:C 04 Jan 11:04:26.772 * AOF rewrite: 6 MB of memory used by copy-on-write
2511:S 04 Jan 11:04:26.784 * Background AOF rewrite terminated with success
2511:S 04 Jan 11:04:26.784 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
2511:S 04 Jan 11:04:26.784 * Background AOF rewrite finished successfully

查看 Slave 中同步过来的数据:

127.0.0.1:6379> GET key-1000
"value-1000"

Master 的 Replication 信息:

127.0.0.1:6379> INFO Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.202,port=6379,state=online,offset=420,lag=1
master_replid:d8b79f11217860aeb81a74da06408af954e306f4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:420
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:104857600
repl_backlog_first_byte_offset:1
repl_backlog_histlen:420

Slave 的 Replication 信息:

127.0.0.1:6379> INFO Replication
# Replication
role:slave
master_host:192.168.1.201
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:448
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:d8b79f11217860aeb81a74da06408af954e306f4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:448
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:104857600
repl_backlog_first_byte_offset:1
repl_backlog_histlen:448

2.2.3:永久实现主从复制

一般采用编辑配置文件的方式来保存主从复制关系,再次启动 Redis 时,主从复制关系仍然存在;

编辑 Slave 配置文件

设置 Master 为 192.168.1.201,并配置 Master 连接密码:

[root@redis2 ~]# vim /apps/redis/etc/redis.conf
slaveof 192.168.1.201 6379
masterauth 123456
重启 Slave 的 Redis 进程
[root@redis2 ~]# systemctl restart redis
验证主从复制

查看主从复制关系:

master_link_status:up,在 Slave 重启后主从复制关系仍然存在;

127.0.0.1:6379> INFO Replication
# Replication
role:slave
master_host:192.168.1.201
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:728
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:d8b79f11217860aeb81a74da06408af954e306f4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:728
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:104857600
repl_backlog_first_byte_offset:631
repl_backlog_histlen:98

Master 添加数据:

127.0.0.1:6379> SET key-1001 value-1001
OK
127.0.0.1:6379> SET key-test value-test
OK

Slave 验证同步数据:

127.0.0.1:6379> GET key-1001
"value-1001"
127.0.0.1:6379> GET key-test
"value-test"

2.2.4:模拟 Master 不可用

验证 Slave 的只读状态

配置文件中设置了 slave-read-only yes,所以 Slave 节点为只读:

127.0.0.1:6379> SET foo bar
(error) READONLY You can't write against a read only slave.
停止 Master 的 Redis 进程

停止 Redis 进程:

[root@redis1 ~]# systemctl stop redis

Slave 日志中显示已失去与 Master 的连接:

2590:S 04 Jan 11:20:40.223 # Connection with master lost.
2590:S 04 Jan 11:20:40.223 * Caching the disconnected master state.
2590:S 04 Jan 11:20:40.314 * Connecting to MASTER 192.168.1.201:6379
2590:S 04 Jan 11:20:40.314 * MASTER <-> SLAVE sync started
2590:S 04 Jan 11:20:40.315 # Error condition on socket for SYNC: Connection refused

Slave 的 Replication 状态:

master_link_status:down,主从复制关系已 down;

127.0.0.1:6379> INFO Replication
# Replication
role:slave
master_host:192.168.1.201
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1413
master_link_down_since_seconds:103
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:d8b79f11217860aeb81a74da06408af954e306f4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1413
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:104857600
repl_backlog_first_byte_offset:631
repl_backlog_histlen:783
手动将 Slave 提升为 Master

手动取消之前的主从复制关系:

127.0.0.1:6379> SLAVEOF no one
OK

Slave 日志:

2590:M 04 Jan 11:23:12.376 # Setting secondary replication ID to d8b79f11217860aeb81a74da06408af954e306f4, valid up to offset: 1414. New replication ID is 37c7c687adda7c9d53578ff8aa16e403413c8a8f
2590:M 04 Jan 11:23:12.376 * Discarding previously cached master state.
2590:M 04 Jan 11:23:12.376 * MASTER MODE enabled (user request from 'id=4 addr=127.0.0.1:52112 fd=7 name= age=596 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=slaveof')
验证 Slave 已提升为 Master

查看当前的 Replicaiton 状态:

角色已成为 master,且之前的 master_replid 已成为 master_replid2;

127.0.0.1:6379> INFO Replication
# Replication
role:master
connected_slaves:0
master_replid:37c7c687adda7c9d53578ff8aa16e403413c8a8f
master_replid2:d8b79f11217860aeb81a74da06408af954e306f4
master_repl_offset:1413
second_repl_offset:1414
repl_backlog_active:1
repl_backlog_size:104857600
repl_backlog_first_byte_offset:631
repl_backlog_histlen:783

验证写入操作:

127.0.0.1:6379> SET foo bar
OK
127.0.0.1:6379> GET foo
"bar"

此时之前的 Slave 已经可以单独对外提供服务了;

2.2.5:将之前的 Master 作为 Slave 添加进来

启动 Redis 并指定 Master

启动 192.168.1.201(原 Master)的 Redis 进程,并设为 192.168.1.202 的 Slave:

[root@redis1 ~]# systemctl start redis
[root@redis1 ~]# redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> SLAVEOF 192.168.1.202 6379
OK
127.0.0.1:6379> CONFIG SET masterauth 123456
OK
验证新的主从复制关系

Master 的 Replication 状态:

127.0.0.1:6379> INFO Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.201,port=6379,state=online,offset=1607,lag=0
master_replid:37c7c687adda7c9d53578ff8aa16e403413c8a8f
master_replid2:d8b79f11217860aeb81a74da06408af954e306f4
master_repl_offset:1607
second_repl_offset:1414
repl_backlog_active:1
repl_backlog_size:104857600
repl_backlog_first_byte_offset:631
repl_backlog_histlen:977

Slave 的 Replication 状态:

127.0.0.1:6379> INFO Replication
# Replication
role:slave
master_host:192.168.1.202
master_port:6379
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_repl_offset:1635
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:37c7c687adda7c9d53578ff8aa16e403413c8a8f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1635
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:104857600
repl_backlog_first_byte_offset:1468
repl_backlog_histlen:168

Master 日志:

2590:M 04 Jan 11:32:15.306 * Slave 192.168.1.201:6379 asks for synchronization
2590:M 04 Jan 11:32:15.306 * Full resync requested by slave 192.168.1.201:6379
2590:M 04 Jan 11:32:16.768 * Starting BGSAVE for SYNC with target: slaves sockets
2590:M 04 Jan 11:32:16.769 * Background RDB transfer started by pid 2615
2615:C 04 Jan 11:32:16.776 * RDB: 6 MB of memory used by copy-on-write
2590:M 04 Jan 11:32:16.877 * Background RDB transfer terminated with success
2590:M 04 Jan 11:32:16.877 # Slave 192.168.1.201:6379 correctly received the streamed RDB file.
2590:M 04 Jan 11:32:16.877 * Streamed RDB transfer with slave 192.168.1.201:6379 succeeded (socket). Waiting for REPLCONF ACK from slave to enable streaming
2590:M 04 Jan 11:32:17.486 * Synchronization with slave 192.168.1.201:6379 succeeded

Slave 日志:

7621:S 04 Jan 11:32:15.302 * Connecting to MASTER 192.168.1.202:6379
7621:S 04 Jan 11:32:15.302 * MASTER <-> SLAVE sync started
7621:S 04 Jan 11:32:15.303 * Non blocking connect for SYNC fired the event.
7621:S 04 Jan 11:32:15.304 * Master replied to PING, replication can continue...
7621:S 04 Jan 11:32:15.306 * Partial resynchronization not possible (no cached master)
7621:S 04 Jan 11:32:16.769 * Full resync from master: 37c7c687adda7c9d53578ff8aa16e403413c8a8f:1467
7621:S 04 Jan 11:32:16.778 * MASTER <-> SLAVE sync: receiving streamed RDB from master
7621:S 04 Jan 11:32:16.782 * MASTER <-> SLAVE sync: Flushing old data
7621:S 04 Jan 11:32:16.783 * MASTER <-> SLAVE sync: Loading DB in memory
7621:S 04 Jan 11:32:16.786 * MASTER <-> SLAVE sync: Finished with success
7621:S 04 Jan 11:32:16.787 * Background append only file rewriting started by pid 7626
7621:S 04 Jan 11:32:17.080 * AOF rewrite child asks to stop sending diffs.
7626:C 04 Jan 11:32:17.081 * Parent agreed to stop sending diffs. Finalizing AOF...
7626:C 04 Jan 11:32:17.081 * Concatenating 0.00 MB of AOF diff received from parent.
7626:C 04 Jan 11:32:17.081 * SYNC append only file rewrite performed
7626:C 04 Jan 11:32:17.082 * AOF rewrite: 6 MB of memory used by copy-on-write
7621:S 04 Jan 11:32:17.158 * Background AOF rewrite terminated with success
7621:S 04 Jan 11:32:17.159 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
7621:S 04 Jan 11:32:17.159 * Background AOF rewrite finished successfully

验证 Slave 数据:

127.0.0.1:6379> GET foo
"bar"
127.0.0.1:6379> GET key-222
"value-222"

Master 的切换会导致 master_replid 发生变化,Slave 之前的 master_replid 就和当前 Master 不一致,从而会引发所有 Slave 的全量同步。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值