Replica Set 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. 副本集最多可以有一个主副本。[1] If the current primary becomes unavailable, an election determines the new primary. 如果当前初选不可用,则选举将决定新的初选。See Replica Set Elections for more details.有关详细信息,请参阅复制集选择

In the following 3-member replica set, the primary becomes unavailable. 在以下三成员副本集中,主副本不可用。This triggers an election which selects one of the remaining secondaries as the new primary.这将触发一次选举,从剩余的二级选举中选择一个作为新的初选。

Diagram of an election of a new primary. In a three member replica set with two secondaries, the primary becomes unreachable. The loss of a primary triggers an election where one of the secondaries becomes the new primary
[1] 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节点,并且对前一个primary节点的新写入最终会回滚。
←  Replica Set MembersReplica Set Secondary Members →