手动分片的操作

自动分片会带来性能的下降. 所以要合理使用手动分片. 并且配合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()

mongodb 手动分片的命令汇总的更多相关文章

  1. MongoDB 常用shell命令汇总

    //指定用户名和密码连接到指定的MongoDB数据库 mongo 192.168.1.200:27017/admin -u user -p password use youDbName 1.Mongo ...

  2. MongoDB之分片

    本文介绍分片的思想和MongoDB中的实现方法. 首先须要介绍一些主要的概念. 分片 分片.也叫做分区.是一种经常使用的数据库优化技术.其含义就是将数据拆分,将数据分散到不同机器上的过程.这样就能够使 ...

  3. 20145222《信息安全系统设计基础》Linux常用命令汇总

    学习Linux时常用命令汇总 通过Ctrl+f键可在该网页搜索到你想要的命令. Linux中命令格式为:command [options] [arguments] //中括号代表是可选的,即有些命令不 ...

  4. mongodb(分片)

    分片(即sharding)是将数据拆分至不同数据节点的方式. 1.在mongoDB中提供了自动分片的方式,它会根据数据块(chunk)大小的设定,对片键进行拆分: 2.mongoDB配置分片,要配置三 ...

  5. MongoDB之分片集群(Sharding)

    MongoDB之分片集群(Sharding) 一.基本概念 分片(sharding)是一个通过多台机器分配数据的方法.MongoDB使用分片支持大数据集和高吞吐量的操作.大数据集和高吞吐量的数据库系统 ...

  6. mongodb系列~mongo常用命令

    mongodb常用命令大全1 索引相关命令 db.chenfeng.ensureIndex({"riqi":1}) 添加索引会阻塞nohup mongo --eval " ...

  7. mongodb的分片(2)

    在上一片博客,详细说明了mongodb的分片搭建的详细过程:分片搭建 在这里会说一些分片的维护与操作! 在集群搭建完,我们使用了sh.status()查看分片之后的数据,如下: #连接的是mongos ...

  8. mongodb的分片

    分片是把大型数据集合进行分区成更小的可管理的片的过程. 副本集的每一个成员(仲裁节点除外)都一份数据的完整拷贝! 分片的目的:在节省硬件成本的基础上,提高系统的整体性能.但是却增加了管理和性能的开销. ...

  9. MongoDB 自动分片 auto sharding

    MongoDB部署实验系列文章 MongoDB做为NoSQL数据库,最近几年持续升温,越来越多的企业都开始尝试用MongoDB代替原有Database做一些事情.MongoDB也在集群,分片,复制上也 ...

随机推荐

  1. css3学习总结7--CSS3 2D转换

    CSS3 转换 通过 CSS3 转换,我们能够对元素进行移动.缩放.转动.拉长或拉伸. 2D 转换 在本次,您将学到如下 2D 转换方法: 1. translate() 2. rotate() 3. ...

  2. 解决Unable to reach a settlement: [diffie-hellman-group1-sha1, diffie-hellman-group-exchange-sha1] and [curve25519-sha256@li

    SharpSSH或JSCH使用diffie-hellman-group1-sha1和diffie-hellman-group-exchange-sha1密钥交换算法,而OpenSSH在6.7p1版本之 ...

  3. Perl中的正则表达式

    转自:http://c20031776.blog.163.com/blog/static/684716252013624383887/ Perl 程序中,正则表达式有三种存在形式 分别是 (1 模式匹 ...

  4. 用sqlplus登陆数据库时,oracle 11g出现ORA-12514问题

    转自:http://zhidao.baidu.com/question/144648216.html 启动服务 然后在sqlplus / as sysdba;执行启动startup nomount;a ...

  5. 比较StringBuffer字符串内容是否相等?

    为什么会有这个问题呢?首先得看看String和StringBuffer的比较区别: ==只能比较两个字符串的内存地址是否一样,不能比较字符串内容: String的equals方法因为重写了Object ...

  6. Grunt上手指南<转>

    原文链接:http://www.hulufei.com/post/grunt-introduction 安装 如果之前有装过grunt,卸载之 npm uninstall -g grunt 安装gru ...

  7. Scala中的match(模式匹配)

    文章来自:http://www.cnblogs.com/hark0623/p/4196261.html   转载请注明 代码如下: /** * 模式匹配 */ case class Class1(pa ...

  8. php升级5.3到5.4,5.5,5.6

    Laravel要求php大于5.5.9,升级php5.3.3到5.6.14(最新版为 5.6.15 ) Add EPEL and Remi repositories onto your system: ...

  9. 使用NGUINGUI的相关介绍

    1.3  使用NGUI 要使用NGUI,需要首先为游戏项目导入NGUI插件资源,然后再创建UI Root对象,在这以后才可以添加各种UI控件,下面本节会详解介绍这些知识本文选自NGUI从入门到实战! ...

  10. 简单几何(凸包+枚举) POJ 1873 The Fortified Forest

    题目传送门 题意:砍掉一些树,用它们做成篱笆把剩余的树围起来,问最小价值 分析:数据量不大,考虑状态压缩暴力枚举,求凸包以及计算凸包长度.虽说是水题,毕竟是final,自己状压的最大情况写错了,而且忘 ...