7.redis哨兵模式
mkdir -p /etc/redis/sentinel
cp /opt/redis-stable/sentinel.conf /etc/redis/sentinel/6379.conf
vim /etc/redis/sentinel/6379.conf
# 修改如下配置
daemonize yes
pidfile /var/run/redis-sentinel-6379.pid
logfile /var/log/redis-sentinel-6379.log
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor mymaster 127.0.0.1 6379 2
# sentinel down-after-milliseconds <master-name> <milliseconds>
# Default is 30 seconds.
sentinel down-after-milliseconds mymaster 30000
# sentinel auth-pass <master-name> <password>
sentinel auth-pass mymaster 123456
# Default is 3 minutes.
sentinel failover-timeout mymaster 180000
# 复制
cp /etc/redis/sentinel/6379.conf /etc/redis/sentinel/6380.conf
vim /etc/redis/sentinel/6380.conf
# 修改如下配置
port 26380
pidfile /var/run/redis-sentinel-6380.pid
logfile /var/log/redis-sentinel-6380.log
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor mymaster 127.0.0.1 6379 2
# sentinel down-after-milliseconds <master-name> <milliseconds>
# Default is 30 seconds.
sentinel down-after-milliseconds mymaster 30000
# sentinel auth-pass <master-name> <password>
sentinel auth-pass mymaster 123456
# Default is 3 minutes.
sentinel failover-timeout mymaster 180000
# 启动
/usr/local/src/redis/bin/redis-server /etc/redis/sentinel/6379.conf --sentinel
/usr/local/src/redis/bin/redis-server /etc/redis/sentinel/6380.conf --sentinel
# 查看日志
tail -fn 300 /var/log/redis-sentinel-6379.log
tail -fn 300 /var/log/redis-sentinel-6380.log
# 关掉主节点
/etc/init.d/redis_6379 stop
# 连接从节点客户端
/usr/local/src/redis/bin/redis-cli -a 123456 -c -h 127.0.0.1 -p 6380
# 查看主从信息
info replication
# 如图,role已经从slave 变成了master, 连接的从库也变为0
# 重启6379服务,按正常原先的master会变成slave
查看发现connected_slaves:0 ,原因是:之前做主从的时候,6379没有设置 masterauth
注:建议后期要用哨兵模式,设置主从的时候要先设置masterauth,确保后期高可用能正常使用
# masterauth <master-password>
masterauth 123456