从副本集中删除成员

在本页面

要删除replica set的成员,请使用以下任一过程。

使用 rs.remove()删除成员

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

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

使用 rs.reconfig()删除成员

要删除成员,您可以按照此处所述手动编辑副本集配置文档

Example

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"
}
]
}
cfg = rs.conf()

Example

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

cfg.members.splice(2,1)
rs.reconfig(cfg)

对于上面的示例,输出为:

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