前一篇文章学习了mongodb副本集添加新节点:mongodb副本集添加新节点,接下来学习下如何移除有问题的节点,先看官方文档:https://docs.mongodb.com/manual/tutorial/remove-replica-set-member/,里面记录了比较详细的步骤,大概如下(移除之前确保业务流量已经从这个已有的节点全部迁移走了):
用rs.remove()方法移除:
- Shut down the
mongod
instance for the member you wish to remove. To shut down the instance, connect using themongo
shell and thedb.shutdownServer()
method. - 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. -
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()也可以及时移除一个投票节点,也可以及时添加一个投票节点。
大概步骤如下:
- Shut down the
mongod
instance for the member you wish to remove. To shut down the instance, connect using themongo
shell and thedb.shutdownServer()
,method.关闭mongodb服务 - 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节点 -
Issue the
rs.conf()
method to view the current configuration document and determine the position in themembers
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" } ] }
-
开始操作,拿到cfg对象,移除
cfg = rs.conf() cfg.members.splice(2,1)
-
加载config生效
rs.reconfig(cfg)
可以看到成员2已经被移除出去了。