On this page本页内容
You can force a replica set member to become primary by giving it a higher 通过赋予副本集成员比该集中任何其他成员更高的members[n].priority value than any other member in the set.members[n].priority值,可以强制副本集成员成为primary。
Optionally, you also can force a member never to become primary by setting its 可选地,您还可以通过将members[n].priority value to 0, which means the member can never seek election as primary. members[n].priority值设置为0来强制成员永远不能成为主要成员,这意味着该成员永远不能寻求当选为主要成员。For more information, see Priority 0 Replica Set Members.有关详细信息,请参阅优先级0副本集成员。
For more information on priorities, see 有关优先级的更多信息,请参阅members[n].priority.members[n].priority。
A majority of the configured members of a replica set must be available for a set to reconfigure a set or elect a primary. 副本集的大多数已配置成员必须可用于集,以重新配置集或选择主副本集。See Replica Set Elections for more information.有关详细信息,请参阅副本集选择。
Changed in version 4.0.2.在版本4.0.2中更改。
enableElectionHandoff is true (default), when a primary steps down from rs.stepDown()(or the replSetStepDown command without the force: true), the stepped-down primary nominates an eligible secondary to call an election immediately. enableElectionHandoff为true(默认值),则当主节点从rs.stepDown()(或不带force: true的replSetStepDown命令)中退出时,退出的主节点将指定一个合格的辅助节点立即调用选举。settings.electionTimeoutMillis before calling an election. settings.electionTimeoutMillis后再进行选举。enableElectionHandoff.enableElectionHandoff。
This procedure assumes your current primary is 此过程假定当前的primary是m1.example.net and that you'd like to instead make m3.example.net primary. m1.example.net,并且您希望改为m3.example.net主文件。The procedure also assumes you have a three-member replica set with the configuration below. 该过程还假定您有一个具有以下配置的三成员复制副本集。For more information on configurations, see Replica Set Configuration Use.有关配置的更多信息,请参阅副本集配置使用。
This procedure assumes this configuration:此过程采用以下配置:
{
"_id" : "rs",
"version" : 7,
"members" : [
{
"_id" : 0,
"host" : "m1.example.net:27017"
},
{
"_id" : 1,
"host" : "m2.example.net:27017"
},
{
"_id" : 2,
"host" : "m3.example.net:27017"
}
]
}
In a 在连接到主会话的mongosh session that is connected to the primary, use the following sequence of operations to make m3.example.net the primary:mongosh会话中,使用以下操作序列使m3.example.net成为主会话:
cfg = rs.conf() cfg.members[0].priority = 0.5 cfg.members[1].priority = 0.5 cfg.members[2].priority = 1 rs.reconfig(cfg)
The last statement calls 最后一条语句使用修改后的配置文档调用rs.reconfig() with the modified configuration document to configure m3.example.net to have a higher members[n].priority value than the other mongod instances.rs.reconfig(),将m3.example.net配置为具有比其他mongod实例更高的members[n].priority值。
The following sequence of events occur:发生以下事件顺序:
m3.example.net and m2.example.net sync with m1.example.net (typically within 10 seconds).m3.example.net和m2.example.net与m1.example.net同步(通常在10秒内)。m1.example.netm1.example.net does not step down if m3.example.net's sync is far behind. m1.example.net的同步远远落后,m3.example.net不会停止。m1.example.net waits until m3.example.net is within 10 seconds of its optime and then steps down. m1.example.net等待直到m3.example.net在10秒内达到最佳状态,然后退出。m3.example.net becomes primary based on its priority setting.m3.example.net根据其priority设置成为主要候选人。Optionally, if 可选地,如果m3.example.net is more than 10 seconds behind m1.example.net's optime, and if you don't need to have a primary designated within 10 seconds, you can force m1.example.net to step down by running:m3.example.net比m1.example.net的最佳值落后10秒以上,并且如果您不需要在10秒内指定主节点,则可以通过运行以下命令强制m1.example.net退出:
db.adminCommand({replSetStepDown: 86400, force: 1})
This prevents 这可以防止m1.example.net from being primary for 86,400 seconds (24 hours), even if there is no other member that can become primary. m1.example.net在86400秒(24小时)内成为主要成员,即使没有其他成员可以成为主要成员。When 当m3.example.net catches up with m1.example.net it will become primary.m3.example.net赶上m1.example.net时,它将成为主要的。
If you later want to make 如果您稍后希望在m1.example.net primary again while it waits for m3.example.net to catch up, issue the following command to make m1.example.net seek election again:m1.example.net等待m3.example.net赶上时再次使其成为主选项,请发出以下命令使m1.example.net再次寻求选择:
rs.freeze()
The rs.freeze() provides a wrapper around the replSetFreeze database command.rs.freeze()提供了replSetFreeze数据库命令的包装器。
Consider a replica set with the following members:考虑具有以下成员的副本集:
mdb0.example.net - mdb1.example.net - mdb2.example.net - To force a member to become primary use the following procedure:要强制成员成为主要成员,请使用以下步骤:
mongosh, run rs.status() to ensure your replica set is running as expected.mongosh中,运行rs.status()以确保副本集按预期运行。In a 在连接到运行在mongosh session that is connected to the mongod instance running on mdb2.example.net, freeze mdb2.example.net so that it does not attempt to become primary for 120 seconds.mdb2.example.net上的mongod实例的mongosh会话中,冻结mdb2.example.net,使其在120秒内不会尝试成为主会话。
rs.freeze(120)
In a 在连接到运行在mongosh session that is connected to the mongod running on mdb0.example.net, step down this instance that the mongod is not eligible to become primary for 120 seconds:mdb0.example.net上的mongod的mongosh会话中,请在120秒内关闭mongod不符合成为主会话条件的实例:
rs.stepDown(120)
mdb1.example.net becomes primary.成为主要的。
During the transition, there is a short window where the set does not have a primary.在转换过程中,有一个短窗口,集合没有主窗口。
For more information, consider the 有关详细信息,请考虑包装rs.freeze() and rs.stepDown() methods that wrap the replSetFreeze and replSetStepDown commands.replSetFreeze和replSetStepDown命令的rs.freeze()和rs.stepDown()方法。