Redis(十四) -- Redis配置(三) -- 主从复制

1. 是什么?

主从复制:就是主机数据更新后根据配置和策略,自动同步到备机的master/slaver机制Master以写为主,Slave以读为主

2. 用处:

  • 读写分离,性能扩展
  • 容灾快速恢复

在这里插入图片描述

3. 配置临时从服务器:

在这里插入图片描述
步骤:

  1. 设置三个conf文件,在每个conf文件中include redis.conf主配置文件,端口号、文件名字不一样
    在这里插入图片描述
    在这里插入图片描述
  2. 通过这些配置文件启动redis
    在这里插入图片描述
  3. 查看服务器状态info replication,可以看到每个服务器都是主服务器
    在这里插入图片描述
  4. 设置称为某个服务器的从服务器:slaveof ip port
    设置6380和6381为6379的从服务器
    在这里插入图片描述
  5. 查看6379:
    在这里插入图片描述

4. 配置永久主从服务器

在配置文件中配置:

# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 1) Redis replication is asynchronous, but you can configure a master to
#    stop accepting writes if it appears to be not connected with at least
#    a given number of slaves.
# 2) Redis slaves are able to perform a partial resynchronization with the
#    master if the replication link is lost for a relatively small amount of
#    time. You may want to configure the replication backlog size (see the next
#    sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
#    network partition slaves automatically try to reconnect to masters
#    and resynchronize with them.
#
# slaveof <masterip> <masterport>

5. 从服务器只能读不能写

在这里插入图片描述

6. 一主二从相关问题:

  1. 一个从服务器宕机关机了,之后主服务器进行了写操作,此时从服务器重新连接并设置为主服务器的从服务器;从服务器还有在这段时间内主服务器写入的值吗?
    答案:有,无论什么时变成主服务器的从服务器,都有主服务器的所有数据
  2. 从机是否可写?
    答案:不可以
  3. 主机shutdown后会发生什么?
    答案:从机在原地等待等待主机上岗。
    在这里插入图片描述

7. 复制原理

在这里插入图片描述

8. 薪火相传

在这里插入图片描述

  1. 再次设置6381的主服务器:slaveof 127.0.0.1 6380
  2. 6379的状态:和6381没有联系
    在这里插入图片描述
  3. 6380的状态:主是6379,还有个从是6381
    在这里插入图片描述
  4. 问题:
    1. 此时如果6379宕机了,6380还是不能晋升为主服务器,需要进行slaveof no one设置,设置为不跟随任何服务器才行
    2. 如果6380宕机了,那么就不存在主从关系了

9. 哨兵模式:

在这里插入图片描述

9.1 配置哨兵模式

在这里插入图片描述

9.2 启动哨兵:

在这里插入图片描述
在这里插入图片描述

9.3 哨兵模式下的故障问题:

在这里插入图片描述

9.3.1 手动宕机6379,查看哨兵服务器日志:

选取6380为新的主服务器
在这里插入图片描述
在这里插入图片描述
查看6380详细信息:
在这里插入图片描述

9.3.2 重新上线6379,变为从服务器

在这里插入图片描述

9.4 存在的问题:复制延时在这里插入图片描述

9.5 如果哨兵挂了怎么办?

哨兵启动时,会通过发布订阅模式连接到主机,其他的哨兵也会通过这种方式连接到主机,这样哨兵之间就能互相感知。

具体可看:https://blog.csdn.net/weixin_42369687/article/details/122735449

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值