Replica Set Members副本集成员

On this page本页内容

A replica set in MongoDB is a group of mongod processes that provide redundancy and high availability. MongoDB中的副本集是一组提供冗余和高可用性的mongod进程。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.例如,辅助设备可以是无投票权的,也可以是优先级为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. MongoDB在主服务器上应用写操作,然后在主服务器的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.然后,二级数据库复制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. 辅助服务器维护primary数据集的副本。To replicate data, a secondary applies operations from the primary's oplog to its own data set in an asynchronous process. 为了复制数据,辅助服务器在异步过程中将主服务器的oplog中的操作应用于自己的数据集。[1] 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.二级数据库复制主数据库的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] Starting in version 4.2 (also available starting in 4.0.6), secondary members of a replica set now log oplog entries that take longer than the slow operation threshold to apply. 从4.2版开始(从4.0.6版开始也可用),副本集的辅助成员现在记录的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.不要依赖于分析级别。
  • May be affected by slowOpSampleRate, depending on your MongoDB version:可能会受到slowOpSampleRate的影响,具体取决于您的MongoDB版本:
    • In MongoDB 4.2 and earlier, these slow oplog entries are not affected by the slowOpSampleRate. 在MongoDB 4.2及更早版本中,这些慢oplog条目不受慢slowOpSampleRate的影响。MongoDB logs all slow oplog entries regardless of the sample rate.无论采样率如何,MongoDB都会记录所有慢操作日志条目。
    • In MongoDB 4.4 and later, these slow oplog entries are affected by the slowOpSampleRate.在MongoDB 4.4及更高版本中,这些慢oplog条目受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.仲裁器参与primary选举,但仲裁员没有数据集的副本,不能成为primary。

An arbiter has exactly 1 election vote. 一个仲裁者只有1张选举票。By default an arbiter has priority 0.默认情况下,仲裁者的优先级为0

Important重要

Do not run an arbiter on systems that also host the primary or the secondary members of the replica set.不要在同时承载副本集的主要或次要成员的系统上运行仲裁器。

To add an arbiter, see Add an Arbiter to 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,并且对前一个主节点的新写入最终会回滚。
←  ReplicationReplica Set Primary →