Docs HomeMongoDB Manual

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. 副本集的最低推荐配置是一个包含三个数据承载成员的三成员副本集:一个primary和两个secondary成员。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在primary上应用写操作,然后将操作记录在primary的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. 要复制数据,辅助进程会在异步进程中将操作从primary的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.辅助设备复制主设备的操作日志,并将操作应用于其数据集。

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, secondary members of a replica set now log oplog entries that take longer than the slow operation threshold to apply. 从4.2版开始,副本集的辅助成员现在会记录应用时间超过慢速操作阈值的oplog条目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.在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, these slow oplog entries are not affected by the slowOpSampleRate. MongoDB logs all slow oplog entries regardless of the sample rate.在MongoDB 4.2中,这些慢速操作日志条目不受slowOpSampleRate的影响。MongoDB记录所有慢速操作日志条目,而不管采样率如何。
    • In MongoDB 4.4 and later, these slow oplog entries are affected by the slowOpSampleRate.在MongoDB 4.4及更高版本中,这些慢速操作日志条目受到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. 仲裁者只有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的客户端可能会观察到过时的数据,尽管请求了读取首选项primary,并且对前一个主要服务器的新写入最终会回滚。