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. 副本集最多只能有一个主副本。[2]如果当前初选不可用,选举将决定新的初选。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.防止它在选举中成为主要数据中心,这样它就可以驻留在次要数据中心或作为冷备份。请参阅优先级0副本集成员
  • 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. 副本集的次要成员现在记录的oplog条目的应用时间超过了慢速操作阈值。These slow oplog messages: 这些慢速oplog消息:
  • 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.REPL组件下记录,并应用文本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.slowOpSampleRate(慢速操作采样率)的影响。
The profiler does not capture slow oplog entries.分析器不会捕获慢速oplog条目。

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.一个仲裁者只有1票选举权。默认情况下,仲裁器的优先级为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. 如果承载数据的辅助节点不可用,则对分片集群中的分片使用主从仲裁器(PSA)架构可能会导致可用性丧失。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.PSA集群不同于典型的副本集:在分片集群中,分片执行w: majority写关注操作,如果确认操作所需的其余集群成员具有仲裁器,则无法完成这些操作。

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. 某些情况下,副本集中的两个节点可能会暂时认为它们是主节点,但最多其中一个节点能够完成{ w: "majority" }写操作。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. 可以完成{ w: "majority" }写入的节点是当前主节点,另一个节点是尚未识别其降级的前主节点,通常是由于网络分区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.当这种情况发生时,连接到前一个主服务器的客户端可能会观察到过时的数据,尽管已经请求了读取首选项primary,并且对前一个主机的新写入最终将回滚。