redis主从结构 主从从

配置环境

在已有的一主多从的基础上改变结构为主从从
已有一主多从https://blog.csdn.net/qq_44659804/article/details/126323971?spm=1001.2014.3001.5502

节点IP地址
master192.168.44.144
slave1192.168.44.145
slave2192.168.44.146

改变为主从从
改变原来的slave1 192.168.44.145,使其拥有两种角色,即使192.168.44.144的从服务器,又是192.168.44.146的主服务器
改变原来的slave2 192.168.44.146,使其为192.168.44.145的从服务器

主从从

master192.168.44.144
slave(master)192.168.44.145
slave01192.168.44.146

恢复192.168.44.146为独立的数据库前状态

恢复前查看192.168.44.146状态
角色为192.168.44.144的从服务器
[root@redis_s2 ~]# redis-cli -h 192.168.44.146 -p 6379
192.168.44.146:6379> info replication
# Replication
role:slave
master_host:192.168.44.144
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:1610
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:0d387793eec1ce8aced7814350148fef9567edf3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1610
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1610
恢复前查看master 192.168.44.144的状态
有两个从服务器
[root@redis_m ~]# redis-cli -h 192.168.44.144 -p 6379
192.168.44.144:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.44.145,port=6379,state=online,offset=308,lag=0
slave1:ip=192.168.44.146,port=6379,state=online,offset=308,lag=0
master_replid:0d387793eec1ce8aced7814350148fef9567edf3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:308
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:308

恢复192.168.44.146为独立的数据库

192.168.44.146角色是master,从服务器为0
192.168.44.146:6379> slaveof no one
OK
192.168.44.146:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:561e5d907e49ce879849e10d15fbcb0ac9fe766d
master_replid2:0d387793eec1ce8aced7814350148fef9567edf3
master_repl_offset:2170
second_repl_offset:2171
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2170
再次查看master 192.168.44.144状态
只有一个从服务器
192.168.44.144:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.44.145,port=6379,state=online,offset=2520,lag=1
master_replid:0d387793eec1ce8aced7814350148fef9567edf3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2520
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2520

设置192.168.44.146为

192.168.44.145的从服务器

设置前查看192.168.44.145的状态为从
[root@redis_s1 ~]# redis-cli -h 192.168.44.145 -p 6379
192.168.44.145:6379> info replication
# Replication
role:slave
master_host:192.168.44.144
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:2716
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:0d387793eec1ce8aced7814350148fef9567edf3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2716
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2716
设置
角色为从,主为192.168.44.145
192.168.44.146:6379> slaveof 192.168.44.145 6379
OK
192.168.44.146:6379> info replication
# Replication
role:slave
master_host:192.168.44.145
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:2828
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:0d387793eec1ce8aced7814350148fef9567edf3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2828
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2815
repl_backlog_histlen:14
再次查看192.168.44.145的状态
角色为从,主为192.168.44.144
角色为主,从为192.168.44.146
192.168.44.145:6379> info replication
# Replication
role:slave
master_host:192.168.44.144
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:3038
slave_priority:100
slave_read_only:1
connected_slaves:1
slave0:ip=192.168.44.146,port=6379,state=online,offset=3038,lag=0
master_replid:0d387793eec1ce8aced7814350148fef9567edf3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3038
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:3038

设置为永久生效

slave01 192.168.44.146
[root@redis_s2 ~]# redis-cli -h 192.168.44.146 -p 6379
192.168.44.146:6379> config rewrite
OK

验证主从从

查看数据,均有已存入的 a b c d
master 192.168.44.144
192.168.44.144:6379> keys *
1) "b"
2) "a"
3) "d"
4) "c"
slave(master)192.168.44.145
192.168.44.145:6379> keys *
1) "d"
2) "b"
3) "a"
4) "c"
slave01 192.168.44.146
192.168.44.146:6379> keys *
1) "a"
2) "b"
3) "d"
4) "c"
master 192.168.44.144 清空已有数据再次存入数据
另外两个服务器均能查询新的数据
master 192.168.44.144
192.168.44.144:6379> flushdb
OK
192.168.44.144:6379> keys *
(empty list or set)
192.168.44.144:6379> mset aa 11 bb 22 cc 33 dd 44
OK
192.168.44.144:6379> keys *
1) "cc"
2) "bb"
3) "dd"
4) "aa"
192.168.44.144:6379>
slave(master)192.168.44.145
192.168.44.145:6379> keys *
(empty list or set)
192.168.44.145:6379> keys *
1) "bb"
2) "aa"
3) "dd"
4) "cc"
slave01 192.168.44.146
192.168.44.146:6379> keys *
(empty list or set)
192.168.44.146:6379> keys *
1) "aa"
2) "cc"
3) "bb"
4) "dd"

给主从从结构增加验证功能

主从从结构中的master服务器设置了连接密码,
slave服务器要指定连接密码才能正常同步master主机数据

给master 192.168.44.144 设置连接密码

192.168.44.144:6379> config get requirepass
1) "requirepass"
2) ""
192.168.44.144:6379> config set requirepass 123456
OK
192.168.44.144:6379> config get requirepass
(error) NOAUTH Authentication required.
192.168.44.144:6379> auth 123456
OK
192.168.44.144:6379> config get requirepass
1) "requirepass"
2) "123456"

给slave(master)192.168.44.145设置连接密码,密码同master;

给slave(master)192.168.44.145指定连接master的密码

192.168.44.145:6379> config get requirepass
1) "requirepass"
2) ""
192.168.44.145:6379> config set requirepass 123456
OK
192.168.44.145:6379> auth 123456
OK
192.168.44.145:6379> config get masterauth
1) "masterauth"
2) ""
192.168.44.145:6379> config set masterauth 123456
OK
192.168.44.145:6379> config get masterauth
1) "masterauth"
2) "123456"
192.168.44.145:6379> config rewrite
OK

给slave01指定连接slave(master)的连接密码

192.168.44.146:6379> config set masterauth 123456
OK
192.168.44.146:6379> config rewrite
OK
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值