redis 主从配置,主从切换

时间:2021-01-04 07:22:17

只需修改从配置文件

# slaveof <masterip> <masterport>

slaveof 127.0.0.1 6379

# masterauth <master-password>
masterauth 123123

启动后查看

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6389,state=online,offset=16744,lag=1
master_repl_offset:16758
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:16703
repl_backlog_histlen:56

127.0.0.1:6389> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:16814
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

这就可以了,也可以搭建主从从来减轻主的压力,因为数据的同步与恢复都是有主操作的

> slaveof NO ONE  退出从节点

下面是主从切换,哨兵

[root@centos199 redis]# grep -Ev "^#|^$" sentinel.conf
port 26379
logfile "/home/test/redis/logs/mymaster.log"
sentinel monitor mymaster 192.168.1.99 6379 1
daemonize yes
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 18000
sentinel auth-pass mymaster Chanzor123
sentinel config-epoch mymaster 17
sentinel leader-epoch mymaster 1
dir "/home/test/redis"

sentinel current-epoch 17

启动sentinel

redis-sentinel sentinel.conf

[root@centos105 redis-3.0.6]# grep -Ev "^#|^$" sentinel.conf
port 26379
logfile "/var/log/redis/sentinel.log"
daemonize yes
sentinel monitor mymaster 192.168.1.99 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 18000
sentinel auth-pass mymaster Chanzor123
sentinel config-epoch mymaster 17
sentinel leader-epoch mymaster 1
dir "/home/test/redis-3.0.6"
sentinel current-epoch 17