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 您可以强制副本集成员成为primary,方法是为其指定比集中任何其他成员都高的members[n].priority
value than any other member in the set.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过程
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 此过程假定您当前的primary是m1.example.net
and that you'd like to instead make m3.example.net
primary. m1.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"
}
]
}
In a在连接到主服务器的mongosh
session that is connected to the primary, use the following sequence of operations to makem3.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 configurem3.example.net
to have a highermembers[n].priority
value than the othermongod
instances.rs.reconfig()
,将m3.example.net
配置为具有比其他mongod
实例更高的members[n].priority
值。The following sequence of events occur:发生以下事件序列:m3.example.net
andm2.example.net
sync withm1.example.net
(typically within 10 seconds).m3.example.net
和m2.example.net
与m1.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 ifm3.example.net
's sync is far behind.m3.example.net
的同步远远落后,则m1.example.net
不会停止。In that case,在这种情况下,m1.example.net
waits untilm3.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 itspriority
setting.m3.example.net
根据其priority
设置成为主要选举。
Optionally, if可选地,如果m3.example.net
is more than 10 seconds behindm1.example.net
's optime, and if you don't need to have a primary designated within 10 seconds, you can forcem1.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 withm1.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 form3.example.net
to catch up, issue the following command to makem1.example.net
seek election again:m3.example.net
赶上时再次将m1.example.net
设为主选项,请发出以下命令使m1.example.net
再次查找选举:rs.freeze()
Thers.freeze()
provides a wrapper around thereplSetFreeze
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:要强制成员成为主要成员,请使用以下过程:
In在mongosh
, runrs.status()
to ensure your replica set is running as expected.mongosh
中,运行rs.status()
以确保复制集按预期运行。In a在连接到在mongosh
session that is connected to themongod
instance running onmdb2.example.net
, freezemdb2.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 themongod
running onmdb0.example.net
, step down this instance that themongod
is not eligible to become primary for 120 seconds:mdb0.example.net
上运行的mongod
的mongosh
会话中,关闭该mongod
在120秒内没有资格成为主要的实例:rs.stepDown(120)
mdb1.example.net
becomes primary.成为primary。NoteDuring 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()
方法。