On this page本页内容
To remove a member of a replica set use either of the following procedures.要删除副本集的成员,请使用以下任一过程。
rs.remove()
rs.remove()
删除成员mongod
instance for the member you wish to remove. mongod
实例。mongosh
and use the db.shutdownServer()
method.mongosh
连接并使用db.shutdownServer()
方法。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可能会暂时断开外壳。The shell then automatically reconnects in such cases. 在这种情况下,外壳会自动重新连接。The shell may display a 即使命令成功,shell也可能显示DBClientCursor::init call() failed
error even though the command succeeds.DBClientCursor::init call() failed
失败错误。
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
个voting
成员。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.有关详细信息,请参阅重新配置可一次添加或删除不超过一个投票成员。
mongod
instance for the member you wish to remove. mongod
实例。mongosh
and use the db.shutdownServer()
method.mongosh
连接并使用db.shutdownServer()
方法。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 the members
array of the member to remove:rs.conf()
方法以查看当前配置文档,并确定要删除的成员在members
数组中的位置:
mongod_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
对象以删除成员。
To 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" } ] }