mongodb副本集移除已有的节点

 

前一篇文章学习了mongodb副本集添加新节点:mongodb副本集添加新节点,接下来学习下如何移除有问题的节点,先看官方文档:https://docs.mongodb.com/manual/tutorial/remove-replica-set-member/,里面记录了比较详细的步骤,大概如下(移除之前确保业务流量已经从这个已有的节点全部迁移走了):

 

 

用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()方法移除节点

在mongodb4.4之后,rs.config()也可以及时移除一个投票节点,也可以及时添加一个投票节点。

大概步骤如下:

  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.关闭mongodb服务
  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.,连接到primary节点
  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()命泠

    {
        "_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()
    cfg.members.splice(2,1)

     

  5. 加载config生效

    rs.reconfig(cfg)

     

可以看到成员2已经被移除出去了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值