一、redis主从原理
1.从库配置主从同步
2.从库像主库发起sync命令
3.主库接收sync命令,执行bgsave,生成持久化rdb文件
4.主库将新的数据临时写入缓冲区
5.主库将rdb文件推送至从库
6.从库接收到rdb文件,会清空自己的数据
7.从库读取并导入rdb文件
8.主库将缓冲区的数据也传给从库
9.实现数据同步
二、环境准备
角色 | 主机 | IP | 端口 |
---|---|---|---|
主库 | db01 | 172.16.1.51 | 6379 |
从库 | db02 | 172.16.1.52 | 6379 |
从库 | db03 | 172.16.1.53 | 6379 |
三、配置主从
#1.登陆三台redis
[root@db01 redis]# redis-cli -h 172.16.1.51
172.16.1.51:6379>
[root@db02 redis]# redis-cli -h 172.16.1.52
172.16.1.52:6379>
[root@db03 redis]# redis-cli -h 172.16.1.53
172.16.1.53:6379>
#2.查看主从状态
172.16.1.51:6379> info replication
# Replication
role:master
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
#3.配置主从
172.16.1.52:6379> SLAVEOF 172.16.1.51 6379
OK
172.16.1.53:6379> SLAVEOF 172.16.1.51 6379
OK
#4.再次查看主从状态
#主库
172.16.1.51:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.16.1.52,port=6379,state=online,offset=29,lag=0
slave1:ip=172.16.1.53,port=6379,state=online,offset=29,lag=0
master_repl_offset:29
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:28
#从库
172.16.1.52:6379> info replication
# Replication
role:slave
master_host:172.16.1.51
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:71
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
#5.如果主库有密码
在从库的配置文件中加上
masterauth 123
四、模拟主库故障
[root@db01 redis]# redis-cli shutdown
五、查看从库状态
172.16.1.52:6379> info replication
# Replication
role:slave
master_host:172.16.1.51
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:393
master_link_down_since_seconds:63
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
172.16.1.52:6379> set k1 v2
(error) READONLY You can't write against a read only slave.
#从库仍然还是从库,还是只读权限,没有办法提供写服务
六、选择一台机器,取消主从
#取消主从
172.16.1.52:6379> SLAVEOF no one
OK
#再次查看状态
172.16.1.52:6379> info replication
# Replication
role:master
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
七、将其他从库指向新的主库
#重新做主从
172.16.1.53:6379> SLAVEOF 172.16.1.52 6379
OK
172.16.1.53:6379> info replication
# Replication
role:slave
master_host:172.16.1.52
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:1
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
标签:offset,Redis,6379,repl,master,172.16,主从,backlog,分布式
From: https://www.cnblogs.com/GAO321/p/16707390.html