Docs HomeMongoDB Manual

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在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. 复制副本集最多可以有一个主副本。[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. 在以下3个成员的复制集中,主复制副本将不可用。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,并且对前一个主要服务器的新写入最终会回滚。