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

On this page本页内容

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.通过赋予副本集成员比该集中任何其他成员更高的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

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注意

Changed in version 4.0.2.在版本4.0.2中更改

If the parameter 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. 如果参数enableElectionHandofftrue(默认值),则当主节点从rs.stepDown()(或不带force: truereplSetStepDown命令)中退出时,退出的主节点将指定一个合格的辅助节点立即调用选举。Otherwise, secondaries can wait up to settings.electionTimeoutMillis before calling an election. 否则,辅助人员可以等待设置settings.electionTimeoutMillis后再进行选举。The stepped down primary does not wait for the effects of the handoff. 降级的主节点不等待切换的影响。For more information, see enableElectionHandoff.有关详细信息,请参阅enableElectionHandoff

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主文件。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. 如果m1.example.net的同步远远落后,m3.example.net不会停止。In that case, 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秒内达到最佳状态,然后退出。This minimizes the amount of time with no primary following failover.这将最大限度地减少故障切换后没有主设备的时间。
    • The step down forces on 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:如果您稍后希望在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数据库命令的包装器。

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

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

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会话中,请在120秒内关闭mongod不符合成为主会话条件的实例:

    rs.stepDown(120)

    mdb1.example.net becomes 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()方法。

←  Perform Maintenance on Replica Set MembersResync a Member of a Replica Set →