redis主从复制实战

1、基础

1.1、什么是主从复制

主机数据更新后根据配置和策略, 自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主

1.2、作用

1、读写分离,性能扩展
2、容灾快速恢复

1.3、流程图

2、环境准备

2.1、规划

1台服务器,3个redis实例,生产是多服务器

2.2、配置流程

拷贝多个redis.conf文件include(写绝对路径)
开启daemonize yes
Pid文件名字pidfile
指定端口port
Log文件名字
dump.rdb名字dbfilename
Appendonly 关掉或者换名字

3、准备实例的配置

3.1、配置主配置文件redis.conf

]# vi /usr/local/redis-6.2.5/etc/redis.conf
daemonize yes
bind *
dir /usr/local/redis-6.2.5/data
# 修改如上参数

3.2、实例1-6379.conf

cat << 'CAT_END' > /usr/local/redis-6.2.5/etc/6379.conf
include /usr/local/redis-6.2.5/etc/redis.conf
logfile "/usr/local/redis-6.2.5/log/6379.log"
port 6379
dbfilename dump_6379.rdb
CAT_END

3.3、实例2-6380.conf

cat << 'CAT_END' > /usr/local/redis-6.2.5/etc/6380.conf
include /usr/local/redis-6.2.5/etc/redis.conf
logfile "/usr/local/redis-6.2.5/log/6380.log"
port 6380
dbfilename dump_6380.rdb
CAT_END

3.4、实例3-6381.conf

cat << 'CAT_END' > /usr/local/redis-6.2.5/etc/6381.conf
include /usr/local/redis-6.2.5/etc/redis.conf
logfile "/usr/local/redis-6.2.5/log/6381.log"
port 6381
dbfilename dump_6381.rdb
CAT_END

4、启动实例

4.1、启动3个实例

redis-server /usr/local/redis-6.2.5/etc/6379.conf
redis-server /usr/local/redis-6.2.5/etc/6380.conf
redis-server /usr/local/redis-6.2.5/etc/6381.conf

4.2、检查启动端口

]# netstat -tunlp | grep redis
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      27980/redis-server  
tcp        0      0 0.0.0.0:6380            0.0.0.0:*               LISTEN      27986/redis-server  
tcp        0      0 0.0.0.0:6381            0.0.0.0:*               LISTEN      27992/redis-server 

4.3、查看3个实例运行情况

]# redis-cli -p 6379 info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:b80f2280de9b36fd760da94d14e86ceec263f60b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

]# redis-cli -p 6380 info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:aa527b340a0bd99264ea949295c4724549b5b6b4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

]# redis-cli -p 6381 info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:8d7d9dc2e5b074aec249838a2e3ca035cae4ca68
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

5、配从(库)不配主(

5.1、规划

637963806381 从

5.2、使用命令配置主从【临时的,重启服务会失效】

5.2.1、6380、6381上执行

]# redis-cli -p 6380 slaveof 192.168.10.19 6379
OK
]# redis-cli -p 6381 slaveof 192.168.10.19 6379 OK

5.2.2、查询实例角色状态

]# redis-cli -p 6379 info replication | grep -i role
role:master

]# redis-cli -p 6380 info replication | grep -i role
role:slave

]# redis-cli -p 6381 info replication | grep -i role
role:slave

5.3、使用配置文件方式配置主从复制

echo "replicaof 192.168.10.19 6379" >>/usr/local/redis-6.2.5/etc/6381.conf 
echo "replicaof 192.168.10.19 6379" >>/usr/local/redis-6.2.5/etc/6380.conf 

5.4、测试主从复制

]# redis-cli -p 6379 set name cyc
OK
]# redis-cli -p 6380 get name "cyc"

]# redis-cli -p 6381 get name "cyc"

# 主从复制成功

6、从节点提升为主节点

6.1、问题点

当一个master宕机后,后面的slave可以立刻升为master,其后面的slave不用做任何修改。

6.2、模拟master宕机

redis-cli -p 6379 shutdown

6.3、6380提升为master

# 提升为master
redis-cli -p 6380 slaveof no one

# slave更换master信息 redis
-cli -p 6381 slaveof 192.168.10.19 6380

6.4、查询实例状态

]# redis-cli -p 6380 info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:02ec57c58ff4e3411ec19c122849075aed435f3b
master_replid2:fed103c20318fa7c0c7a498237891831cc44682e
master_repl_offset:1077
second_repl_offset:1078
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1077

]# redis-cli -p 6381 info replication
# Replication
role:slave
master_host:192.168.10.19
master_port:6380
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:1147
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:02ec57c58ff4e3411ec19c122849075aed435f3b
master_replid2:fed103c20318fa7c0c7a498237891831cc44682e
master_repl_offset:1147
second_repl_offset:1078
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:463
repl_backlog_histlen:685

6.5、测试数据读写

# 以前的数据
]# redis-cli -p 6380 get name
"cyc"
]# redis-cli -p 6381 get name
"cyc"

# 新写入的数据和查询数据
]# redis-cli -p 6380 set age 18
OK
]# redis-cli -p 6381 get age
"18"

7、主从复制原理

7.1、流程说明

1、Slave启动成功连接到master后会发送一个sync命令
2、Master接到命令启动后台的存盘进程,同时收集所有接收到的用于修改数据集命令, 在后台进程执行完毕之后,master将传送整个数据文件到slave,以完成一次完全同步
3、全量复制:而slave服务在接收到数据库文件数据后,将其存盘并加载到内存中。
4、增量复制:Master继续将新的所有收集到的修改命令依次传给slave,完成同步
5、但是只要是重新连接master,一次完全同步(全量复制)将被自动执行

7.2、流程图

 

posted @ 2023-05-16 12:34  小粉优化大师  阅读(40)  评论(0编辑  收藏  举报