Replica Set从副本集中删除成员

本文介绍两种从MongoDB副本集中删除成员的方法:使用rs.remove()直接移除成员及使用rs.reconfig()重新配置副本集以排除指定成员。文章详细展示了操作步骤与示例。

一、使用rs.remove()删除成员

1.Shut down the mongod instance for the member you wish to remove. To shut down the instance, connect using the mongo shell and the db.shutdownServer() method.
2.Connect to the replica set’s current primary. To determine the current primary, use db.isMaster() while connected to any member of the replica set.
3.Use rs.remove() in either of the following forms to remove the member:

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

二、使用rs.reconfig()删除成员

1.Shut down the mongod instance for the member you wish to remove. To shut down the instance, connect using the mongo shell and the db.shutdownServer() method.
2.Connect to the replica set’s current primary. To determine the current primary, use db.isMaster() while connected to any member of the replica set.
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:

例如:
{
    "_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()
修改cfg变量以删除成员:cfg.members.splice(2,1)
通过发布以下新配置来覆盖副本集配置文档:rs.reconfig(cfg)
rs.conf()
{
    "_id" : "rs",
    "version" : 8,
    "members" : [
        {
            "_id" : 0,
            "host" : "mongod_A.example.net:27017"
        },
        {
            "_id" : 1,
            "host" : "mongod_B.example.net:27017"
        }
    ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值