首页 / GO / mongodb 手动分片的命令汇总
mongodb 手动分片的命令汇总
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mongodb 手动分片的命令汇总,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2389字,纯文字阅读大概需要4分钟。
内容图文
手动分片的操作
自动分片会带来性能的下降. 所以要合理使用手动分片. 并且配合Tag一起使用.
# 对于4个shard的程序, 预先处理的指令
1. 加入分片服务器
sh.addShard( "192.168.1.60:27017" )
sh.addShard( "192.168.1.61:27017" )
sh.addShard( "192.168.1.62:27017" )
sh.addShard( "192.168.1.63:27017" )
2. 启动collection分片, 并指定分片键
db.location.ensureIndex({"hostid":1})
sh.enableSharding("mydb")
sh.shardCollection("mydb.location", { "hostid": 1})
运行 sh.status()
{ "_id" : "mydb", "partitioned" : true, "primary" : "shard0003" }
mydb.location
shard key: { "hostid" : 1 }
chunks:
shard0003 1
{ "hostid" : { "$minKey" : 1 } } -->> { "hostid" : { "$maxKey" : 1 } } on : shard0003 Timestamp(1, 0)
sh.addShardTag("shard0000", "TAG0")
sh.addShardTag("shard0001", "TAG1")
sh.addShardTag("shard0002", "TAG2")
sh.addShardTag("shard0003", "TAG3")
# 具体操作
1. 加入tagrange,
加入两个
sh.addTagRange("mydb.location", { hostid: "0000000" }, { hostid: "3100000" }, "TAG0")
sh.addTagRange("mydb.location", { hostid: "3100000" }, { hostid: "3200000" }, "TAG1")
由于当前的primary位 shard0003,
sh.addTagRange("mydb.location", { hostid: "3200000" }, { hostid: "3300000" }, "TAG2")
sh.addTagRange("mydb.location", { hostid: "3300000" }, { hostid: "3500000" }, "TAG3")
sh.addTagRange("mydb.location", { hostid: "3500000" }, { $maxKey: 1 }, "TAG3")
4. add chunck
// 将primary 放到第二个分片上
db.runCommand({movePrimary:"mydb",to:"shard0001"})
查看分片情况
db.chunks.find({ns:"mydb.location"})
手动建立空的chunk
for ( var x=300; x<350; x++ ){
var prefix = String(x*10000);
sh.splitAt( "mydb.location", { "hostid":prefix } )
}
然后移动chunk到对应的地方, 移动chunk的命令
sh.moveChunk("mydb.location", { hostid: "3000000"}, "shard0000")
==shard0000
for ( var x=300; x<310; x++ ){
var prefix = String(x*10000);
sh.moveChunk( "mydb.location", { "hostid":prefix }, "shard0000" )
}
==shard0001 310 - 320
==shard0002 320 - 330
==shard0003 330 - 350
其他一些查询操作
// 查找大于 340000的数据, 并且
db.chunks.find({ns:"mydb.location", min:{$gte:{ "hostid" : "3400000" }}})
db.getSiblingDB("admin").runCommand( { moveChunk : "mydb.location" ,
find : { "hostid" : "3400000" } ,
to : "shard0003" } )
1. 建立索引
use mydb
db.location.ensureIndex({"hostid":1})
db.location.ensureIndex({"posTime":1})
db.location.ensureIndex({"hostno":1})
db.location.createIndex( { loc : "2dsphere" } )
db.location.ensureIndex({"posTime":1,"loc":"2dsphere"})
db.location.ensureIndex({"posTime":1,"hostno":1,"loc":"2dsphere"})
db.location.getIndexes()
原文:http://www.cnblogs.com/myibm/p/5939382.html
内容总结
以上是互联网集市为您收集整理的mongodb 手动分片的命令汇总全部内容,希望文章能够帮你解决mongodb 手动分片的命令汇总所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。