例:现有3台服务器,需要配置1主1从3哨兵
服务器1:103.162.37.166 (主:redis6380,哨兵1:sentinel26380)
服务器2:103.162.37.167(从:redis6380,哨兵2:sentinel26380)
服务器3:103.162.37.168(哨兵3:sentinel26380)
一.redis【主】服务器基本配置(103.162.37.166:6380):
#绑定ip
bind 103.162.37.166
#绑定端口
port 6380
#是否开启守护线程
daemonize yes
#设置访问密码
requirepass foobared
二.redis【**从】**服务器基本配置(103.162.37.167:6380):
#绑定ip
bind 103.162.37.167
#绑定端口
port 6380
#是否开启守护线程
daemonize yes
#设置访问密码
requirepass foobared
#配置主服务器ip和端口
slaveof 103.162.37.166 6380
#配置主服务器的密码
masterauth foobared
#配置从服务器是否只读
slave-read-only yes
三.哨兵配置,以哨兵1的配置为例
###普通配置
bind 103.162.37.166
port 26380
# 保护模式关闭,这样其他服务起就可以访问此台redis
protected-mode no
# 哨兵模式是否后台启动,默认no,改为yes
daemonize yes
pidfile /var/run/redis-sentinel.pid
# log日志保存位置
logfile /usr/local/redis/sentinel/redis-sentinel.log
# 工作目录
dir /usr/local/redis/sentinel
###核心配置
# 核心配置。
# 第三个参数:哨兵名字,可自行修改。(若修改了,那后面涉及到的都得同步)
# 第四个参数:master主机ip地址
# 第五个参数:redis端口号
# 第六个参数:哨兵的数量。比如2表示,当至少有2个哨兵发现master的redis挂了,那么就将此master标记为宕机节点。这个时候就会进行故障的转移,将其中的一个从节点变为master。
sentinel monitor mymaster 103.162.37.166 6380 2
# master中redis的密码
sentinel auth-pass mymaster foobared
# 哨兵从master节点宕机后,等待多少时间(毫秒),认定master不可用。
# 默认30s,这里为了测试,改成10s
sentinel down-after-milliseconds mymaster 10000
# 当替换主节点后,剩余从节点重新和新master做同步的并行数量,默认为 1
sentinel parallel-syncs mymaster 1
# 主备切换的时间,若在3分钟内没有切换成功,换另一个从节点切换
sentinel failover-timeout mymaster 180000
四.常用命令
info replication #查看redis主从节点信息
nohup ./bin/redis-server redis6380 & #后端启动redis服务
nohup ./bin/redis-sentinel sentinel26380 & #后端启动哨兵
./bin/redis-cli -h host -p port -a password #连接redis客户端
auth password #密码授权