1,准备两个主机,192.168.152.128作为master,192.168.152.129作为slave。修改各自的redis.conf如下:
192.168.152.128 redis.conf
bind 0.0.0.0
daemonize yes
requirepass "123456"
192.168.152.129 redis.conf
bind 0.0.0.0
requirepass "123456"
slaveof 192.168.152.128 6379
masterauth 123456
2.修改各自的sentinel.conf
bind 0.0.0.0
protected-mode no
daemonize yes
#最后那个1有特殊意义,有几个slave就写几个,我现在就只有1个slave所以这里是1
sentinel monitor mymaster 192.168.11.128 6379 1
sentinel auth-pass mymaster 123456
3.启动redis和sentinel,注意启动顺序(先master后slave,先redis-server后先redis-sentinel)
这里有个坑要注意,启动服务后一定要记得在防火墙中开放相应的端口
cd redis/src
redis-server ../redis.conf
redis-sentinel ../sentinel.conf
4.宕机维护
如果192.168.152.128宕机了,sentinel会自动切换到192.168.152.129并将其为master。
但是192.168.152.128重启后是不是能自动作为192.168.152.129的备库呢?答案是不能
解决:修改192.168.152.128的redis.conf如下并重启即可
bind 0.0.0.0
requirepass "123456"
slaveof 192.168.152.129 6379
masterauth 123456
5.其他命令
src/redis-cli查看sentinel信息
info replication
解决redis客户端中文乱码
redis-cli --raw
本文详细介绍了如何配置Redis的主从复制及哨兵系统,包括修改redis.conf和sentinel.conf文件,设置密码验证,启动服务顺序,以及在宕机情况下的角色自动切换和重新配置过程。
3186

被折叠的 条评论
为什么被折叠?



