[root@host53 ~]# /etc/init.d/redis_6379 stop
[root@host53 ~]# vim /etc/redis/6379.conf
282 # slaveof
282 slaveof 192.168.4.52 6352
:wq
[root@host53 ~]# /etc/init.d/redis_6379 start
[root@host53 ~]# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> info replication
# Replication
role:slave
master_host:192.168.4.52
master_port:6352
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:5970
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:f55ecd93599d6b47277955e4de285afb339e9a4f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5970
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:5929
repl_backlog_histlen:42
手动切换临时成为主库:
192.168.4.53:6353> SLAVEOF no one
OK
192.168.4.53:6353> info replication
# Replication
role:master
connected_slaves:0
master_replid:9f315efb80c002a1495988a4b956da7d718b3998
master_replid2:f55ecd93599d6b47277955e4de285afb339e9a4f
master_repl_offset:6082
second_repl_offset:6083
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:5929
repl_backlog_histlen:154
重起服务继续成为从库:
[root@host53 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@host53 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host53 ~]# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> info replication
# Replication
role:slave
master_host:192.168.4.52
master_port:6352
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:6166
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:f55ecd93599d6b47277955e4de285afb339e9a4f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:6166
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:6153
repl_backlog_histlen:14
主库设置连接密码时,从库的配置。
给主库设置密码:
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 shutdown
[root@host51 ~]# ss -ntulp | grep redis
[root@host51 ~]# vim /etc/redis/6379.conf
282 # slaveof
[root@host2 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@host2 ~]# vim /etc/redis/6379.conf
[root@host51 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host51 ~]# vim /etc/init.d/redis_6379
$CLIEXEC -h 192.168.4.51 -p 6351 -a 123456 shutdown
[root@host2 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@host2 ~]# vim /etc/redis/6379.conf
282 # slaveof
289 # masterauth
282 slaveof 192.168.4.51 6351
289 masterauth 123456
:wq
[root@host2 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
连接从库,查看信息
[root@host2 ~]# redis-cli -h 192.168.4.52 -p 6352
192.168.4.52:6352> info replication
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:98
slave_priority:100
slave_read_only:1
connected_slaves:1
slave0:ip=192.168.4.53,port=6353,state=online,offset=98,lag=0
master_replid:eda3397625fe384aa6e7a7a64fb826103a9b0b7b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:98
主从同步的哨兵模式:
主库坏掉,自动升级为主库:(适合 主从 主从从结构)
开启哨兵模式:
在 slave 主机运行哨兵程序
在 slave 主机编辑 sentinel.conf 文件
结构准备:
模拟测试:
192.168.4.51:6351> SHUTDOWN //关闭51的主库
not connected>
192.168.4.52:6352> info replication
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1008
master_link_down_since_seconds:3
slave_priority:100
slave_read_only:1
connected_slaves:1
slave0:ip=192.168.4.53,port=6353,state=online,offset=1008,lag=0
master_replid:eda3397625fe384aa6e7a7a64fb826103a9b0b7b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1008
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1008
配置哨兵模式
[root@host2 ~]# redis-se
redis-sentinel redis-server
[root@host2 ~]# redis-se^C
[root@host2 ~]# vim /etc/sentinel.conf
sentinel monitor ser51 192.168.4.51 6351 1 //
sentinel auth-pass host51 123456
~
:wq
启动哨兵模式
[root@host2 ~]# redis-sentinel /etc/sentinel.conf
模拟主库坏了,主动切换主库
[root@host51 ~]# /etc/init.d/redis_6379 stop
[root@host2 ~]# redis-cli -h 192.168.4.52 -p 6352
192.168.4.52:6352> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.4.53,port=6353,state=online,offset=6396,lag=1
master_replid:d079ae4c3bf40e49293151251e0786e23a0c4d25
master_replid2:447b9371185f6729df431826688f7a6218687f6a
master_repl_offset:6533
second_repl_offset:2728
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:6533
192.168.4.52:6352>
切换从库变主库后,自动删除配置文件的从库信息
[root@host2 ~]# grep "slaveof 192.168.4.51 6351" /etc/redis/6379.conf
修复好的主库,加入架构后,自动变成从库
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 -a 123456
192.168.4.51:6351> info replication
# Replication
role:slave
master_host:192.168.4.52
master_port:6352
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:16904
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:d079ae4c3bf40e49293151251e0786e23a0c4d25
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:16904
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:16745
repl_backlog_histlen:160
哨兵模式原理:
https://blog.csdn.net/u012240455/article/details/81843714