案例测试:

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,打开日志

spacer.gifwKiom1i60hLhJ381AACV_vxopzM371.png-wh_50

4. 查看Replication信息

Master:

spacer.gifwKiom1i60h-jc22IAABZlAMws74903.png-wh_50

Slave:

wKioL1i60i7zP36nAABl0jugJEE491.png-wh_50

wKiom1i60jqDzzfeAAAyZIN0jL4161.png-wh_50


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

wKioL1i60oqjKbSQAAA_qUgwa7A871.png-wh_50

-----------------------------------------

#

# 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



参考资源:

  1.redis3.0配置文件详解