这是我参与2022首次更文挑战的第28天,活动详情查看:2022首次更文挑战
hello,你好呀,我是灰小猿,一个超会写bug的程序猿!
今天我们来讲解一下如何实现Redis的主从复制架构,这是在使用Redis进行项目开发的时候最基本也是最常用的一个Redis架构,所以掌握Redis主从架构的使用是十分重要的。
Redis主从复制的原理是: 一个主节点可以有多个从节点,一个从节点只能对应一个主节点
环境配置
在使用Redis主从复制的时候,只需要配置从库,不需要配置主库!
查看当前库的信息:
通过INFO replication命令,我们可以查看当前库的信息:
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:0
master_replid:7af8929260834b6a6499910900c51765193db3b5
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主从复制的案例分析,首先,复制三个配置文件,修改其中的配置信息:
-
- 端口号 port 6382
- 修改为三个不一样的端口号
-
- pid名称 pidfile /var/run/redis6382.pid
-
- log文件名称 logfile "6382.log"
-
- dump.rdb名称 dbfilename dump6382.rdb
配置从机
默认情况下,每台Redis服务器都是主节点,我们一般情况下只用配置从机就好了!
INFO replication查看配置信息
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:3
slave0:ip=127.0.0.1,port=6380,state=online,offset=154,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=154,lag=1
slave2:ip=127.0.0.1,port=6382,state=online,offset=154,lag=1
master_replid:6da34eb34e2644c39209bb056fa817f6c64dfcb2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:168
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:168
从机连接主机
通过命令SLAVEOF 127.0.0.1 6379 我们可以让从机连接主机,
操作如下:
127.0.0.1:6380> SLAVEOF 127.0.0.1 6379
OK
127.0.0.1:6380> INFO replication
# Replication
role:slave # 查看从机信息
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:14
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:6da34eb34e2644c39209bb056fa817f6c64dfcb2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14
真实的配置应该是在配置文件中配置的,主要做的配置如下:
################################# REPLICATION #################################
# Master-Replica replication. Use replicaof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# +------------------+ +---------------+
# | Master | ---> | Replica |
# | (receive writes) | | (exact copy) |
# +------------------+ +---------------+
#
# 1) Redis replication is asynchronous, but you can configure a master to
# stop accepting writes if it appears to be not connected with at least
# a given number of replicas.
# 2) Redis replicas are able to perform a partial resynchronization with the
# master if the replication link is lost for a relatively small amount of
# time. You may want to configure the replication backlog size (see the next
# sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
# network partition replicas automatically try to reconnect to masters
# and resynchronize with them.
# 配置主机的地址和端口
replicaof <masterip> <masterport>
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
# 如果有密码就配置密码
masterauth <master-password>
细节问题
这里需要注意: 主机可以设置值,从机不能设置值,
主机中的所有信息和数据都会自动被从机保存,主机负责写,从机负责读,从机只能读不能写;
127.0.0.1:6380> SET k2 v2
(error) READONLY You can't write against a read only replica.
主机断开
如果主机断掉,从机还在。
如果从机要更改主机,那么从机上面需要进行配置。
如果主机断开再连接,从机依旧可以读取到主机之前写的数据。
从机断开
如果从机断开,再次连接时,依然可以获取到主机中的数据
复制原理
Slave启动成功连接到master后会发送一个sync同步命令。 Master接收到命令之后,启动后台的存盘进程,同时收集所有接收到的用于修改数据集命令,在后台进程执行完毕之后,master将传送整个数据文件到slave,并完成一次完全同步。
全量复制: slave服务器在接收到数据库文件数据之后,将其存盘并加载到内存中。
增量复制: master继续将新的所有收集到的修改命令依次传给slave,完成同步。
但是只要是重新连接master,一次完全同步(全量复制)将被自动执行,我们的数据就一定可以在从机中看到。
以上就是Redis实现主从复制的基本内容!
我是灰小猿,我们下期见!