Docs HomeMongoDB Manual

Force a Member to Become Primary强制成员成为主要成员

Overview概述

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.您可以强制副本集成员成为primary,方法是为其指定比集中任何其他成员都高的members[n].priority值。

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

Consideration考虑事项

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.有关详细信息,请参阅副本集选择

Procedures过程

Note

When you step down a primary using rs.stepDown() or replSetStepDown without setting the force field to true, the stepped-down primary nominates an eligible secondary to call an election immediately.当您使用rs.stepDown()replSetStepDown在未将力场设置为true的情况下退出初级时,退出的初级会提名一个合格的secondary立即进行选举。

Force a Member to be Primary by Setting its Priority High通过将成员的优先级设置为高,强制其成为主要成员

This procedure assumes your current primary is m1.example.net and that you'd like to instead make m3.example.net primary. 此过程假定您当前的primarym1.example.net,并且您希望将m3.example.net改为primary。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"
}
]
}
  1. 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.netm2.example.netm1.example.net同步(通常在10秒内)。
    • m1.example.net sees that it no longer has highest priority and, in most cases, steps down. 看到它不再拥有最高优先级,在大多数情况下,它会下台。m1.example.net does not step down if m3.example.net's sync is far behind. 如果m3.example.net的同步远远落后,则m1.example.net不会停止。In that case, m1.example.net waits until m3.example.net is within 10 seconds of its optime and then steps down. This minimizes the amount of time with no primary following failover.在这种情况下,m1.example.net会等待,直到m3.example.net在其最佳值的10秒内,然后退出。这最大限度地减少了没有主故障切换的时间。
    • The step down forces an election in which m3.example.net becomes primary based on its priority setting.下台将强制进行一次选举,其中m3.example.net根据其priority设置成为主要选举。
  2. 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.netm1.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:如果您稍后希望在等待m3.example.net赶上时再次将m1.example.net设为主选项,请发出以下命令使m1.example.net再次查找选举:

    rs.freeze()

    The rs.freeze() provides a wrapper around the replSetFreeze database command.rs.freeze()replSetFreeze数据库命令提供了一个包装。

Force a Member to be Primary Using Database Commands使用数据库命令强制成员成为primary

Consider a replica set with the following members:考虑具有以下成员的复制副本集

  • mdb0.example.net - the current primary.
  • mdb1.example.net - a secondary.
  • mdb2.example.net - a secondary .

To force a member to become primary use the following procedure:要强制成员成为主要成员,请使用以下过程:

  1. In mongosh, run rs.status() to ensure your replica set is running as expected.mongosh中,运行rs.status()以确保复制集按预期运行。
  2. 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)
  3. 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上运行的mongodmongosh会话中,关闭该mongod在120秒内没有资格成为主要的实例:

    rs.stepDown(120)

    mdb1.example.net becomes primary.成为primary。

    Note

    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.有关详细信息,请考虑包装replSetFreezereplSetStepDown命令的rs.freeze()rs.stepDown()方法。