1. 是什么?
主从复制:就是主机数据更新后根据配置和策略,自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主
2. 用处:
- 读写分离,性能扩展
- 容灾快速恢复
3. 配置临时从服务器:
步骤:
- 设置三个conf文件,在每个conf文件中include redis.conf主配置文件,端口号、文件名字不一样
- 通过这些配置文件启动redis
- 查看服务器状态info replication,可以看到每个服务器都是主服务器
- 设置称为某个服务器的从服务器:slaveof ip port
设置6380和6381为6379的从服务器
- 查看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. 一主二从相关问题:
- 一个从服务器宕机关机了,之后主服务器进行了写操作,此时从服务器重新连接并设置为主服务器的从服务器;从服务器还有在这段时间内主服务器写入的值吗?
答案:有,无论什么时变成主服务器的从服务器,都有主服务器的所有数据 - 从机是否可写?
答案:不可以 - 主机shutdown后会发生什么?
答案:从机在原地等待等待主机上岗。
7. 复制原理
8. 薪火相传
- 再次设置6381的主服务器:slaveof 127.0.0.1 6380
- 6379的状态:和6381没有联系
- 6380的状态:主是6379,还有个从是6381
- 问题:
- 此时如果6379宕机了,6380还是不能晋升为主服务器,需要进行slaveof no one设置,设置为不跟随任何服务器才行
- 如果6380宕机了,那么就不存在主从关系了
9. 哨兵模式:
9.1 配置哨兵模式
9.2 启动哨兵:
9.3 哨兵模式下的故障问题:
9.3.1 手动宕机6379,查看哨兵服务器日志:
选取6380为新的主服务器
查看6380详细信息:
9.3.2 重新上线6379,变为从服务器
9.4 存在的问题:复制延时![在这里插入图片描述](https://img-blog.csdnimg.cn/35e5f7e3074540929c092b3abefadb5d.png)
9.5 如果哨兵挂了怎么办?
哨兵启动时,会通过发布订阅模式连接到主机,其他的哨兵也会通过这种方式连接到主机,这样哨兵之间就能互相感知。
具体可看:https://blog.csdn.net/weixin_42369687/article/details/122735449