Database Manual / Replication

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:MongoDB中的副本集是一组提供冗余和高可用性的mongod进程。副本集的成员包括:

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.次要人员从主要人员复制操作以维护相同的数据集。借调人员可能有特殊使用配置的额外配置。例如,次级可能是无投票权优先级0

The minimum recommended configuration for a replica set is a three member replica set with three data-bearing members: one primary and two secondary members. 副本集的最低建议配置是一个由三个成员组成的副本集,其中有三个数据承载成员:一个主要成员和两个次要成员。In some circumstances (such as you have a primary and a secondary but cost constraints prohibit adding another secondary), you may choose to include an arbiter. 在某些情况下(例如您有一个主要和一个次要,但成本限制禁止添加另一个次要),您可以选择包含一个仲裁器An arbiter participates in elections but does not hold data (i.e. does not provide data redundancy).仲裁器参与选举,但不保存数据(即不提供数据冗余)。

A replica set can have up to 50 members but only 7 voting members.一个复制集最多可以有50个成员,但只有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.主副本是副本集中唯一接收写入操作的成员。MongoDB在primary上应用写操作,然后在primary的oplog上记录这些操作。secondary成员复制此日志并将操作应用于其数据集。

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.在以下三个成员的副本集中,主副本接受所有写入操作。然后,次级服务器复制oplog以应用于它们的数据集。

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 Read Preference for details on changing the default read behavior.

The replica set can have at most one primary. [2] If the current primary becomes unavailable, an election determines the new primary. See Replica Set Elections 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. [1] A replica set can have one or more secondaries.辅助服务器维护primary's数据集的副本。为了复制数据,辅助服务器在异步进程中将主服务器oplog中的操作应用于其自己的数据集。[1]副本集可以有一个或多个次级。

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.以下三个成员的副本集有两个次要成员。次级服务器复制初级服务器的oplog,并将操作应用于它们的数据集。

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 which 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.
[1] Secondary members of a replica set now log oplog entries that take longer than the slow operation threshold to apply. These slow oplog messages:
  • Are logged for the secondaries in the diagnostic log.
  • Are logged under the REPL component with the text applied op: <oplog entry> took <num>ms.
  • Do not depend on the log levels (either at the system or component level)
  • Do not depend on the profiling level.
  • Are affected by slowOpSampleRate.
The profiler does not capture slow oplog entries.

Arbiter

In some circumstances (such as when you have a primary and a secondary, but cost constraints prohibit adding another secondary), you may choose to add an arbiter to your replica set. An arbiter participates in elections for primary but an arbiter does not have a copy of the data set and cannot become a primary.

An arbiter has exactly 1 election vote. By default an arbiter has priority 0.

Important

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

Warning

Using a primary-secondary-arbiter (PSA) architecture for shards in a sharded cluster can cause a loss of availability if a data-bearing secondary is unavailable. A PSA cluster differs from a typical replica set: In a sharded cluster, shards perform w: majority write concern operations that cannot complete if the remaining cluster members required to confirm an operation have an arbiter.

To add an arbiter, see Add an Arbiter to a Self-Managed Replica Set.

For considerations when using an arbiter, see Replica Set Arbiter.

[2] In some circumstances, two nodes in a replica set may transiently believe that they are the primary, but at most, one of them will be able to complete writes with { w: "majority" } write concern. The node that can complete { w: "majority" } writes is the current primary, and the other node is a former primary that has not yet recognized its demotion, typically due to a network partition. When this occurs, clients that connect to the former primary may observe stale data despite having requested read preference primary, and new writes to the former primary will eventually roll back.