MongoDB的Shard集群来说,添加一个分片很简单,AddShard就可以了。
但是缩减集群(删除分片)这种一般很少用到。由于曙光的某服务器又挂了,所以我们送修之前必须把它上面的数据自动迁移到其他Shard上。
1、执行RemoveShard命令
1 |
db.runCommand( { removeshard: "your_shard_name" } )
|
3 |
{ msg : "draining started successfully" , state: "started" , shard : "mongodb0" , ok : 1 }
|
上面这句会立即返回,实际在后台执行。
2、查看迁移状态
我们可以反复执行上面语句,查看执行结果。
1 |
db.runCommand( { removeshard: "your_shard_name" } )
|
3 |
{ msg: "draining ongoing" , state: "ongoing" , remaining: { chunks: 42, dbs : 1 }, ok: 1 }
|
从上面可以看到,正在迁移,还剩下42块没迁移完。
当remain为0之后,这一步就结束了。
3、移出非Shard数据
1 |
db.runCommand( { movePrimary: "myapp" , to: "mongodb1" })
|
这次就不是立即返回了,需要很久,然后会返回如下:
1 |
{ "primary" : "mongodb1" , "ok" : 1 }
|
4、最后的清理
上面步骤都完成后,还需要再执行一次RemoveShard,清理残余数据。
1 |
db.runCommand( { removeshard: "mongodb0" } )
|
执行成功后,会如下结果:
1 |
{ msg: "remove shard completed succesfully" , stage: "completed" , host: "mongodb0" , ok : 1 }
|
显示completed后,就可以安心的关闭mongod的进程了。
一些遇到的问题:
Popgo: In my MongoDB sharding cluster, i used db.runCommand( { removeshard: "shard3" } )
command to remove a shard "shard3", but it just puts the shard in
draining mode, which will never end and three days past. All chunks of
the shard "shard3" are moved into "shard1" or "shard2", but the APP read
request can use it to query data. So i want to know which process of
the sharding now or how to diagnose this problem?
MrKurt:You probably have a database using shard3
as its primary shard, check the output of db.printShardingStatus()
to see where each database resides.
Once you figure out which database it is, move it to a different shard with the movePrimary
command. Then run removeShard
again and you should be good to go.
Popgo:
Andre: You do need to run movePrimary to complete
draining in the case of a db being on the primary. If you look at the
results of the removeshard
and see "chunks" : NumberLong(0)
but "dbs": NumberLong(
some number other than 0 )
you probably have a DB whose primary is the shard you are removing and will probably have to run movePrimary even though removeshard
hasn't said "draining is complete". Check out this discussion on mongodb-user for more information:
相关的官方文档:http://docs.mongodb.org/manual/tutorial/remove-shards-from-cluster/
注意官方关于是否需要运行movePrimary的说明:
Databases with non-sharded collections store those collections on a
single shard known as the primary shard for that database. The
following step is necessary only when the shard to remove is
also the primary shard for one or more databases.
也就是说,如果在这个片上有非分片的collection,这样的话,分片的数据合到其他片上了,那么剩下的非分片数据,没法通过合并分片的方式合到其他服务器上,所以这时要运行一个movePrimary命令将这些非分片的数据移到另一个服务器上。db.runCommand( { movePrimary: "myapp", to: "mongodb1" })
This command migrates all remaining non-sharded data in the
database named myapp to the shard named mongodb1
还有一个官方说明需要注意的是:
Warning
Do not run the movePrimary until you have finished
draining the shard
也就是说,一定要等到分片数据迁移完了,再运行movePrimary命令!!!
而且这句命令不像removeshard是异步的,这个movePrimary命令会等到将所有非分片数据都移到其他服务器后,才响应,所以时间有可能会比较长,主要还是看这个服务器上,非分片数据有多少。
另外,movePrimary执行完后,还记得将db.runCommand({removeshard:"shardx"})再运行一遍,直到看到如下结果{ msg: "remove shard completed successfully" , stage: "completed", host: "mongodb0", ok : 1 }
到此为止,迁移才真正完成,可以放心地关闭mongod。
about how to identify the non-sharding databases int server2, to run:db.printShardingStatus();
references:
If you need to figure out which database the removeshard output refers
to, you can use the printShardingStatus command. It will tell you what
is the "primary" shard for each non-partitioned database.
If any of the DBs have the drained shard listed as the primary, you
will need to issue a move primary command (again listed on the page
linked).
最后一个疑问是:在迁移的过程中,mongos是打到哪个分片上?迁移的具体执行是copy还是move?比如从服务器B合到A,在这个过程中,流量是打到B,还是全打到A?已合到A的分块去A中取,还是从B中取?
还有一个问题:如果总共就两个分片,将一个分片合回到新分片后,相关collections的分片策略可以更改了吗?因为分片的策略一旦确定后是无法修改的,但对于这种情况又是如何的呢?
有了解的朋友麻烦介绍一下呗!!!
公司现在的用户数据的一个片需要迁移回去,等具体执行后再整理以上文字吧。
分享到:
相关推荐
MongoDB4.2分片及副本集群搭建 MongoDB集群 MongoDB分片 MongoDB副本 MongoDB副本集群
mongodb的分片备份 复制集的备份和配置的备份
mongodb集群分片操作以及增加acl,有什么问题联系我!
MongoDB分片介绍
公司单节点升级到分片下,搜索了一圈大多数MongoDB集群的部署方案都是分片+副本集,没有讲述如何从单节点升级到分片+复制集的方法,亲自试验后记录整个过程以及中间的参考内容和疑难解决,以备后查。内容包含复制集+...
MongoDB分片副本级 详细的讲述了MongoDB分片副本级配置
公司单节点升级到分片下,搜索了一圈大多数 MongoDB 集群的部署方案都是分片+副本集,没有讲述如何从单节点升级到分片+复制集的方法,亲自试验后记录整个过程以及中间的参考内容和疑难解 决,以备后查。内容包含复制...
实验五 MongoDB分片部署与启动
mongodb副本集加分片集群安全认证使用账号密码登录
config server,顾名思义为配置服务器,存储所有数据库元信息(路由、分片)的配置。mongos本身没有物理存储分片服务器和数据路由信息,只是缓存在内存里,配置服务器则实际存储这些数据。mongos第一次启动或者关掉...
k8s 安装 mongodb 分片(Sharding)+ 副本集(Replica Set)
英文当的mongodb分片设计以及原理,喜欢的可以下载来看看
mongodb 分片集群搭建过程(无副本),详细操作步骤,linux命令
mongodb中文API及分布式分片实例详解。
Mongodb_集群分片部署 Mongodb_集群分片部署 Mongodb_集群分片部署
13、MongoDB分片集群&高级集群架构详解_ev.rar13、MongoDB分片集群&高级集群架构详解_ev.rar13、MongoDB分片集群&高级集群架构详解_ev.rar13、MongoDB分片集群&高级集群架构详解_ev.rar13、MongoDB分片集群&高级集群...
mongodb4.22分片及副本集搭建
目录 1 MongoDB的简介和安装启动.mp4 2 MongoDB服务器的启动优化.mp4 3 MongoDB客户端基础使用.mp4 4 MongoDB集合的多种查询条件.mp4 5 MongoDB索引查询与建立.mp4 ...17 MongoDB分片集群的使用介绍.mp4
MongoDB搭建分片集群windows端