阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Redis主从复制(Master-Slave Replication) 详解

187次阅读
没有评论

共计 14012 个字符,预计需要花费 36 分钟才能阅读完成。

案例测试:

1. Master 新增网卡,修改 server 端配置

IP : 192.168.40.128/24

注释:bind,支持网络连接

2. 新建虚机 slave,配置网络,修改 Redis 配置

#slaveof <masterip> <masterport>

slaveof 192.168.40.128 6379

# masterauth <master-password>

masterauth “zcy1991”

3. 启动 redis,打开日志

Redis 主从复制(Master-Slave Replication) 详解

4. 查看 Replication 信息

Master:

Redis 主从复制(Master-Slave Replication) 详解

Slave:

Redis 主从复制(Master-Slave Replication) 详解

Redis 主从复制(Master-Slave Replication) 详解

5. 配置文件“主从复制”部分【翻译 + 理解】(Verion 3.2.8)

################################# REPLICATION (复制) #################################

——————————————-

# Master-Slave replication. Use slaveof to make a Redis instance a copy of

# another Redis server. A few things to understand ASAP about Redis replication.

# 主从复制。使用 slaveof 使一个 Redis 实例成为另一个 Redis 服务器的拷贝(副本)。

关于 Redis 复制的几个事情:

# 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 slaves.

1) Redis 复制是 异步 的,但是如果它看起来没有与至少给定数量的 slave 连接,您可以配置一个主设备停止接受写入。

# 2) Redis slaves 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.

2) 如果复制链路丢失相对较少的时间,Redis slave 能够与 master 执行部分重新同步。您可能需要根据需要,使用合理的值配置 replication backlog 大小(请参阅此文件的下一个操作)。

# 3) Replication is automatic and does not need user intervention. After a

#    network partition slaves automatically try to reconnect to masters

#    and resynchronize with them.

# 3) 复制是自动的,不需要用户干预。网络分割后,slaves 自动尝试重新连接到 masters 并与它们重新同步。

# slaveof <masterip> <masterport>

# If the master is password protected (using the “requirepass” configuration

# directive below) it is possible to tell the slave to authenticate before

# starting the replication synchronization process, otherwise the master will

# refuse the slave request.

# 如果 master 使用密码保护(使用下面的“requirepass”配置指令),可以在开始复制同步过程之前,告诉 slaves 进行认证,否则 master 将拒绝 slave 连接请求。

# masterauth <master-password>

——————————————-

# When a slave loses its connection with the master, or when the replication

# is still in progress, the slave can act in two different ways:

#当一个 slave 与 master 断开连接或复制仍在进行时,slave 可以采用两种不同的方式:

# 1) if slave-serve-stale-data is set to ‘yes’ (the default) the slave will

#    still reply to client requests, possibly with out of date data, or the

#    data set may just be empty if this is the first synchronization.

# 如果 slave-serve-stale-data 设置为“yes”(默认值),则 slave 仍将回复客户端请求,可能使用过期数据,或者如果这是第一次同步,则数据集可能为空。

# 2) if slave-serve-stale-data is set to ‘no’ the slave will reply with

#    an error “SYNC with master in progress” to all the kind of commands

#    but to INFO and SLAVEOF.

# 如果 slave-serve-stale-data 设置为“no”,除了 INFO 和 SLAVEOF,slave 将回复错误“SYNC with master in progress”给所有类型的命令。

slave-serve-stale-data yes

# You can configure a slave instance to accept writes or not. Writing against

# a slave instance may be useful to store some ephemeral data (because data

# written on a slave will be easily deleted after resync with the master) but

# may also cause problems if clients are writing to it because of a

# misconfiguration.

# 您可以配置 slave 实例接受写入或不接受写入。对 slave 实例进行写操作对存储一些临时数据可能是有用的(因为写在 slave 上的数据将在与 master 重新同步后很容易被删除),但是如果客户端因为错误配置而写入它,也会导致问题。

#

# Since Redis 2.6 by default slaves are read-only. 自 2.6 之后,slaves 默认只读(不可写)

#

# Note: read only slaves are not designed to be exposed to untrusted clients

# on the internet. It’s just a protection layer against miSUSE of the instance.

# Still a read only slave exports by default all the administrative commands

# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve

# security of read only slaves using ‘rename-command’ to shadow all the

# administrative / dangerous commands.

# 注意:只读 slaves 并不设计为了暴露给互联网上的不受信任的客户端。它只是一个防止滥用实例的保护层。Slaves 默认情况下仍然可以导出所有管理命令,如 CONFIG,DEBUG 等。在有限的程度上,您可以使用“rename-command”来提高只读从设备的安全性,以隐藏所有管理 / 危险命令。

# 作为从服务器,默认情况下是只读的(yes),可以修改成 NO,用于写(不建议

slave-read-only yes

——————————————-

# Replication SYNC strategy: disk or socket.  复制同步策略:disk 或 socket

#

# ——————————————————-

# WARNING: DISKLESS REPLICATION IS EXPERIMENTAL CURRENTLY  警告:无磁盘的复制当前还处于试验阶段。

# ——————————————————-

#

# New slaves and reconnecting slaves that are not able to continue the replication

# process just receiving differences, need to do what is called a “full

# synchronization”. An RDB file is transmitted from the master to the slaves.

# The transmission can happen in two different ways:

# 是否使用 socket 方式复制数据。目前 redis 复制提供两种方式,disk 和 socket。如果新的 slave 连上来或者重连的 slave 无法部分同步,就会执行全量同步,master 会生成 rdb 文件。有 2 种方式:disk 方式是 master 创建一个新的进程把 rdb 文件保存到磁盘,再把磁盘上的 rdb 文件传递给 slave。socket 是 master 创建一个新的进程,直接把 rdb 文件以 socket 的方式发给 slave。disk 方式的时候,当一个 rdb 保存的过程中,多个 slave 都能共享这个 rdb 文件。socket 的方式就的一个个 slave 顺序复制。在磁盘速度缓慢,网速快的情况下推荐用 socket 方式。

# 新的 slaves 和重新连接的 slaves,不能继续复制过程只是接收差异,需要做的就是所谓的“完全同步”。一个 RDB 文件从 master 传输到 slaves。

# 传输可以以两种不同的方式发生:

# 1) Disk-backed: The Redis master creates a new process that writes the RDB

#                 file on disk. Later the file is transferred by the parent

#                 process to the slaves incrementally.

磁盘支持:Redis master 创建一个新进程,负责将 RDB 文件写入磁盘。稍后,该文件由父进程以增量方式传送到 slaves。

# 2) Diskless: The Redis master creates a new process that directly writes the

#              RDB file to slave sockets, without touching the disk at all.

# 无盘:Redis master 创建一个新的进程,直接将 RDB 文件写入 slaves sockets,而不触及磁盘。

# With disk-backed replication, while the RDB file is generated, more slaves

# can be queued and served with the RDB file as soon as the current child producing

# the RDB file finishes its work. With diskless replication instead once

# the transfer starts, new slaves arriving will be queued and a new transfer

# will start when the current one terminates.

# 使用 ” 磁盘支持 ” 的复制,在生成 RDB 文件时,只要生成 RDB 文件,当前子进程就完成了它的工作,更多的 slaves 就可以排队并被提供这一份 RDB 文件。(可同时提供给多个 slaves)

# 使用无盘复制,一旦传输开始,新 slaves 到达将排队,并且当当前的一个终止时,新的传输将开始。(顺序复制,一个接一个)

# When diskless replication is used, the master waits a configurable amount of

# time (in seconds) before starting the transfer in the hope that multiple slaves

# will arrive and the transfer can be parallelized.

# 当使用“无盘复制”时,master 在开始传输之前等待可配置的时间量(以秒为单位),希望多个 slaves 将到达并且传输可以并行化。

#

# With slow disks and fast (large bandwidth) networks, diskless replication

# works better. 使用慢磁盘和快速(大带宽)网络,无盘复制工作更好(建议 socket)

repl-diskless-sync no

# When diskless replication is enabled, it is possible to configure the delay

# the server waits in order to spawn the child that transfers the RDB via socket

# to the slaves.

diskless 复制的延迟时间,防止设置为 0。一旦复制开始,节点不会再接收新 slave 的复制请求直到下一个 rdb 传输。所以最好等待一段时间,等更多的 slave 连上来。

# 当启用无磁盘复制时,可以配置服务器等待的延迟,以便产生子进程,通过 socket 传输 RDB 给 slaves。

# This is important since once the transfer starts, it is not possible to serve

# new slaves arriving, that will be queued for the next RDB transfer, so the server

# waits a delay in order to let more slaves arrive.

# 这是很重要的,因为一旦传输开始,不可能服务新到达的 slaves,它们将为下一个 RDB 传输排队,所以服务器等待一段时间,以便让更多的 slaves 到达。

————————————-

# The delay is specified in seconds, and by default is 5 seconds. To disable

# it entirely just set it to 0 seconds and the transfer will start ASAP.

# 延迟以秒为单位指定,默认值为 5 秒。要完全禁用它,仅仅设置为 0 秒即可,传输将尽快开始。

repl-diskless-sync-delay 5

# Slaves send PINGs to server in a predefined interval. It’s possible to change

# this interval with the repl_ping_slave_period option. The default value is 10

# seconds.

#slaves 以预定义的时间间隔向服务器发送 PING。可以使用 repl_ping_slave_period 选项更改此时间间隔。默认值为 10 秒。

#

# repl-ping-slave-period 10

——————————————-

复制连接超时时间。master 和 slave 都有超时时间的设置。master 检测到 slave 上次发送的时间超过 repl-timeout,即认为 slave 离线,清除该 slave 信息。slave 检测到上次和 master 交互的时间超过 repl-timeout,则认为 master 离线。需要注意的是 repl-timeout 需要设置一个比 repl-ping-slave-period 更大的值,不然会经常检测到超时。

# The following option sets the replication timeout for: 设置复制 timeout

#

# 1) Bulk transfer I/O during SYNC, from the point of view of slave. 从 slave 角度看,批量传输 I / O 在 SYNC 期间。

# 2) Master timeout from the point of view of slaves (data, pings).  从 slave 角度看,master 超时(data,pings)

# 3) Slave timeout from the point of view of masters (REPLCONF ACK pings). 从 masters 角度看,slave 超时(REPLCONF ACK pings)

#

# It is important to make sure that this value is greater than the value

# specified for repl-ping-slave-period otherwise a timeout will be detected

# every time there is low traffic between the master and the slave.

#重要的是确保此值大于为 repl-ping-slave-period 指定的值,否则每次 master 和 slave 之间的流量低时都会检测到超时。

#

# repl-timeout 60

——————————————-

是否禁止复制 tcp 链接的 tcp nodelay 参数,可传递 yes 或者 no。默认是 no,即使用 tcp nodelay。如果 master 设置了 yes 来禁止 tcp nodelay 设置,在把数据复制给 slave 的时候,会减少包的数量和更小的网络带宽。但是这也可能带来数据的延迟。默认我们推荐更小的延迟,但是在数据量传输很大的场景下,建议选择 yes。

# Disable TCP_NODELAY on the slave socket after SYNC? SYNC 之后在 slave socket 上禁用 TCP_NODELAY?

#

# If you select “yes” Redis will use a smaller number of TCP packets and

# less bandwidth to send data to slaves. But this can add a delay for

# the data to appear on the slave side, up to 40 milliseconds with

# Linux kernels using a default configuration.

#如果选择“是”,Redis 将使用较少数量的 TCP 数据包和较少带宽将数据发送到从站。但是这可能会增加 数据出现在对端 slave 上的延迟,对于 Linux 内核,使用默认配置可以延迟 40 毫秒。

# If you select “no” the delay for data to appear on the slave side will

# be reduced but more bandwidth will be used for replication.

# 如果选择“否”,数据在 slave 端出现的延迟将减少,但更多的带宽将用于复制

# By default we optimize for low latency, but in very high traffic conditions

# or when the master and slaves are many hops away, turning this to “yes” may

# be a good idea.

#默认情况下,我们针对低延迟进行优化,但在非常高的流量情况下,或者当 master 和 slave 相隔许多跳时,将其转换为“yes”可能是个好主意。

repl-disable-tcp-nodelay no

—————————————

复制缓冲区大小,这是一个环形复制缓冲区,用来保存最新复制的命令。这样在 slave 离线的时候,不需要完全复制 master 的数据,如果可以执行部分同步,只需要把缓冲区的部分数据复制给 slave,就能恢复正常复制状态。缓冲区的大小越大,slave 离线的时间可以更长,复制缓冲区只有在有 slave 连接的时候才分配内存。没有 slave 的一段时间,内存会被释放出来,默认 1m。

# Set the replication backlog size. The backlog is a buffer that accumulates

# slave data when slaves are disconnected for some time, so that when a slave

# wants to reconnect again, often a full resync is not needed, but a partial

# resync is enough, just passing the portion of data the slave missed while

# disconnected.

# 设置复制 backlog 大小。backlog 是一个缓冲器 (buffer) 当 slave 断开一段时间时积累 slave 数据,以便当 slave 想要再次重新连接时,通常不需要再“完全同步(full resync)”,而是“部分再同步(partial resync)”就足够了,仅仅传输 slave 在断开连接时错失的那部分数据(余下的)即可。

#

# The bigger the replication backlog, the longer the time the slave can be

# disconnected and later be able to perform a partial resynchronization.

#复制 backlog 越大,slave 可以断开连接的时间就越长,之后就可以执行”部分重新同步(partial resynchronization)“。

# The backlog is only allocated once there is at least a slave connected. 只有在至少有一个 slave 连接时才分配 backlog。

#

# repl-backlog-size 1mb

# After a master has no longer connected slaves for some time, the backlog

# will be freed. The following option configures the amount of seconds that

# need to elapse, starting from the time the last slave disconnected, for

# the backlog buffer to be freed.

# master 没有 slave 一段时间会释放复制缓冲区的内存,repl-backlog-ttl 用来设置该时间长度。单位为秒。

# 在 master 已经不再连接 slave 一段时间后,backlog 将被释放。以下选项配置需要经过的秒数,从最后一个 slave 服务器断开的时间开始,backlog 缓冲区被释放。

# A value of 0 means to never release the backlog. 值为 0 表示永不释放 backlog

#

# repl-backlog-ttl 3600

———————————

# 当 master 不可用,Sentinel 会根据 slave 的优先级选举一个 master。最低的优先级的 slave,当选 master。而配置成 0,永远不会被选举。

# The slave priority is an integer number published by Redis in the INFO output.

# It is used by Redis Sentinel in order to select a slave to promote into a

# master if the master is no longer working correctly.

#slave 优先级是由 Redis 在 INFO 输出中发布的整数。它由 Redis Sentinel 使用,以便选择 slave,如果 master 不再正常工作,则升级为主设备。

# A slave with a low priority number is considered better for promotion, so

# for instance if there are three slaves with priority 10, 100, 25 Sentinel will

# pick the one with priority 10, that is the lowest.

#具有低优先级编号的 slave 被认为更好地用于提升,因此例如如果存在具有优先级 10,100,25 的三个从设备,则哨兵将选择具有优先级 10,即最低的那个。

# However a special priority of 0 marks the slave as not able to perform the

# role of master, so a slave with priority of 0 will never be selected by

# Redis Sentinel for promotion.

#然而,0 的特殊优先级标志着 slave 不能执行主设备的角色,因此优先级为 0 的从设备将不会被 Redis Sentinel 选择用于升级。

# By default the priority is 100.

slave-priority 100

Redis 主从复制(Master-Slave Replication) 详解

—————————————–

#

# It is possible for a master to stop accepting writes if there are less than

# N slaves connected, having a lag less or equal than M seconds.

#如果连接少于 N 个 slave,具有小于或等于 M 秒的滞后,则 master 可以停止接受写入。

# The N slaves need to be in “online” state.

#

# The lag in seconds, that must be <= the specified value, is calculated from

# the last ping received from the slave, that is usually sent every second.

#以秒为单位的滞后,必须小于指定值,根据从 slave 接收的最后一次 ping 计算,通常每秒发送一次。

# This option does not GUARANTEE that N replicas will accept the write, but

# will limit the window of exposure for lost writes in case not enough slaves

# are available, to the specified number of seconds.

#此选项不保证 N 个副本将接受写入,但会在没有足够的 slave 可用的情况下, 将丢失写入的曝光窗口限制为指定的秒数。

# For example to require at least 3 slaves with a lag <= 10 seconds use:  例如,至少需要 3 个 slave,lag 小于 10s

#

#redis 提供了可以让 master 停止写入的方式,如果配置了 min-slaves-to-write,健康的 slave 的个数小于 N,mater 就禁止写入。master 最少得有多少个健康的 slave 存活才能执行写命令。这个配置虽然不能保证 N 个 slave 都一定能接收到 master 的写操作,但是能避免没有足够健康的 slave 的时候,master 不能写入来避免数据丢失。设置为 0 是关闭该功能。

# min-slaves-to-write 3

# 延迟小于 min-slaves-max-lag 秒的 slave 才认为是健康的 slave

# min-slaves-max-lag 10

#设置 1 或另一个设置为 0 禁用这个特性。

# Setting one or the other to 0 disables the feature.

#

# By default min-slaves-to-write is set to 0 (feature disabled) and

# min-slaves-max-lag is set to 10.  默认配置

# A Redis master is able to list the address and port of the attached

# slaves in different ways. For example the “INFO replication” section

# offers this information, which is used, among other tools, by

# Redis Sentinel in order to discover slave instances.

# Another place where this info is available is in the output of the

# “ROLE” command of a masteer.

# Redis master 能够以不同的方式列出所连接 slave 的地址和端口。例如,“INFO replication”部分提供此信息,除了其他工具之外,Redis Sentinel 还使用该信息来发现 slave 实例。此信息可用的另一个地方在 masterser 的“ROLE”命令的输出中。

#

# The listed IP and address normally reported by a slave is obtained

# in the following way:

# 通常由 slave 报告的列出的 IP 和地址, 通过以下方式获得:

#   IP: The address is auto detected by checking the peer address

#   of the socket used by the slave to connect with the master.

#IP:通过检查 slave 与 master 连接使用的套接字的对等体地址自动检测地址。

#   Port: The port is communicated by the slave during the replication

#   handshake, and is normally the port that the slave is using to

#   list for connections.

# 端口:端口在复制握手期间由 slavet 通信,并且通常是 slave 正在使用列出连接的端口。

# However when port forwarding or Network Address Translation (NAT) is

# used, the slave may be actually reachable via different IP and port

# pairs. The following two options can be used by a slave in order to

# report to its master a specific set of IP and port, so that both INFO

# and ROLE will report those values.

# 然而,当使用端口转发或网络地址转换(NAT)时,slave 实际上可以通过 (不同的 IP 和端口对) 来到达。slave 可以使用以下两个选项,以便向 master 报告一组特定的 IP 和端口,以便 INFO 和 ROLE 将报告这些值。

# There is no need to use both the options if you need to override just

# the port or the IP address. 如果你需要仅覆盖端口或 IP 地址,则没必要使用这两个选项。

#

# slave-announce-ip 5.5.5.5

# slave-announce-port 1234

下面关于 Redis 的文章您也可能喜欢,不妨参考下:

Ubuntu 14.04 下 Redis 安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis 主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm

Redis3.0 配置文件详解  http://www.linuxidc.com/Linux/2017-03/141369.htm

CentOS 7 下 Redis 的安装与配置 http://www.linuxidc.com/Linux/2017-02/140363.htm

Ubuntu 14.04 安装 Redis 与简单配置 http://www.linuxidc.com/Linux/2017-01/139075.htm

Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

CentOS 7.0 安装 Redis 3.2.1 详细过程和使用常见问题 http://www.linuxidc.com/Linux/2016-09/135071.htm

Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

Ubuntu 15.10 下 Redis 集群部署文档 http://www.linuxidc.com/Linux/2016-06/132340.htm

Redis 实战 中文 PDF http://www.linuxidc.com/Linux/2016-04/129932.htm

Redis 热迁移实战总结  http://www.linuxidc.com/Linux/2017-02/141083.htm

Redis 的详细介绍:请点这里
Redis 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/141374.htm

 

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计14012字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中