Remove Members from Replica Set从副本集中删除成员
On this page本页内容
To remove a member of a replica set use either of the following procedures.要删除复制副本集的成员,请使用以下任一过程。
Remove a Member Using rs.remove()
使用rs.remove()
删除成员
rs.remove()
Shut down the关闭要删除的成员的mongod
instance for the member you wish to remove.mongod
实例。To shut down the instance, connect using要关闭实例,请使用mongosh
and use thedb.shutdownServer()
method.mongosh
连接并使用db.shutdownServer()
方法。Connect to the replica set's current primary.连接到复制副本集的当前primary。To determine the current primary, use要确定当前的主节点,请在连接到副本集的任何成员时使用db.hello()
while connected to any member of the replica set.db.hello()
。Use使用以下任一形式的rs.remove()
in either of the following forms to remove the member:rs.remove()
来删除成员:rs.remove("mongod3.example.net:27017")
rs.remove("mongod3.example.net")MongoDB may disconnect the shell briefly if the replica set needs to elect a new primary.如果副本集需要选择一个新的主副本,MongoDB可能会短暂断开shell的连接。The shell then automatically reconnects in such cases. The shell may display a在这种情况下,shell会自动重新连接。即使命令成功,shell也可能显示DBClientCursor::init call() failed
error even though the command succeeds.DBClientCursor::init call() failed
错误。
Remove a Member Using rs.reconfig()
使用rs.reconfig()
删除成员
rs.reconfig()
You can remove a member by reconfiguring the replica set using a replica configuration document where that member is removed from the 您可以通过使用副本配置文档重新配置副本集来删除成员,其中该成员已从members
array.members
数组中删除。
Starting in MongoDB 4.4, 从MongoDB 4.4开始,rs.reconfig()
allows adding or removing no more than 1
voting
member at a time. rs.reconfig()
允许一次添加或删除不超过1个投票成员。To remove multiple voting members from the replica set, issue a series of 要从副本集中删除多个有投票权的成员,请执行一系列rs.reconfig()
operations to remove one member at a time. rs.reconfig()
操作,一次删除一个成员。See Reconfiguration Can Add or Remove No More than One Voting Member at a Time for more information.有关详细信息,请参阅重新配置一次最多可添加或删除一个投票成员。
Procedure过程
Shut down the关闭要删除的成员的mongod
instance for the member you wish to remove.mongod
实例。To shut down the instance, connect using要关闭实例,请使用mongosh
and use thedb.shutdownServer()
method.mongosh
连接并使用db.shutdownServer()
方法。Connect to the replica set's current primary.连接到复制副本集的当前primary。To determine the current primary, use要确定当前的主节点,请在连接到副本集的任何成员时使用db.hello()
while connected to any member of the replica set.db.hello()
。Issue the发出rs.conf()
method to view the current configuration document and determine the position in themembers
array of the member to remove:rs.conf()
方法以查看当前配置文档,并确定要删除的成员在成员数组中的位置:Examplemongod_C.example.net
is in position位于以下配置文件的位置2
of the following configuration file:2
:{
"_id" : "rs",
"version" : 7,
"members" : [
{
"_id" : 0,
"host" : "mongod_A.example.net:27017"
},
{
"_id" : 1,
"host" : "mongod_B.example.net:27017"
},
{
"_id" : 2,
"host" : "mongod_C.example.net:27017"
}
]
}Assign the current configuration document to the variable将当前配置文档分配给变量cfg
:cfg
:cfg = rs.conf()
Modify the修改cfg
object to remove the member.cfg
对象以删除成员。ExampleTo remove要删除mongod_C.example.net:27017
use the following JavaScript operation:mongod_C.example.net:27017
,请使用以下JavaScript操作:cfg.members.splice(2,1)
Overwrite the replica set configuration document with the new configuration by issuing the following:通过发出以下命令,用新配置覆盖副本集配置文档:rs.reconfig(cfg)
To confirm the new configuration, issue要确认新配置,请发出rs.conf()
.rs.conf()
。For the example above the output would be:对于上述示例,输出为:{
"_id" : "rs",
"version" : 8,
"members" : [
{
"_id" : 0,
"host" : "mongod_A.example.net:27017"
},
{
"_id" : 1,
"host" : "mongod_B.example.net:27017"
}
]
}