mongodb迁移分片,关闭或者移除表的sharding ,

发布时间:2020-07-21 18:39:07 作者:cuisichao
来源:网络 阅读:4817

MongoDBShard集群来说,添加一个分片很简单,AddShard就可以了。

但是缩减集群(删除分片)这种一般很少用到,但是有些场景,必须把它上面的数据自动迁移到其他Shard上。


 mongodb迁移分片 

1、执行RemoveShard命令 --只是迁移sharding

1       db.runCommand( { removeshard:"your_shard_name" } )

 

3       { msg : "draining startedsuccessfully" , 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块没迁移完。

remain0之后,这一步就结束了。

 

 

3、迁移非Shard数据或者非Shard

 

1       db.runCommand( { movePrimary:"testdb", to: "shard2" })

        

这次就不是立即返回了,需要很久,然后会返回如下:

1       { "primary" :"mongodb1", "ok" : 1 }

 

 

        

4、最后的清理

上面步骤都完成后,还需要再执行一次RemoveShard,清理残余数据。

1       db.runCommand( { removeshard: "shard1"} )

执行成功后,会如下结果:

1       { msg: "remove shard completedsuccesfully", stage: "completed", host: " shard1", ok: 1 }

显示completed后,就可以安心的关闭mongod的进程了。

 

 

注意官方关于是否需要运行movePrimary的说明:

 

也就是说,如果在这个片上有非分片的collection,这样的话,分片的数据合到其他片上了,那么剩下的非分片数据,没法通过合并分片的方式合到其他服务器上,所以这时要运行一个movePrimary命令将这些非分片的数据移到另一个服务器上。db.runCommand( { movePrimary: "testdb", to: "shard1"})

 

还有一个官方说明需要注意的是:

 

Warning

Do not run themovePrimary 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




mongodb关闭表的分片 (移除分片)

过程: 导出带有sharding 的集合, 删除集合 ,导入集合


1.导出集合

/usr/local/mongodb/bin/mongodump--host 127.0.0.1:30000  -d testdb  -c table1  -o  testdb/

2.禁用分片的自动平衡

 

> use config

>db.settings.update( { _id: "balancer" }, { $set : { stopped: true } } , true );

> db.printShardingStatus()

 

mongodb迁移分片,关闭或者移除表的sharding ,

 

3.删除集合

db.table1.drop();

 

4.导入集合

/usr/local/mongodb/bin/mongorestore  --host 127.0.0.1:30000  -d testdb     testdb/ 


执行 db.printShardingStatus() ,查看分片概要

发现集合table1 的 sharding 功能已经被关闭!




平衡的关闭和开启

mongodb在做自动分片平衡的时候,或引起数据库响应的缓慢,可以通过禁用自动平衡以及设置自动平衡进行的时间来解决这一问题。

1)禁用分片的自动平衡

> use config

>db.settings.update( { _id: "balancer" }, { $set : { stopped: true } } , true );


mongodb迁移分片,关闭或者移除表的sharding ,

 

恢复动态平台:

use config

db.settings.remove({"_id":"balancer"});

 

 

2)自定义自动平衡进行的时间段

> use config

>db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "21:00", stop : "9:00" } } }, true )

 


推荐阅读:
  1. 初步安装、使用PyPy
  2. 多张图片上传简单示例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mongodb迁移分片 关闭或者移除表的sharding 开启自动平衡

上一篇:oracle查询临时表空间使用情况

下一篇:php在windows上扩展pthreads多线程

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》