Replication Concepts—複製的概念

資料來源:

http://docs.mongodb.org/manual/core/replica-set-members/

http://docs.mongodb.org/manual/core/replica-set-members/

http://docs.mongodb.org/manual/core/replica-set-secondary/

http://docs.mongodb.org/manual/core/replica-set-priority-0-member/

http://docs.mongodb.org/manual/core/replica-set-hidden-member/

http://docs.mongodb.org/manual/core/replica-set-arbiter/

Replica Set Members—成員介紹

A replica set in MongoDB is a group of mongod processes that provide redundancy and high availability.

The members of a replica set are:

Primary.

The primary receives all write operations.

Secondaries.

Secondaries replicate operations from the primary to maintain an identical data set. Secondaries may have additional configurations for special usage profiles. For example, secondaries may be non-voting or priority 0. You can also maintain an arbiter as part of a replica set. Arbiters do not keep a copy of the data. However, arbiters play a role in the elections that select a primary if the current primary is unavailable.

A replica set can have up to 12 members. [1] However, only 7 members can vote at a time.

The minimum requirements for a replica set are: A primary, a secondary, and an arbiter. Most deployments, however, will keep three members that store data: A primary and two secondary members.

Note

副本集在mongodb是一組mongod的進程,提供冗餘備份與確保高可使用行。

副本集的成員主要有:

主資料庫—Primary:

接受所有的寫入與操作。

次級資料庫—Secondaries:

次級資料庫用來複製Primary的所有資料用來備份。

次級資料庫可能有其他的設定,用來作特殊用途。如:次要資料庫可以設定成無投票權或優先權為0。

你可以可以設定一個仲裁者(arbiter) 在你的副本集裡,仲裁者 是不用保留資料備份的。他發揮的作用是當需要選舉出一個主資料庫時 (當原本的主資料庫掛了),應用在選舉中以順利的選出主資料庫。

一個副本集最多可以有12名成員,但擁有投票權的最多只能有7個。

一個副本集的最低要求要有:一個主資料庫,一個次級資料庫,一個仲裁者。

然而大多都是佈署一台主資料庫、兩台次級資料庫。

Primary

The primary is the only member in the replica set that receives write operations. MongoDB applies write operations on the primary and then records the operations on the primary’s oplog. Secondary members replicate this log and apply the operations to their data sets.

In the following three-member replica set, the primary accepts all write operations. Then the secondaries replicate the oplog to apply to their data sets.

Note

MongoDB中的複製是通過一個日誌來儲存寫入操作的,這個日誌就叫做OPLOG。

Note

主資料庫是在副本集內唯一可以接受讀/寫操作的成員。

mongodb執行寫入操作到主資料庫,紀錄在主資料庫的紀錄檔(oplog)。

次級資料庫根據紀錄檔內容將這些操作寫入到自己的資料庫內。

下方以圖示來表示這三者間的關係。

Mongodb/MongoDB_Replication/./image/replica-set-read-write-operations-primary.png

Diagram of default routing of reads and writes to the primary.

Note

圖示:主、次級資料庫的預設路由

All members of the replica set can accept read operations. However, by default, an application directs its read operations to the primary member. See Read Preference for details on changing the default read behavior.

The replica set can have at most one primary. If the current primary becomes unavailable, an election determines the new primary. See Replica Set Elections for more details.

Note

副本集中的所有成員都可以被讀取資料。但預設只有主資料庫可以被讀取。關於這方面的訊息請閱讀” 讀取設定

Secondaries

A secondary maintains a copy of the primary’s data set. To replicate data, a secondary applies operations from the primary’s oplog to its own data set in an asynchronous process. A replica set can have one or more secondaries.

The following three-member replica set has two secondary members. The secondaries replicate the primary’s oplog and apply the operations to their data sets

Note

次級資料庫,主要進行複製主資料庫的動作,讀取主資料庫的紀錄檔並複製主資料庫的資料到自己的資料庫。

副本集內可以有一個或多個次級資料庫。

下圖為三個副本集成員,其中有兩個為次級資料庫。

以圖示說明次級資料庫與主資料庫的操作與應用。

Mongodb/MongoDB_Replication/./image/replica-set-primary-with-two-secondaries.png

Although clients cannot write data to secondaries, clients can read data from secondary members.

See Read Preference for more information on how clients direct read operations to replica sets.

A secondary can become a primary. If the current primary becomes unavailable, the replica set holds an election to choose with of the secondaries becomes the new primary.

See Replica Set Elections for more details.

Note

客戶端不能寫入資料到次級資料庫,但可以對次級資料庫進行讀取,詳見” 讀取設定

透過 選舉 ,次級資料庫會在主資料庫掛掉的時候成為主資料庫,

You can configure a secondary member for a specific purpose. You can configure a secondary to:

Prevent it from becoming a primary in an election, which allows it to reside in a secondary data center or to serve as a cold standby. See Priority 0 Replica Set Members.

Prevent applications from reading from it, which allows it to run applications that require separation from normal traffic. See Hidden Replica Set Members.

Keep a running “historical” snapshot for use in recovery from certain errors, such as unintentionally deleted databases. See Delayed Replica Set Members.

Arbiter

An arbiter does not have a copy of data set and cannot become a primary. Replica sets may have arbiters to add a vote in elections of for primary. Arbiters allow replica sets to have an uneven number of members, without the overhead of a member that replicates data.

Note

仲裁者,並不會對主資料庫的資料進行複製備份,所以當然也無法成為主資料庫。

副本集在進行主資料庫選舉的時候,如果次級資料庫數目是偶數,可能會讓主資料庫難產,仲裁者就是在此時增加一張選票,使選舉成員成為奇數位順利完成選舉。

Important Do not run an arbiter on systems that also host the primary or the secondary members of the replica set.

Warning

請不要把仲裁者放在副本集內的主、次要資料庫的系統上。

Only add an arbiter to sets with even numbers of members. If you add an arbiter to a set with an odd number of members, the set may suffer from tied elections. To add an arbiter, see Add an Arbiter to Replica Set.

Note

仲裁者添加在可選舉票數為偶數位的副本集上,可以讓選舉順利進行,如果添加在奇數位可能會導致選舉票數對等無法結束選舉, 要新增仲裁者,請參考文件 副本集新增仲裁者

Replica Set Primary—主資料庫配置

The primary is the only member in the replica set that receives write operations. MongoDB applies write operations on the primary and then records the operations on the primary’s oplog. Secondary members replicate this log and apply the operations to their data sets.

In the following three-member replica set, the primary accepts all write operations. Then the secondaries replicate the oplog to apply to their data sets.

Note

主資料庫設定,主資料庫是副本集中唯一可以進行寫入操作的成員,將寫入的操作紀錄在oplog內。

次級資料庫就根據這個oplog的操作來進行複製的動作。

Mongodb/MongoDB_Replication/./image/replica-set-read-write-operations-primary_1.png

Diagram of default routing of reads and writes to the primary.

Note

預設主資料庫的讀取與寫入的路由圖

All members of the replica set can accept read operations. However, by default, an application directs its read operations to the primary member. See Read Preference for details on changing the default read behavior.

The replica set can have at most one primary. If the current primary becomes unavailable, an election determines the new primary. See Replica Set Elections for more details.

In the following 3-member replica set, the primary becomes unavailable. This triggers an election which selects one of the remaining secondaries as the new primary.

Note

副本集內的所有成員都可以進行資料讀取操作,但一開始預設只有主資料庫可以進行讀寫,要改變預設請參考 讀取設定

多數副本集僅允許有一個主資料庫。 如果當前的主資料庫掛掉了,副本集會進行選舉從次級資料庫內選出一個來當主資料庫。更多細節請參見 副本集選舉

下圖中,主資料庫突然掛掉了,副本集將會啟動一場選舉,把其中一個次級資料庫指定成為主資料庫。

Mongodb/MongoDB_Replication/./image/replica-set-trigger-election.png

Diagram of an election of a new primary. In a three member replica set with two secondaries, the primary becomes unreachable. The loss of a primary triggers an election where one of the secondaries becomes the new primary

Note

圖中為副本集進行選舉的示意圖,當失去主資料庫的時候,將會觸發選舉來使兩個次級資料庫中的其中一個成主資料庫。

Replica Set Secondary Members—次級資料庫配置

A secondary maintains a copy of the primary’s data set. To replicate data, a secondary applies operations from the primary’s oplog to its own data set in an asynchronous process. A replica set can have one or more secondaries.

The following three-member replica set has two secondary members. The secondaries replicate the primary’s oplog and apply the operations to their data sets.

Note

次級資料庫主要作的事情,就是根據主資料庫的oplog上所寫的的操作,來複製相關的資料變動至自己的資料庫中。

副本集可以有一個或多個次級資料庫。

下圖為一個主資料庫與兩個次級資料庫的配置,兩個次級資料庫根據oplog的操作輔助複製主資料庫的資料進行數據備份。

Mongodb/MongoDB_Replication/./image/replica-set-primary-with-two-secondaries_1.png

Diagram of a 3 member replica set that consists of a primary and two secondaries.

Although clients cannot write data to secondaries, clients can read data from secondary members. See Read Preference for more information on how clients direct read operations to replica sets.

A secondary can become a primary. If the current primary becomes unavailable, the replica set holds an election to choose with of the secondaries becomes the new primary.

In the following three-member replica set, the primary becomes unavailable. This triggers an election where one of the remaining secondaries becomes the new primary.

Note

雖然客戶端不可以直接把數據寫入到次級資料庫中,但可以從次級資料庫中讀取資料。如何從次級資料庫中讀取資料,請參考 讀取設定

下圖的三個副本集成員會在主資料庫掛掉之後觸發選舉,剩下的兩個次級資料庫會有一個成為主資料庫。

Mongodb/MongoDB_Replication/./image/replica-set-trigger-election_1.png

Diagram of an election of a new primary. In a three member replica set with two secondaries, the primary becomes unreachable. The loss of a primary triggers an election where one of the secondaries becomes the new primary

See Replica Set Elections for more details.

You can configure a secondary member for a specific purpose. You can configure a secondary to:

Prevent it from becoming a primary in an election, which allows it to reside in a secondary data center or to serve as a cold standby. See Priority 0 Replica Set Members.

Prevent applications from reading from it, which allows it to run applications that require separation from normal traffic. See Hidden Replica Set Members.

Keep a running “historical” snapshot for use in recovery from certain errors, such as unintentionally deleted databases. See Delayed Replica Set Members.

Note

參考 副本集詳細設定

你可以將次級資料庫作為特殊的用途進行其他的設定:

防止他參與選舉,讓他成為永久的次級資料庫作為永久備份使用,優先級可以設定成0。

防止被讀取,可以將其設定成隱藏模式。

可以進行時間差(延遲備份)複製,如主資料庫中不小心在操作上出現嚴重的資料錯誤,可以用來進行資料回溯。參考 延遲副本集

Priority 0 Replica Set Members—優先權0的副本集成員

A priority 0 member is a secondary that cannot become primary. Priority 0 members cannot trigger elections. Otherwise these members function as normal secondaries. A priority 0 member maintains a copy of the data set, accepts read operations, and votes in elections. Configure a priority 0 member to prevent secondaries from becoming primary, which is particularly useful in multi-data center deployments.

In a three-member replica set, in one data center hosts the primary and a secondary. A second data center hosts one priority 0 member that cannot become primary.

Note

次級資料庫的優先權設定成0之後就無法成為主資料庫了。

優先權為0的成員無法觸發選舉,除此之外這些成員就是一個正常功能的次級資料庫。

優先權0的成員,可以正常進行複製的副本集功能,接受讀取操作,設定成優先權0的成員可以防止其成為主資料庫, 這是特別有用的多數據中心佈署。

下圖,在一個由三個成員組成的副本集中,一個主資料庫與次級資料庫在同一個數據中心,另一個數據中心的次級資料庫設定優先權為0。

Mongodb/MongoDB_Replication/./image/replica-set-three-members-geographically-distributed.png

Diagram of a 3 member replica set distributed across two data centers. Replica set includes a priority 0 member.

Priority 0 Members as Standbys—作為備份使用

A priority 0 member can function as a standby.

In some replica sets, it might not be possible to add a new member in a reasonable amount of time. A standby member keeps a current copy of the data to be able to replace an unavailable member.

In many cases, you need not set standby to priority 0. However, in sets with varied hardware or geographic distribution, a priority 0 standby ensures that only qualified members become primary.

A priority 0 standby may also be valuable for some members of a set with different hardware or workload profiles. In these cases, deploy a member with priority 0 so it can’t become primary. Also consider using an hidden member for this purpose.

If your set already has seven voting members, also configure the member as non-voting.

Note

在一些副本集中,有可能會出現沒辦法在適當時間新增成員的狀況,

在許多情況下,不需要把次級資料庫成員設定成優先權零,你可以依據你所需要的設計來指定各個次級資料庫的優先權大小。

在不同的硬體上或是資料庫地理位置不同的情形時,設定優先權0是用來作為純資料備份使用,確保你設計上在在故障發生時, 要取代原先主資料庫的次級資料庫能順利頂上。

優先權0的備份優點是可以確保你的主資料庫不管如何變更都在你想要的數據中心內。

當然你也可以把次級資料庫設定成隱藏來作此用途。

如果你的副本集已經有七個有投票權的成員,而且也配置了無投票權的成員

Note

設定無投票權 可以在設定檔內寫入 votes=0 ,如下

{
  "_id" : <num>
  "host" : <hostname:port>,
  "votes" : 0
}

Priority 0 Members and Failover When configuring a priority 0 member, consider potential failover patterns, including all possible network partitions. Always ensure that your main data center contains both a quorum of voting members and contains members that are eligible to be primary.

Configuration To configure a priority 0 member, see Prevent Secondary from Becoming Primary.

Note

優先權0成員和故障轉移

配置優先權0成員時,要考慮到潛在的故障轉移的模式,包含所有可能使用到的網路分區。

請務必確保你的主要數據中心內的成員有達到副本集規定的成員數(投票權的成員),這些成員要符合能成為主資料庫的資格。

設定

關於設定優先權0成員的詳細設定方式,請參考 防止成員成為主資料庫

Hidden Replica Set Members—隱藏副本集成員

A hidden member maintains a copy of the primary’s data set but is invisible to client applications. Hidden members are ideal for workloads with different usage patterns from the other members in the replica set. Hidden members are also priority 0 members and cannot become primary. The db.isMaster() method does not display hidden members. Hidden members, however, do vote in elections.

In the following five-member replica set, all four secondary members have copies of the primary’s data set, but one of the secondary members is hidden.

Note

隱藏的成員會留存一份資料在主資料庫內,但客戶端訪問的時候是看不到的。

隱藏成員同時也是優先權0的成員,並不能成為主資料庫。

使用 db.isMaster() 的方法無法顯示出隱藏的成員。

隱藏的成員擁有投票權

下圖,由五個成員組成的副本集中,有一個次要成員是設定成隱藏的。

Mongodb/MongoDB_Replication/./image/replica-set-hidden-member.png

Diagram of a 5 member replica set with a hidden priority 0 member.

Secondary reads do not reach a hidden member, so the member receives no traffic beyond what replication requires. It can be useful to keep a hidden member dedicated to reporting or to do backups.

For dedicated backup, ensure that the hidden member has low network latency to the primary or likely primary. Ensure that the replication lag is minimal or non-existent.

Avoid stopping the mongod process of a hidden members. Instead, for filesystem snapshots, use db.fsyncLock() to flush all writes and lock the mongod instance for the duration of the backup.

For more information about backing up MongoDB databases, see Backup Strategies for MongoDB Systems. To configure a hidden member, see Configure a Hidden Replica Set Member.

Note

隱藏成員與一般的次級成員的使用模式是不同的,通常會將其當成提供報告與備份專用。

對於用來備份專用,要注意與主資料庫的連結不能有太多的網路延遲(lag),儘量讓延遲剪到最低會不存在。

配置隱藏節點請參考這篇