对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

从自管理副本集中删除成员

要删除副本集中的成员,请使用以下任一程序。

  1. Shut down the mongod instance for the member you wish to remove. To shut down the instance, connect using mongosh and use the db.shutdownServer() method.

  2. 连接到副本集的当前主节点。要确定当前主节点,请在连接到副本集的任何成员时使用 db.hello()。

  3. 使用以下任一形式的 rs.remove() 删除成员:

    rs.remove("mongod3.example.net:27017")
    rs.remove("mongod3.example.net")

    如果副本集需要选择新的主节点,MongoDB 可能会短暂断开 Shell。在这种情况下,Shell 会自动重新连接。即使命令成功执行,Shell 也可能会显示 DBClientCursor::init call() failed 错误。

您可以通过使用副本配置文档重新配置副本集来删除成员,其中该成员已从 members 数组中删除。

rs.reconfig() 允许一次添加或删除不超过 1 voting 个节点。要从副本集中删除多个投票节点,可执行一系列 rs.reconfig() 操作,每次删除一个节点。请参阅重新配置只能添加或删除不超过一个投票节点,了解更多信息。

  1. Shut down the mongod instance for the member you wish to remove. To shut down the instance, connect using mongosh and use the db.shutdownServer() method.

  2. 连接到副本集的当前主节点。要确定当前主节点,请在连接到副本集的任何成员时使用 db.hello()。

  3. 使用 rs.conf() 方法查看当前配置文件,确定要删除的成员在 members 数组中的位置:

    例子

    mongod_C.example.net 位于以下配置文件的位置 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. 将当前配置文档分配给变量cfg:

    cfg = rs.conf()
  5. 修改 cfg 对象,删除成员。

    例子

    要删除mongod_C.example.net:27017,请使用以下 JavaScript 操作:

    cfg.members.splice(2,1)
  6. 通过发出以下命令,使用新配置覆盖副本集配置文档:

    rs.reconfig(cfg)
  7. 要确认新配置,请执行 rs.conf()。

    对于上面的示例,输出将是:

    {
    "_id" : "rs",
    "version" : 8,
    "members" : [
    {
    "_id" : 0,
    "host" : "mongod_A.example.net:27017"
    },
    {
    "_id" : 1,
    "host" : "mongod_B.example.net:27017"
    }
    ]
    }