mongodb sharding ( replica set + sharding) 移除shard节点

本文详细介绍如何在MongoDB集群中移除一个Shard节点,包括登录Mongos、使用admin数据库、执行removeShard命令及验证移除过程。通过实际操作步骤,展示了shard状态变化,最终确认shard4成功从集群中移除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

os: centos 7.4
monggo: 3.6.6

mongodb replicat set + sharding 规划如下:

192.168.56.101 node1 configserver replset(27017、27018、27019)

192.168.56.102 node2 mongos(27017、27018、27019)

192.168.56.103 node3 shard1 replset(27017、27018、27019)
192.168.56.104 node4 shard2 replset(27017、27018、27019)
192.168.56.105 node5 shard3 replset(27017、27018、27019)
192.168.56.106 node6 shard4 replset(27017、27018、27019)

现在移除一个 shard4

node6 从 mongodb sharding 移除

node2 登录 mongos

$ mongo --port 27017

mongos> use admin
mongos> db.auth('root','rootroot')
mongos> show dbs
mongos> use config
mongos> sh.status()

mongos> db.adminCommand({removeShard:"shard4"})
{
	"msg" : "draining started successfully",
	"state" : "started",
	"shard" : "shard4",
	"note" : "you need to drop or movePrimary these databases",
	"dbsToMove" : [ ],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1532426167, 3),
		"signature" : {
			"hash" : BinData(0,"tp0QX4bjy1hJ4Xt29XDYOKoxwuQ="),
			"keyId" : NumberLong("6581372726341009427")
		}
	},
	"operationTime" : Timestamp(1532426167, 3)
}

mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("5b55bba0b4856e5663e0a7ad")
  }
  shards:
        {  "_id" : "shard1",  "host" : "shard1/192.168.56.103:27017,192.168.56.103:27018,192.168.56.103:27019",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/192.168.56.104:27017,192.168.56.104:27018,192.168.56.104:27019",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/192.168.56.105:27017,192.168.56.105:27018,192.168.56.105:27019",  "state" : 1 }
        {  "_id" : "shard4",  "host" : "shard4/192.168.56.106:27017,192.168.56.106:27018,192.168.56.106:27019",  "state" : 1,  "draining" : true }

可以看到 shard4 显示为 draining ,等把 shard4 分散到其余shard节点后,这个节点就算移除完成了。

##验证

node2 登录mongos

$ mongo --port 27018

mongos> use admin
mongos> db.auth('root','rootroot')
mongos> show dbs
mongos> use config
mongos> db.getCollectionNames()
[
	"actionlog",
	"changelog",
	"chunks",
	"collections",
	"databases",
	"lockpings",
	"locks",
	"migrations",
	"mongos",
	"settings",
	"shards",
	"tags",
	"transactions",
	"version"
]

mongos> db.mongos.find()
{ "_id" : "node2:27017", "advisoryHostFQDNs" : [ ], "mongoVersion" : "3.6.6", "ping" : ISODate("2018-07-23T20:24:55.557Z"), "up" : NumberLong(9356), "waiting" : true }
{ "_id" : "node2:27018", "advisoryHostFQDNs" : [ ], "mongoVersion" : "3.6.6", "ping" : ISODate("2018-07-23T20:24:55.558Z"), "up" : NumberLong(9351), "waiting" : true }
{ "_id" : "node2:27019", "advisoryHostFQDNs" : [ ], "mongoVersion" : "3.6.6", "ping" : ISODate("2018-07-23T20:24:55.559Z"), "up" : NumberLong(9346), "waiting" : true }

mongos> db.shards.find()
{ "_id" : "shard1", "host" : "shard1/192.168.56.103:27017,192.168.56.103:27018,192.168.56.103:27019", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/192.168.56.104:27017,192.168.56.104:27018,192.168.56.104:27019", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/192.168.56.105:27017,192.168.56.105:27018,192.168.56.105:27019", "state" : 1 }

mongos> sh.status()

 

参考:
https://docs.mongodb.com/v3.6/sharding/
https://docs.mongodb.com/v3.6/reference/command/removeShard/#dbcmd.removeShard
http://www.mongoing.com/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据库人生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值