mongodb分片集群之移除分片
参考官方文档:
这里写链接内容
1、查看balancer是否开启
mongos> sh.getBalancerState()
true
2、查找要删除的分片id
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5ab2d4f47692cf09f088f952")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.31.59:22001" }
{ "_id" : "shard0001", "host" : "192.168.31.16:22002", "draining" : true }
{ "_id" : "shard0002", "host" : "192.168.31.118:22003" }
active mongoses:
要删除的ip服务器192.168.31.16,对应id :shard0001
3、执行移除分片
mongos> db.runCommand( { removeShard: "shard0001" } )
{
"msg" : "draining started successfully",#抽水成功开始
"state" : "started",
"shard" : "shard0001",
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [ ],
"ok" : 1
}
4、查看
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5ab2d4f47692cf09f088f952")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.31.59:22001" }
{ "_id" : "shard0001", "host" : "192.168.31.16:22002", "draining" : true }
{ "_id" : "shard0002", "host" : "192.168.31.118:22003" }
active mongoses:
"3.2.18-3.9" : 1
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "ntrailcenter", "primary" : "shard0000", "partitioned" : true }
{ "_id" : "testdb", "primary" : "shard0000", "partitioned" : true }
testdb.table1
shard key: { "id" : 1 }
unique: false
balancing: false
chunks:
shard0000 1
shard0001 1
shard0002 1
{ "id" : { "$minKey" : 1 } } -->> { "id" : 2 } on : shard0001 Timestamp(2, 0)
{ "id" : 2 } -->> { "id" : 20 } on : shard0002 Timestamp(3, 0)
{ "id" : 20 } -->> { "id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(3, 1)
5、检查迁移的状态
检查迁移的状态,再次在 admin 数据库运行 removeShard 命令,
mongos> db.runCommand( { removeShard: "shard0001" } )
{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : NumberLong(1),
"dbs" : NumberLong(0)
},
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [ ],
"ok" : 1
}
6、移除未分片数据
迁移没有分片的数据
如果这个分片是一个或多个数据库的 primary shard ,上面会存储没有分片的数据,如果不是,则跳过 完成迁移 任务.
在集群中,没有分片的数据库只会将数据存放在一个分片上,这个分片就是这个数据库的主分片.(不同的数据库可以有不同的主分片.)
警告:
Do not perform this procedure until you have finished draining the shard.
To determine if the shard you are removing is the primary shard for any of the cluster’s databases, issue one of the following methods:
sh.status()
db.printShardingStatus()
在返回的文档中, databases 字段列出了所有数据库和它的主分片.比如,以下的 databases 字段显示了 products 数据库使用 mongodb0 作为主分片.
{ "_id" : "products", "partitioned" : true, "primary" : "mongodb0" }
将数据库迁移到另一个分片,需要使用 movePrimary 命令.使用如下命令将所有的剩余的未分片的数据从 mongodb0 迁移到 mongodb1 上.
db.runCommand( { movePrimary: "products", to: "mongodb1" })
This command does not return until MongoDB completes moving all data, which may take a long time. The response from this command will resemble the following:
{ "primary" : "mongodb1", "ok" : 1 }
警告
这个命令直到全部数据迁移完成才会返回,可能会花费较长时间.最后返回的结果类似这样
6、迁移完成
mongos> use admin
switched to db admin
mongos> db.runCommand( { removeShard: "shard0001" } )
{
"msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "shard0001",
"ok" : 1
}
如果state为 completed,表示已完成迁移。