日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Redis主从复制(Master-Slave Replication)

發(fā)布時(shí)間:2023/12/4 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Redis主从复制(Master-Slave Replication) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

案例測(cè)試:

1. Master新增網(wǎng)卡,修改server端配置

IP : 192.168.40.128/24

注釋: bind,支持網(wǎng)絡(luò)連接

2. 新建虛機(jī)slave,配置網(wǎng)絡(luò),修改redis配置

#slaveof <masterip> <masterport>

slaveof 192.168.40.128 6379

# masterauth <master-password>

masterauth "zcy1991"

3. 啟動(dòng)redis,打開日志

4. 查看Replication信息

Master:

Slave:


5.配置文件 “主從復(fù)制”部分 【翻譯+理解】 (Verion 3.2.8)

################################# REPLICATION (復(fù)制) #################################

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

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

#主從復(fù)制。 使用slaveof使一個(gè)Redis實(shí)例成為另一個(gè)Redis服務(wù)器的拷貝(副本)。

關(guān)于Redis復(fù)制的幾個(gè)事情:

# 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復(fù)制是異步的,但是如果它看起來沒有與至少給定數(shù)量的slave連接,您可以配置一個(gè)主設(shè)備停止接受寫入。

# 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) 如果復(fù)制鏈路丟失相對(duì)較少的時(shí)間,Redis slave能夠與master執(zhí)行部分重新同步。 您可能需要根據(jù)需要,使用合理的值配置replication backlog 大小(請(qǐng)參閱此文件的下一個(gè)操作)。

# 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) 復(fù)制是自動(dòng)的,不需要用戶干預(yù)。 網(wǎng)絡(luò)分割后,slaves 自動(dòng)嘗試重新連接到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使用密碼保護(hù)(使用下面的“requirepass”配置指令),可以在開始復(fù)制同步過程之前,告訴slaves進(jìn)行認(rèn)證,否則master將拒絕slave連接請(qǐng)求。

# 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:

#當(dāng)一個(gè)slave與master斷開連接或復(fù)制仍在進(jìn)行時(shí),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設(shè)置為“yes”(默認(rèn)值),則slave仍將回復(fù)客戶端請(qǐng)求,可能使用過期數(shù)據(jù),或者如果這是第一次同步,則數(shù)據(jù)集可能為空。

# 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設(shè)置為“no”,除了INFO和SLAVEOF,slave將回復(fù)錯(cuò)誤“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實(shí)例接受寫入或不接受寫入。 對(duì)slave實(shí)例進(jìn)行寫操作對(duì)存儲(chǔ)一些臨時(shí)數(shù)據(jù)可能是有用的(因?yàn)閷懺趕lave上的數(shù)據(jù)將在與master重新同步后很容易被刪除),但是如果客戶端因?yàn)殄e(cuò)誤配置而寫入它,也會(huì)導(dǎo)致問題。

#

# Since Redis 2.6 by default slaves are read-only. 自2.6之后,slaves默認(rèn)只讀(不可寫)

#

# 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并不設(shè)計(jì)為了暴露給互聯(lián)網(wǎng)上的不受信任的客戶端。 它只是一個(gè)防止濫用實(shí)例的保護(hù)層。 Slaves默認(rèn)情況下仍然可以導(dǎo)出所有管理命令,如CONFIG,DEBUG等。 在有限的程度上,您可以使用“rename-command”來提高只讀從設(shè)備的安全性,以隱藏所有管理/危險(xiǎn)命令。

#作為從服務(wù)器,默認(rèn)情況下是只讀的(yes),可以修改成NO,用于寫(不建議

slave-read-only yes

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

# Replication SYNC strategy: disk or socket. ?復(fù)制同步策略: disk或socket

#

# -------------------------------------------------------

# WARNING: DISKLESS REPLICATION IS EXPERIMENTAL CURRENTLY ?警告:無磁盤的復(fù)制當(dāng)前還處于試驗(yàn)階段。

# -------------------------------------------------------

#

# 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方式復(fù)制數(shù)據(jù)。目前redis復(fù)制提供兩種方式,disk和socket。如果新的slave連上來或者重連的slave無法部分同步,就會(huì)執(zhí)行全量同步,master會(huì)生成rdb文件。有2種方式:disk方式是master創(chuàng)建一個(gè)新的進(jìn)程把rdb文件保存到磁盤,再把磁盤上的rdb文件傳遞給slave。socket是master創(chuàng)建一個(gè)新的進(jìn)程,直接把rdb文件以socket的方式發(fā)給slave。disk方式的時(shí)候,當(dāng)一個(gè)rdb保存的過程中,多個(gè)slave都能共享這個(gè)rdb文件。socket的方式就的一個(gè)個(gè)slave順序復(fù)制。在磁盤速度緩慢,網(wǎng)速快的情況下推薦用socket方式。

#新的slaves和重新連接的slaves,不能繼續(xù)復(fù)制過程只是接收差異,需要做的就是所謂的“完全同步”。 一個(gè)RDB文件從master傳輸?shù)絪laves。

# 傳輸可以以兩種不同的方式發(fā)生:

# 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創(chuàng)建一個(gè)新進(jìn)程,負(fù)責(zé)將RDB文件寫入磁盤。 稍后,該文件由父進(jìn)程以增量方式傳送到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創(chuàng)建一個(gè)新的進(jìn)程,直接將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.

#使用"磁盤支持"的復(fù)制,在生成RDB文件時(shí),只要生成RDB文件,當(dāng)前子進(jìn)程就完成了它的工作,更多的slaves就可以排隊(duì)并被提供這一份RDB文件。(可同時(shí)提供給多個(gè)slaves)

# 使用無盤復(fù)制,一旦傳輸開始,新slaves到達(dá)將排隊(duì),并且當(dāng)當(dāng)前的一個(gè)終止時(shí),新的傳輸將開始。(順序復(fù)制,一個(gè)接一個(gè))

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

#當(dāng)使用“無盤復(fù)制”時(shí),master在開始傳輸之前等待可配置的時(shí)間量(以秒為單位),希望多個(gè)slaves將到達(dá)并且傳輸可以并行化。

#

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

# works better. 使用慢磁盤和快速(大帶寬)網(wǎng)絡(luò),無盤復(fù)制工作更好(建議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復(fù)制的延遲時(shí)間,防止設(shè)置為0。一旦復(fù)制開始,節(jié)點(diǎn)不會(huì)再接收新slave的復(fù)制請(qǐng)求直到下一個(gè)rdb傳輸。所以最好等待一段時(shí)間,等更多的slave連上來。

#當(dāng)啟用無磁盤復(fù)制時(shí),可以配置服務(wù)器等待的延遲,以便產(chǎn)生子進(jìn)程,通過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.

#這是很重要的,因?yàn)橐坏﹤鬏旈_始,不可能服務(wù)新到達(dá)的slaves,它們將為下一個(gè)RDB傳輸排隊(duì),所以服務(wù)器等待一段時(shí)間,以便讓更多的slaves到達(dá)。

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

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

# 延遲以秒為單位指定,默認(rèn)值為5秒。要完全禁用它,僅僅設(shè)置為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以預(yù)定義的時(shí)間間隔向服務(wù)器發(fā)送PING。可以使用repl_ping_slave_period選項(xiàng)更改此時(shí)間間隔。默認(rèn)值為10秒。

#

# repl-ping-slave-period 10

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

復(fù)制連接超時(shí)時(shí)間。master和slave都有超時(shí)時(shí)間的設(shè)置。master檢測(cè)到slave上次發(fā)送的時(shí)間超過repl-timeout,即認(rèn)為slave離線,清除該slave信息。slave檢測(cè)到上次和master交互的時(shí)間超過repl-timeout,則認(rèn)為master離線。需要注意的是repl-timeout需要設(shè)置一個(gè)比repl-ping-slave-period更大的值,不然會(huì)經(jīng)常檢測(cè)到超時(shí)。

# The following option sets the replication timeout for: 設(shè)置復(fù)制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 超時(shí)(data,pings)

# 3) Slave timeout from the point of view of masters (REPLCONF ACK pings). 從masters角度看,slave超時(shí)(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之間的流量低時(shí)都會(huì)檢測(cè)到超時(shí)。

#

# repl-timeout 60

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

是否禁止復(fù)制tcp鏈接的tcp nodelay參數(shù),可傳遞yes或者no。默認(rèn)是no,即使用tcp nodelay。如果master設(shè)置了yes來禁止tcp nodelay設(shè)置,在把數(shù)據(jù)復(fù)制給slave的時(shí)候,會(huì)減少包的數(shù)量和更小的網(wǎng)絡(luò)帶寬。但是這也可能帶來數(shù)據(jù)的延遲。默認(rèn)我們推薦更小的延遲,但是在數(shù)據(jù)量傳輸很大的場(chǎng)景下,建議選擇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將使用較少數(shù)量的TCP數(shù)據(jù)包和較少帶寬將數(shù)據(jù)發(fā)送到從站。但是這可能會(huì)增加數(shù)據(jù)出現(xiàn)在對(duì)端slave上的延遲,對(duì)于Linux內(nèi)核,使用默認(rèn)配置可以延遲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.

#如果選擇“否”,數(shù)據(jù)在slave端出現(xiàn)的延遲將減少,但更多的帶寬將用于復(fù)制

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

#默認(rèn)情況下,我們針對(duì)低延遲進(jìn)行優(yōu)化,但在非常高的流量情況下,或者當(dāng)master和slave相隔許多跳時(shí),將其轉(zhuǎn)換為“yes”可能是個(gè)好主意。

repl-disable-tcp-nodelay no

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

復(fù)制緩沖區(qū)大小,這是一個(gè)環(huán)形復(fù)制緩沖區(qū),用來保存最新復(fù)制的命令。這樣在slave離線的時(shí)候,不需要完全復(fù)制master的數(shù)據(jù),如果可以執(zhí)行部分同步,只需要把緩沖區(qū)的部分?jǐn)?shù)據(jù)復(fù)制給slave,就能恢復(fù)正常復(fù)制狀態(tài)。緩沖區(qū)的大小越大,slave離線的時(shí)間可以更長(zhǎng),復(fù)制緩沖區(qū)只有在有slave連接的時(shí)候才分配內(nèi)存。沒有slave的一段時(shí)間,內(nèi)存會(huì)被釋放出來,默認(rèn)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.

#設(shè)置復(fù)制backlog大小。backlog是一個(gè)緩沖器(buffer)當(dāng)slave斷開一段時(shí)間時(shí)積累slave數(shù)據(jù),以便當(dāng)slave想要再次重新連接時(shí),通常不需要再“完全同步(full resync)”,而是“部分再同步(partial resync)”就足夠了,僅僅傳輸slave在斷開連接時(shí)錯(cuò)失的那部分?jǐn)?shù)據(jù)(余下的)即可。

#

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

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

#復(fù)制backlog越大,slave可以斷開連接的時(shí)間就越長(zhǎng),之后就可以執(zhí)行”部分重新同步(partial resynchronization)“。

# The backlog is only allocated once there is at least a slave connected. 只有在至少有一個(gè)slave連接時(shí)才分配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一段時(shí)間會(huì)釋放復(fù)制緩沖區(qū)的內(nèi)存,repl-backlog-ttl用來設(shè)置該時(shí)間長(zhǎng)度。單位為秒。

#在master已經(jīng)不再連接slave一段時(shí)間后,backlog將被釋放。 以下選項(xiàng)配置需要經(jīng)過的秒數(shù),從最后一個(gè)slave服務(wù)器斷開的時(shí)間開始,backlog緩沖區(qū)被釋放。

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

#

# repl-backlog-ttl 3600

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

# 當(dāng)master不可用,Sentinel會(huì)根據(jù)slave的優(yōu)先級(jí)選舉一個(gè)master。最低的優(yōu)先級(jí)的slave,當(dāng)選master。而配置成0,永遠(yuǎn)不會(huì)被選舉。

# 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優(yōu)先級(jí)是由Redis在INFO輸出中發(fā)布的整數(shù)。 它由Redis Sentinel使用,以便選擇slave,如果master不再正常工作,則升級(jí)為主設(shè)備。

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

#具有低優(yōu)先級(jí)編號(hào)的slave被認(rèn)為更好地用于提升,因此例如如果存在具有優(yōu)先級(jí)10,100,25的三個(gè)從設(shè)備,則哨兵將選擇具有優(yōu)先級(jí)10,即最低的那個(gè)。

# 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的特殊優(yōu)先級(jí)標(biāo)志著slave不能執(zhí)行主設(shè)備的角色,因此優(yōu)先級(jí)為0的從設(shè)備將不會(huì)被Redis Sentinel選擇用于升級(jí)。

# By default the priority is 100.

slave-priority 100

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

#

# 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個(gè)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.

#以秒為單位的滯后,必須小于指定值,根據(jù)從slave接收的最后一次ping計(jì)算,通常每秒發(fā)送一次。

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

#此選項(xiàng)不保證N個(gè)副本將接受寫入,但會(huì)在沒有足夠的slave可用的情況下,將丟失寫入的曝光窗口限制為指定的秒數(shù)。

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

#

#redis提供了可以讓master停止寫入的方式,如果配置了min-slaves-to-write,健康的slave的個(gè)數(shù)小于N,mater就禁止寫入。master最少得有多少個(gè)健康的slave存活才能執(zhí)行寫命令。這個(gè)配置雖然不能保證N個(gè)slave都一定能接收到master的寫操作,但是能避免沒有足夠健康的slave的時(shí)候,master不能寫入來避免數(shù)據(jù)丟失。設(shè)置為0是關(guān)閉該功能。

# min-slaves-to-write 3

# 延遲小于min-slaves-max-lag秒的slave才認(rèn)為是健康的slave

# min-slaves-max-lag 10

#設(shè)置1或另一個(gè)設(shè)置為0禁用這個(gè)特性。

# 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. ?默認(rèn)配置

# 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還使用該信息來發(fā)現(xiàn)slave實(shí)例。 此信息可用的另一個(gè)地方在masterser的“ROLE”命令的輸出中。

#

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

# in the following way:

#通常由slave報(bào)告的列出的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連接使用的套接字的對(duì)等體地址自動(dòng)檢測(cè)地址。

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

#端口:端口在復(fù)制握手期間由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.

#然而,當(dāng)使用端口轉(zhuǎn)發(fā)或網(wǎng)絡(luò)地址轉(zhuǎn)換(NAT)時(shí),slave實(shí)際上可以通過(不同的IP和端口對(duì))來到達(dá)。 slave可以使用以下兩個(gè)選項(xiàng),以便向master報(bào)告一組特定的IP和端口,以便INFO和ROLE將報(bào)告這些值。

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

# the port or the IP address. 如果你需要僅覆蓋端口或IP地址,則沒必要使用這兩個(gè)選項(xiàng)。

#

# slave-announce-ip 5.5.5.5

# slave-announce-port 1234



參考資源:

? 1.redis3.0配置文件詳解?


轉(zhuǎn)載于:https://blog.51cto.com/zcyns/1903276

總結(jié)

以上是生活随笔為你收集整理的Redis主从复制(Master-Slave Replication)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。