语法 https://segmentfault.com/a/1190000002680804
启动3台redis 6379,6380,6381
cp 多个redis.conf文件
开启daemonize yes
PID文件名字
端口
log文件名字
dump.rdb名字
requirepass 密码
更改从机的masterauth密码 和requirepass密码一致.
下载的redis 有默认的sentinel.conf默认配置文件
touch sentinel.conf.创建一个哨兵配置文件
#1表示在sentinel集群中只要有1个sentinel认为master死了, 就算真的死了,
#集群的时候sentinel也是多个
sentinel monitor mymaster 127.0.0.1 6380 1
#如果10秒后,mysater仍没活过来,则启动failover(主备切换) #sentinel 会给master发送心跳PING来确认
sentinel down-after-milliseconds mymaster 3000 #指定多少个slave同时对新matster同步, 值越小耗时越长,越大多个slave因为replication不可以 #设定为1 每次只有1个slve处于不可以处理命令请求状态 sentinel failover-timeout mymaster 10000 #后台执行 daemonize yes #指定工作目录 dir "/usr/local/redis/sentinel-work" protected-mode no logfile "/usr/local/redis/sentinel-log/sentinel.log" #主 的密码 sentinel auth-pass mymaster bb123456 # Generated by CONFIG REWRITE port 26379
执行redis-server redis.conf启动redis
执行redis-cli -p 6379 [6380,6381] 进入redis客户端查看info replication信息,
运行sentinel有两种方式:
redis-sentinel /path/to/sentinel.conf
redis-server /path/to/sentinel.conf --sentinel
主从切换日志
10671:X 20 Jun 12:45:55.951 # +sdown master mymaster 127.0.0.1 6379 10671:X 20 Jun 12:45:55.951 # +odown master mymaster 127.0.0.1 6379 #quorum 1/1 10671:X 20 Jun 12:45:55.951 # +new-epoch 2 10671:X 20 Jun 12:45:55.951 # +try-failover master mymaster 127.0.0.1 6379 10671:X 20 Jun 12:45:55.954 # +vote-for-leader 0bc851c61ce22b98c9b3c7857f240721a5d6c9b5 2 10671:X 20 Jun 12:45:55.954 # +elected-leader master mymaster 127.0.0.1 6379 10671:X 20 Jun 12:45:55.954 # +failover-state-select-slave master mymaster 127.0.0.1 6379 10671:X 20 Jun 12:45:56.038 # +selected-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379 10671:X 20 Jun 12:45:56.038 * +failover-state-send-slaveof-noone slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379 10671:X 20 Jun 12:45:56.128 * +failover-state-wait-promotion slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379 10671:X 20 Jun 12:45:57.020 # +promoted-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379 选举6381作为新的主机 10671:X 20 Jun 12:45:57.020 # +failover-state-reconf-slaves master mymaster 127.0.0.1 6379 10671:X 20 Jun 12:45:57.080 * +slave-reconf-sent slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379 10671:X 20 Jun 12:45:58.027 * +slave-reconf-inprog slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379 10671:X 20 Jun 12:45:58.027 * +slave-reconf-done slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379 10671:X 20 Jun 12:45:58.088 # +failover-end master mymaster 127.0.0.1 6379 10671:X 20 Jun 12:45:58.088 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6381 切换81为新的主机 10671:X 20 Jun 12:45:58.088 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381 10671:X 20 Jun 12:45:58.088 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381 10671:X 20 Jun 12:46:01.162 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381