从一个分片集群中移除一个分片,我们会使用 removeShard命令,mongodb首先移动被移除分片的chunks到其他的分片上。然后mongodb 才移除分片。
首先使用removeShard命令需要登陆到mongos,假如你有开启认证,你需要使用集群的管理账户登陆后再使用removeShard命令
每个database再一个分片集群中农有一个primary分片。假如你想移除一个分片也要将primary移除,被迁移的分片所有数据都移走后你必须手动移动databases到一个新的分片上。查看movePrimary命令。
在mongo shell下,使用removeshared操作如下:
use admin
db.runCommand( { removeShard : "shared0001" } )
当运行removeshared命令后,立刻就会有信息返回:
{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "shared0001",
"ok" : 1}
平衡器开始迁移chunks从shared0001分片到集群中其他的分片中。迁移的速度取决于分片的chunks数量。
假如你再次执行removeshared命令,将会返回迁移的进度信息:
{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : NumberLong(3850),
"dbs" : NumberLong(1)
},
"ok" : 1}
当发现chunks为0后,数据块已全部迁移完毕:
{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : NumberLong(0),
"dbs" : NumberLong(1)
},
"ok" : 1}
这时我们可以看到dbs还有一个需要移动,接下来使用db.printShardingStatus()列出dbs,然后再找出需要移走的分片,最后使用
movePrimary移走dbs.
echo 'db.printShardingStatus(true)' | mongo 127.0.0.1:22000/admin -u xxxx-p xxxxx> /tmp/mongo_log.txt
db.runCommand( { movePrimary : "mxm2_android", to : "shard0000" } )
从分片上移除所有的chunks和dbs后,再使用 removeShard查看:
{
"msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "shared0001",
"ok" : 1}
至此,shared0001分片从集群中完全移除。end.
参考链接:https://docs.mongodb.com/v3.0/reference/command/removeShard/
https://docs.mongodb.com/v3.0/reference/command/movePrimary/#dbcmd.movePrimary