Redis主从配置以及哨兵模式

Redis主从模式,应用写master,读slave,减轻master的压力。

配置主结点:

daemonize yes
port 6379
bind 0.0.0.0 pidfile /opt/redis/redis_6379.pid

配置从结点的时候,除了port不同,还在末尾加上一行:

slaveof 127.0.0.1 6379

 启动服务

>redis-server /path/to/6379.conf
>redis-server /path/to/6380.conf
>redis-server /path/to/6381.conf

启动后,登录并查看master的主从信息

>redis-cli -h 127.0.0.1 -p 6379
>info replication

打印:
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=0,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=0,lag=0

此时,在主库set a b,在从库就可以get a,成功打印b

-----------------------------------------

Redis哨兵模式,用于master宕机后的主从切换

额外启动若干个哨兵进程,通过1秒发1次消息的方式,监视master与slave的在线状态

一个哨兵认为master挂了,标记master为sdown

如果超过一定数量的哨兵都认为master挂了,标记master为odown,同时投票选举一个新的master,将其他slave从这个新master进行同步

如果挂了的master重新上线,它将是新master的slave

原文地址:https://www.cnblogs.com/shuada/p/9328161.html