主从复制
主从复制
概念
所谓的主从复制,就是主机数据更新后根据陪着和策略。自动同步到备机的master/slave
机制。master
以写为主,slave
以读为主。
可以达到:读写分离,容灾恢复
原理
Slave启动成功连接到master后会发送一个sync命令
Master接到命令启动后台的存盘进程,同时收集所有接收到的用修改数据集命令, 在后台进程执行完毕之后,master将传送整个数据文件到slave,以完成一次完全同步
全量复制:而slave服务在接收到数据库文件数据后,将其存盘并加载到内存中。
增量复制:Master继续将新的所有收集到的修改命令依次传给slave,完成同步
但是只要是重新连接naster,一次完全同步(全量复制)将被自动执行
缺点
由于所有的写操作都是先在Master上操作,然后同步更新到Slave上,所以从Master同步到Slave机器有一定的延迟,当系统很繁忙的时候,延迟问题会更加严重,Slave机器数量的增加也会使这个问题更加严重。
案例
配从库,不配主库
从库配置
replicaof <masterip> <masterport>
- 在旧版的redis中,命令是
slaveof
- 每次与master断开之后,都需要重新连接,除非将配置写到配置文件
- 在旧版的redis中,命令是
info replication
:查看当前主从复制配置信息
常用三招:
- 一主二仆
- 薪火相传
- 反客为主
在同一机器上模拟多个redis实例,可以修改配置文件,生成不同的redis服务
- 拷贝多个
redis.conf
文件,如redis6379.conf
、redis6380.conf
、redis6381.conf
- 开启
daemonize yes
,指定启动方式为后台守护进程 - pid文件名字, 生成不同的pid文件
- 指定不同的端口:6379、6380、6381
- 指定不同的logfile,生成不同的日志文件
- 指定不同的logfile,生成不同的
dump.rdb
文件
一主二仆
查询发现三个redis服务都是master主机,在6379主机上设置
k1
之后,其余主机并没有数据
6379 | 6380 | 6381 |
---|---|---|
127.0.0.1:6379> info replication ## Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:d85526558c93bba35bb845647c291f6f445d247a 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:6380> info replication ## Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:a480db46cee6f08627069f4888fb0b0eeeca743a 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:6381> info replication ## Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:890f56ca20f0562ebf75129c2f9b80746abf9a69 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> keys * (empty list or set) 127.0.0.1:6379> set k1 1 OK | 127.0.0.1:6380> keys * (empty list or set) | 127.0.0.1:6381> keys * (empty list or set) |
在6380、6381设置同步主机,查询发现6379为主机,6380、6381为从机
6379 | 6380 | 6381 |
---|---|---|
127.0.0.1:6380> replicaof 127.0.0.1 6379 OK | 127.0.0.1:6381> replicaof 127.0.0.1 6379 OK | |
127.0.0.1:6379> info replication ## Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=42,lag=0 slave1:ip=127.0.0.1,port=6381,state=online,offset=42,lag=0 master_failover_state:no-failover master_replid:c611d20cbae3b4b6e38a1886e1a71f95d488fd58 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:42 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:42 | 127.0.0.1:6380> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:4 master_sync_in_progress:0 slave_read_repl_offset:56 slave_repl_offset:56 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:c611d20cbae3b4b6e38a1886e1a71f95d488fd58 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:56 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:56 | 127.0.0.1:6381> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:8 master_sync_in_progress:0 slave_read_repl_offset:56 slave_repl_offset:56 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:c611d20cbae3b4b6e38a1886e1a71f95d488fd58 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:56 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:15 repl_backlog_histlen:42 |
设置主从同步之后,在主机6379存入
k2
之后,发现6380、6381也同步数据啦,并且 在设置同步之前的主机数据也同步到从机,说明主从同步是全量复制。
6379 | 6380 | 6381 |
---|---|---|
127.0.0.1:6379> set k2 2 OK | 127.0.0.1:6380> get k2 "2" | 127.0.0.1:6381> get k2 "2" |
127.0.0.1:6380> get k1 "1" | 127.0.0.1:6381> get k1 "1" |
在从机上只能读,不能写
6379 | 6380 | 6381 |
---|---|---|
127.0.0.1:6380> set k3 3 (error) READONLY You can't write against a read only replica. | 127.0.0.1:6381> set k3 3 (error) READONLY You can't write against a read only replica. |
在主机挂掉之后,从机不做任何操作,原地待命,只要主机重新启动,将会继续进行主从同步
6379 | 6380 | 6381 |
---|---|---|
127.0.0.1:6379> shutdown not connected> | 127.0.0.1:6380> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_read_repl_offset:1017 slave_repl_offset:1017 master_link_down_since_seconds:13 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:c611d20cbae3b4b6e38a1886e1a71f95d488fd58 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:1017 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:1017 | 127.0.0.1:6381> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_read_repl_offset:1017 slave_repl_offset:1017 master_link_down_since_seconds:19 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:c611d20cbae3b4b6e38a1886e1a71f95d488fd58 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:1017 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:15 repl_backlog_histlen:1003 |
重新启动6379redis服务 | ||
127.0.0.1:6379> info replication ## Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6381,state=online,offset=1092,lag=1 slave1:ip=127.0.0.1,port=6380,state=online,offset=1092,lag=1 master_failover_state:no-failover master_replid:d527d415d36986a6605ebaefb4f87cf264337954 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:1092 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:1092 | 127.0.0.1:6380> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:4 master_sync_in_progress:0 slave_read_repl_offset:56 slave_repl_offset:56 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:c611d20cbae3b4b6e38a1886e1a71f95d488fd58 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:56 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:56 | 127.0.0.1:6381> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:8 master_sync_in_progress:0 slave_read_repl_offset:56 slave_repl_offset:56 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:c611d20cbae3b4b6e38a1886e1a71f95d488fd58 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:56 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:15 repl_backlog_histlen:42 |
在从机挂掉之后,其余主从之间同步数据不受影响,当从机重新启动后,此时又会变为主机,如果还想同步,要么重新使用命令配置;要么修改配置文件,永久生效
6379 | 6380 | 6381 |
---|---|---|
127.0.0.1:6380> SHUTDOWN not connected> | ||
127.0.0.1:6379> set k3 3 OK | 127.0.0.1:6381> get k3 "3" | |
启动6380redis服务 | ||
127.0.0.1:6380> info replication ## Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:0ed1fc0f18352946fb5116ddc1efee30d0c8896c 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:6380> replicaof 127.0.0.1 6379 OK | ||
127.0.0.1:6379> set k4 4 OK | 127.0.0.1:6380> get k4 "4" | 127.0.0.1:6381> get k4 "4" |
薪火相传
上一个Slave可以是下一个slave的Master,Slave同样可以接收其他 slaves的连接和同步请求,那么该slave作为了链条中下一个的master, 可以有效减轻master的写压力
中途变更转向:会清除之前的数据,重新建立拷贝最新的:
replicaof 新主库IP 新主库端口
6379 | 6380 | 6381 |
---|---|---|
127.0.0.1:6379> info replication ## Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:d527d415d36986a6605ebaefb4f87cf264337954 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:53260 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:53260 | 127.0.0.1:6380> info replication ## Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:97a3aef08514da0dc26349df22e9f7afd826e5ee 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:6381> info replication ## Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:61b186d4b048bda5b1fc55c6cd3bd49e2c06f179 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:6380> replicaof 127.0.0.1 6379 OK | 127.0.0.1:6381> replicaof 127.0.0.1 6380 OK | |
127.0.0.1:6380> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:5 master_sync_in_progress:0 slave_read_repl_offset:53288 slave_repl_offset:53288 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:1 slave0:ip=127.0.0.1,port=6381,state=online,offset=53288,lag=1 master_failover_state:no-failover master_replid:d527d415d36986a6605ebaefb4f87cf264337954 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:53288 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:53261 repl_backlog_histlen:28 | 127.0.0.1:6381> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6380 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_read_repl_offset:53288 slave_repl_offset:53288 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:d527d415d36986a6605ebaefb4f87cf264337954 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:53288 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:53275 repl_backlog_histlen:14 | |
127.0.0.1:6379> set k5 5 OK | 127.0.0.1:6380> get k5 "5" | 127.0.0.1:6381> get k5 "5" |
反客为主
salveof no one
:使当前数据库停止与其他数据库同步,转为主库,即使原先的主库重新启动,也和现在的主从体系无关系
6379 | 6380 | 6381 |
---|---|---|
127.0.0.1:6380> replicaof 127.0.0.1 6379 OK | 127.0.0.1:6381> replicaof 127.0.0.1 6379 OK | |
127.0.0.1:6379> info replication ## Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=54389,lag=0 slave1:ip=127.0.0.1,port=6381,state=online,offset=54389,lag=1 master_failover_state:no-failover master_replid:d527d415d36986a6605ebaefb4f87cf264337954 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:54389 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:54389 | 127.0.0.1:6380> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_read_repl_offset:54403 slave_repl_offset:54403 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:d527d415d36986a6605ebaefb4f87cf264337954 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:54403 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:54348 repl_backlog_histlen:56 | 127.0.0.1:6381> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:6 master_sync_in_progress:0 slave_read_repl_offset:54403 slave_repl_offset:54403 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:d527d415d36986a6605ebaefb4f87cf264337954 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:54403 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:54348 repl_backlog_histlen:56 |
主机6379服务挂掉 | 127.0.0.1:6380> replicaof no one OK | 127.0.0.1:6381> replicaof 127.0.0.1 6380 OK |
127.0.0.1:6380> info replication ## Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6381,state=online,offset=54515,lag=0 master_failover_state:no-failover master_replid:85761bb4699d91aacdb2359d282605906e908dbb master_replid2:d527d415d36986a6605ebaefb4f87cf264337954 master_repl_offset:54515 second_repl_offset:54502 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:54348 repl_backlog_histlen:168 | 127.0.0.1:6381> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6380 master_link_status:up master_last_io_seconds_ago:3 master_sync_in_progress:0 slave_read_repl_offset:54501 slave_repl_offset:54501 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:85761bb4699d91aacdb2359d282605906e908dbb master_replid2:d527d415d36986a6605ebaefb4f87cf264337954 master_repl_offset:54501 second_repl_offset:54502 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:54348 repl_backlog_histlen:154 | |
启动6379redis服务 | ||
127.0.0.1:6380> info replication ## Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6381,state=online,offset=54515,lag=0 master_failover_state:no-failover master_replid:85761bb4699d91aacdb2359d282605906e908dbb master_replid2:d527d415d36986a6605ebaefb4f87cf264337954 master_repl_offset:54515 second_repl_offset:54502 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:54348 repl_backlog_histlen:168 | 127.0.0.1:6381> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6380 master_link_status:up master_last_io_seconds_ago:3 master_sync_in_progress:0 slave_read_repl_offset:54501 slave_repl_offset:54501 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:85761bb4699d91aacdb2359d282605906e908dbb master_replid2:d527d415d36986a6605ebaefb4f87cf264337954 master_repl_offset:54501 second_repl_offset:54502 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:54348 repl_backlog_histlen:154 |
哨兵模式
概念
反客为主的自动版, 能够后台监控主机是否故障,如果故障了根据投票数自动将从库转换为主库
一组sentinel能同时监控多个Master
案例
调整结构,保证一主二仆,即 Master:6379,Slave:6380、6381
6379 | 6380 | 6381 |
---|---|---|
127.0.0.1:6380> replicaof 127.0.0.1 6379 OK | 127.0.0.1:6381> replicaof 127.0.0.1 6379 OK | |
127.0.0.1:6379> info replication ## Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=54389,lag=0 slave1:ip=127.0.0.1,port=6381,state=online,offset=54389,lag=1 master_failover_state:no-failover master_replid:d527d415d36986a6605ebaefb4f87cf264337954 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:54389 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:54389 | 127.0.0.1:6380> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_read_repl_offset:54403 slave_repl_offset:54403 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:d527d415d36986a6605ebaefb4f87cf264337954 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:54403 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:54348 repl_backlog_histlen:56 | 127.0.0.1:6381> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:6 master_sync_in_progress:0 slave_read_repl_offset:54403 slave_repl_offset:54403 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:d527d415d36986a6605ebaefb4f87cf264337954 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:54403 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:54348 repl_backlog_histlen:56 |
新建配置文件:
sentinel.conf
,名字决不能错,配置哨兵
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
# master-name 自定义的主机名
# ip 主机ip
# redis-port 主机redis端口号
# quorum 法定人数
# 告诉哨兵监视master,并考虑在主机挂掉状态下,只有在至少quorum个slave同意的情况下。选择让被选中的slave成为master
sentinel monitor host6379 127.0.0.1 6379 1
启动哨兵
# redis-sentinel /path/sentinel.conf,注意,文件的路径问题
Ξ local/etc → /usr/local/opt/redis/bin/redis-sentinel /usr/local/etc/sentinel.conf
18623:X 10 Apr 2022 11:49:23.098 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18623:X 10 Apr 2022 11:49:23.098 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=18623, just started
18623:X 10 Apr 2022 11:49:23.098 # Configuration loaded
18623:X 10 Apr 2022 11:49:23.099 * Increased maximum number of open files to 10032 (it was originally set to 256).
18623:X 10 Apr 2022 11:49:23.099 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 18623
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
18623:X 10 Apr 2022 11:49:23.101 # Sentinel ID is 673a6620f7e7ec1c2ead4c3db0a654084498d454
18623:X 10 Apr 2022 11:49:23.101 # +monitor master host6379 127.0.0.1 6379 quorum 1
18623:X 10 Apr 2022 11:49:23.102 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
18623:X 10 Apr 2022 11:49:23.102 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
主机6379挂掉
6379 | 6380 | 6381 |
---|---|---|
127.0.0.1:6379> SHUTDOWN not connected> exit | ||
127.0.0.1:6380> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_read_repl_offset:27596 slave_repl_offset:27596 master_link_down_since_seconds:28 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:c1553a88ed74415a327718d75088a042fe901dba master_replid2:0000000000000000000000000000000000000000 master_repl_offset:27596 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:27596 | 127.0.0.1:6381> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_read_repl_offset:36598 slave_repl_offset:36598 master_link_down_since_seconds:162 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:14b778500b7f43bc27d7c2a29279b5846c653ca8 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:36598 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:36598 |
sentinel会自动切换其他从机为主机
18623:X 10 Apr 2022 11:56:22.184 # +sdown master host6379 127.0.0.1 6379
18623:X 10 Apr 2022 11:56:22.184 # +odown master host6379 127.0.0.1 6379 #quorum 1/1
18623:X 10 Apr 2022 11:56:22.184 # +new-epoch 1
18623:X 10 Apr 2022 11:56:22.184 # +try-failover master host6379 127.0.0.1 6379
18623:X 10 Apr 2022 11:56:22.186 # +vote-for-leader 673a6620f7e7ec1c2ead4c3db0a654084498d454 1
18623:X 10 Apr 2022 11:56:22.186 # +elected-leader master host6379 127.0.0.1 6379
18623:X 10 Apr 2022 11:56:22.186 # +failover-state-select-slave master host6379 127.0.0.1 6379
18623:X 10 Apr 2022 11:56:22.271 # +selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
18623:X 10 Apr 2022 11:56:22.271 * +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
18623:X 10 Apr 2022 11:56:22.354 * +failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
18623:X 10 Apr 2022 11:56:22.594 # +promoted-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
18623:X 10 Apr 2022 11:56:22.594 # +failover-state-reconf-slaves master host6379 127.0.0.1 6379
18623:X 10 Apr 2022 11:56:22.659 * +slave-reconf-sent slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
18623:X 10 Apr 2022 11:56:23.657 * +slave-reconf-inprog slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
18623:X 10 Apr 2022 11:56:23.657 * +slave-reconf-done slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
18623:X 10 Apr 2022 11:56:23.727 # +failover-end master host6379 127.0.0.1 6379
18623:X 10 Apr 2022 11:56:23.727 # +switch-master host6379 127.0.0.1 6379 127.0.0.1 6380
18623:X 10 Apr 2022 11:56:23.727 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6380
18623:X 10 Apr 2022 11:56:23.727 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6380
18623:X 10 Apr 2022 11:56:53.738 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6380
此时,查看其余redis服务状态
6380 | 6381 |
---|---|
127.0.0.1:6380> info replication ## Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6381,state=online,offset=30748,lag=0 master_failover_state:no-failover master_replid:418e39999e908c633bf8103c2c4a6c8a5940274e master_replid2:c1553a88ed74415a327718d75088a042fe901dba master_repl_offset:30748 second_repl_offset:27597 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:30748 | 127.0.0.1:6381> info replication ## Replication role:slave master_host:127.0.0.1 master_port:6380 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_read_repl_offset:29789 slave_repl_offset:29789 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:418e39999e908c633bf8103c2c4a6c8a5940274e master_replid2:c1553a88ed74415a327718d75088a042fe901dba master_repl_offset:29789 second_repl_offset:27597 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:29789 |
此时,6379服务重新启动,sentinel会将其切换为slave
18623:X 10 Apr 2022 12:02:51.333 # -sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6380
18623:X 10 Apr 2022 12:03:01.319 * +convert-to-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6380
18623:X 10 Apr 2022 12:04:30.110 # +tilt #tilt mode entered
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:55628
slave_repl_offset:55628
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:418e39999e908c633bf8103c2c4a6c8a5940274e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:55628
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:54248
repl_backlog_histlen:1381