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()删除成员

  1. Shut down the mongod instance for the member you wish to remove. 关闭要删除的成员的mongod实例。To shut down the instance, connect using mongosh and use the db.shutdownServer() method.要关闭实例,请使用mongosh连接并使用db.shutdownServer()方法。
  2. Connect to the replica set's current primary. 连接到副本集的当前primaryTo determine the current primary, use db.hello() while connected to any member of the replica set.要确定当前的主对象,请在连接到副本集的任何成员时使用db.hello()
  3. 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 DBClientCursor::init call() failed error even though the command succeeds.即使命令成功,shell也可能显示DBClientCursor::init call() failed失败错误。

Remove a Member Using 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, rs.reconfig() allows adding or removing no more than 1 voting member at a time. 从MongoDB 4.4开始,rs.reconfig()允许一次添加或删除不超过1voting成员。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过程

  1. Shut down the mongod instance for the member you wish to remove. 关闭要删除的成员的mongod实例。To shut down the instance, connect using mongosh and use the db.shutdownServer() method.要关闭实例,请使用mongosh连接并使用db.shutdownServer()方法。
  2. Connect to the replica set's current primary. 连接到副本集的当前primaryTo determine the current primary, use db.hello() while connected to any member of the replica set.要确定当前的主对象,请在连接到副本集的任何成员时使用db.hello()
  3. 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数组中的位置:

    Example示例

    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"
            }
        ]
    }
  4. Assign the current configuration document to the variable cfg:将当前配置文档分配给变量cfg

    cfg = rs.conf()
  5. Modify the cfg object to remove the member.修改cfg对象以删除成员。

    Example示例

    To remove mongod_C.example.net:27017 use the following JavaScript operation:要删除mongod_C.example.net:27017,请使用以下JavaScript操作:

    cfg.members.splice(2,1)
  6. Overwrite the replica set configuration document with the new configuration by issuing the following:通过发出以下命令,用新配置覆盖副本集配置文档:

    rs.reconfig(cfg)
  7. 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"
            }
        ]
    }
←  Add Members to a Replica SetReplace a Replica Set Member →