OPTIONS
翻译或纠错本页面

复制集成员

MongoDB的 复制集 是由一组 mongod 实例所组成的,并提供了数据冗余与高可用性。复制集中的成员有以下几种:

Primary.

主节点 接收所有的写操作请求。

Secondaries.

从节点通过应用主节点传来的数据变动操作来保持其数据集与主节点的一致。从节点也可以通过增加额外的参数配置来对应特殊的需求。例如,从节点可以是 non-voting 或是 priority 0

我们也可以为复制集设置一个 投票节点 。投票节点其本身并不包含数据集。但是,一旦当前的主节点不可用时,投票节点就会参与到新的主节点选举的投票中。

一个复制集最多可以拥有12个成员。 [1] 但是同时最多只有其中的7个可以进行投票。

一个复制集至少需要这几个成员:一个 主节点 ,一个 从节点 ,和一个 投票节点 。但是在大多数情况下,我们会保持3个拥有数据集的节点:一个 主节点 和两个 从节点

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.

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

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

All members of the replica set can accept read operations. However, by default, an application directs its read operations to the primary member. See 复制集读选项 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 复制集选举 for more details.

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.

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

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 复制集读选项 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 which of the secondaries becomes the new primary.

See 复制集选举 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 优先级为0的复制集成员.
  • Prevent applications from reading from it, which allows it to run applications that require separation from normal traffic. See 隐藏节点.
  • Keep a running “historical” snapshot for use in recovery from certain errors, such as unintentionally deleted databases. See 延时节点.

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.

重要

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

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 为复制集添加投票节点.

[1]

如果我们选择使用复制集来做业务的解决方案,需要注意的是,一个复制集最多可以拥有12个成员。如果我们需要超过12个成员,那么就需要使用 主-从 复制的架构来实现了。需要注意的是,主-从架构并没有自动故障切换的功能。