1.帮助指令:sh.help(),查看有哪些指令及其含义和使用方法。
mongos> sh.help()
 sh.addShard( host )                       server:port OR setname/server:port
 sh.enableSharding(dbname)                 enables sharding on the database dbname
 sh.shardCollection(fullName,key,unique)   shards the collection
 sh.splitFind(fullName,find)               splits the chunk that find is in at the median
 sh.splitAt(fullName,middle)               splits the chunk that middle is in at middle
 sh.moveChunk(fullName,find,to)            move the chunk where 'find' is to 'to' (name of shard)
 sh.setBalancerState( <bool on or not> )   turns the balancer on or off true=on, false=off
 sh.getBalancerState()                     return true if enabled
 sh.isBalancerRunning()                    return true if the balancer has work in progress on any mongos
 sh.disableBalancing(db.coll)                 disable balancing on one collection
 sh.enableBalancing(db.coll)                  re-enable balancing on one collection
 sh.addShardTag(shard,tag)                 adds the tag to the shard
 sh.removeShardTag(shard,tag)              removes the tag from the shard
 sh.addTagRange(fullName,min,max,tag)      tags the specified range of the given collection
 sh.removeTagRange(fullName,min,max,tag)   removes the tagged range of the given collection
 sh.status()                               prints a general overview of the cluster
或者直接运行help查看系统指令:
mongos> help
 db.help()                    help on db methods
 db.mycoll.help()             help on collection methods
 sh.help()                    sharding helpers
 rs.help()                    replica set helpers
 help admin                   administrative help
 help connect                 connecting to a db help
 help keys                    key shortcuts
 help misc                    misc things to know
 help mr                      mapreduce
 show dbs                     show database names
 show collections             show collections in current database
 show users                   show users in current database
 show profile                 show most recent system.profile entries with time >= 1ms
 show logs                    show the accessible logger names
 show log [name]              prints out the last segment of log in memory, 'global' is default
 use <db_name>                set current database
 db.foo.find()                list objects in collection foo
 db.foo.find( { a : 1 } )     list objects in foo where a == 1
 it                           result of the last line evaluated; use to further iterate
 DBQuery.shellBatchSize = x   set default number of items to display on shell
 exit                         quit the mongo shell
2.sh.status() :查看分片的状态信息:
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("55cd8969e940b6d9c82cd178")
}
  shards:
    {  "_id" : "shard0000",  "host" : "219.219.220.180:27017" }
    {  "_id" : "shard0001",  "host" : "219.219.220.248:27017" }
    {  "_id" : "shard0002",  "host" : "219.219.220.131:27017" }
  balancer:
    Currently enabled:  yes
    Currently running:  no
    Failed balancer rounds in last 5 attempts:  0
    Migration Results for the last 24 hours:
        No recent migrations
  databases:
    {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
    {  "_id" : "xubo",  "partitioned" : true,  "primary" : "shard0002" }
        xubo.resume_meta_data
            shard key: { "_id" : "hashed" }
            chunks:
                shard0000    2
                shard0001    2
                shard0002    2
            { "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong("-6148914691236517204") } on : shard0002 Timestamp(3, 2)
            { "_id" : NumberLong("-6148914691236517204") } -->> { "_id" : NumberLong("-3074457345618258602") } on : shard0002 Timestamp(3, 3)
            { "_id" : NumberLong("-3074457345618258602") } -->> { "_id" : NumberLong(0) } on : shard0000 Timestamp(3, 4)
            { "_id" : NumberLong(0) } -->> { "_id" : NumberLong("3074457345618258602") } on : shard0000 Timestamp(3, 5)
            { "_id" : NumberLong("3074457345618258602") } -->> { "_id" : NumberLong("6148914691236517204") } on : shard0001 Timestamp(3, 6)
            { "_id" : NumberLong("6148914691236517204") } -->> { "_id" : { "$maxKey" : 1 } } on : shard0001 Timestamp(3, 7)
    {  "_id" : "test",  "partitioned" : false,  "primary" : "shard0002" }
    {  "_id" : "ustc",  "partitioned" : false,  "primary" : "shard0000" }
    {  "_id" : "test2",  "partitioned" : false,  "primary" : "shard0001" }
    {  "_id" : "test1",  "partitioned" : false,  "primary" : "shard0001" }
    {  "_id" : "testdb",  "partitioned" : false,  "primary" : "shard0001" }
显示有shard0000,shard0001,shard0002三个分片。admin,xubo等五个数据库,_id后面为数据库名;partitioned表示是否分区,true为是;primary表示数据库的主分区,所有的数据都会位于主分区上,MongoDB目前还不能主动将数据分发到不同的分片上,因为它不知道你希望如何分发数据(参考【1】,P236)。自己测试了插入333821557个文档,大小大概为58G,经查询所有数据都在主分区,其他分区基本没有数据。
3.db.stats(): 查看数据库信息,辅助分片数据库信息查询。
switched to db xubo
mongos> db.stats()
{
 "raw" : {
  "ip_node1:27017" : {
   "db" : "xubo",
   "collections" : 3,
   "objects" : 8,
   "avgObjSize" : 88,
   "dataSize" : 704,
   "storageSize" : 20480,
   "numExtents" : 3,
   "indexes" : 3,
   "indexSize" : 24528,
   "fileSize" : 67108864,
   "nsSizeMB" : 16,
   "extentFreeList" : {
    "num" : 0,
    "totalSize" : 0
   },
   "dataFileVersion" : {
    "major" : 4,
    "minor" : 22
   },
   "ok" : 1
  },
  "ip_node2:27017" : {
   "db" : "xubo",
   "collections" : 3,
   "objects" : 8,
   "avgObjSize" : 88,
   "dataSize" : 704,
   "storageSize" : 20480,
   "numExtents" : 3,
   "indexes" : 3,
   "indexSize" : 24528,
   "fileSize" : 67108864,
   "nsSizeMB" : 16,
   "extentFreeList" : {
    "num" : 0,
    "totalSize" : 0
   },
   "dataFileVersion" : {
    "major" : 4,
    "minor" : 22
   },
   "ok" : 1
  },
  "ip_node3:27017" : {
   "db" : "xubo",
   "collections" : 4,
   "objects" : 333821541,
   "avgObjSize" : 111.99999904140398,
   "dataSize" : 37388012272,
   "storageSize" : 43648786160,
   "numExtents" : 44,
   "indexes" : 4,
   "indexSize" : 10831008832,
   "fileSize" : 57887686656,
   "nsSizeMB" : 16,
   "extentFreeList" : {
    "num" : 0,
    "totalSize" : 0
   },
   "dataFileVersion" : {
    "major" : 4,
    "minor" : 22
   },
   "ok" : 1
  }
 },
 "objects" : 333821557,
 "avgObjSize" : 110.99999889761463,
 "dataSize" : 37388013680,
 "storageSize" : 43648827120,
 "numExtents" : 50,
 "indexes" : 10,
 "indexSize" : 10831057888,
 "fileSize" : 58021904384,
 "extentFreeList" : {
  "num" : 0,
  "totalSize" : 0
 },
 "ok" : 1
}
ip_node1-3是具体的ip地址,这个因设置而不同。上面现实了xubo数据库三个不同分片上的信息,数据基本都在主分区。"objects" : 333821541,
4.数据库分区:
mongos> sh.enableSharding("testdb")
{ "ok" : 1 }
通过sh.status()查看显示testdb已经分片:
 {  "_id" : "testdb",  "partitioned" : true,  "primary" : "shard0001" }
5.对表建立索引db.table2.ensureIndex({"id":1})
mongos> db.table2.ensureIndex({"id":1})
{
 "raw" : {
  "ip_node1:27017" : {
   "createdCollectionAutomatically" : true,
   "numIndexesBefore" : 1,
   "numIndexesAfter" : 2,
   "ok" : 1
  }
 },
 "ok" : 1
}
6.对表建立分片:sh.shardCollection("testdb.table2",{"id":1})
mongos> sh.shardCollection("testdb.table2",{"id":1})
{ "collectionsharded" : "testdb.table2", "ok" : 1 }
查看分片情况:
 {  "_id" : "testdb",  "partitioned" : true,  "primary" : "shard0001" }
  testdb.table1
   shard key: { "id" : 1 }
   chunks:
    shard0000 1
    shard0001 1
    shard0002 1
   { "id" : { "$minKey" : 1 } } -->> { "id" : 1 } on : shard0000 Timestamp(2, 0)
   { "id" : 1 } -->> { "id" : 9 } on : shard0002 Timestamp(3, 0)
   { "id" : 9 } -->> { "id" : { "$maxKey" : 1 } } on : shard0001 Timestamp(3, 1)
  testdb.table2
   shard key: { "id" : 1 }
   chunks:
    shard0001 1
   { "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard0001 Timestamp(1, 0)
  testdb.users
   shard key: { "username" : 1 }
   chunks:
    shard0000 1
    shard0001 1
    shard0002 1
   { "username" : { "$minKey" : 1 } } -->> { "username" : 2 } on : shard0000 Timestamp(2, 0)
   { "username" : 2 } -->> { "username" : 10 } on : shard0002 Timestamp(3, 0)
   { "username" : 10 } -->> { "username" : { "$maxKey" : 1 } } on : shard0001 Timestamp(3, 1)
显示已经分片
7.查看某个表的状态信息:db.table2.stats()mongos> db.table2.stats()
{
 "sharded" : true,
 "paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
 "userFlags" : 1,
 "capped" : false,
 "ns" : "testdb.table2",
 "count" : 111111,
 "numExtents" : 9,
 "size" : 12444432,
 "storageSize" : 22523904,
 "totalIndexSize" : 7064064,
 "indexSizes" : {
  "_id_" : 3638320,
  "id_1" : 3425744
 },
 "avgObjSize" : 112,
 "nindexes" : 2,
 "nchunks" : 3,
 "shards" : {
  "shard0000" : {
   "ns" : "testdb.table2",
   "count" : 0,
   "size" : 0,
   "numExtents" : 1,
   "storageSize" : 8192,
   "lastExtentSize" : 8192,
   "paddingFactor" : 1,
   "paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
   "userFlags" : 1,
   "capped" : false,
   "nindexes" : 2,
   "totalIndexSize" : 16352,
   "indexSizes" : {
    "_id_" : 8176,
    "id_1" : 8176
   },
   "ok" : 1
  },
  "shard0001" : {
   "ns" : "testdb.table2",
   "count" : 111070,
   "size" : 12439840,
   "avgObjSize" : 112,
   "numExtents" : 7,
   "storageSize" : 22507520,
   "lastExtentSize" : 11325440,
   "paddingFactor" : 1,
   "paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
   "userFlags" : 1,
   "capped" : false,
   "nindexes" : 2,
   "totalIndexSize" : 7031360,
   "indexSizes" : {
    "_id_" : 3621968,
    "id_1" : 3409392
   },
   "ok" : 1
  },
  "shard0002" : {
   "ns" : "testdb.table2",
   "count" : 41,
   "size" : 4592,
   "avgObjSize" : 112,
   "numExtents" : 1,
   "storageSize" : 8192,
   "lastExtentSize" : 8192,
   "paddingFactor" : 1,
   "paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
   "userFlags" : 1,
   "capped" : false,
   "nindexes" : 2,
   "totalIndexSize" : 16352,
   "indexSizes" : {
    "_id_" : 8176,
    "id_1" : 8176
   },
   "ok" : 1
  }
 },
 "ok" : 1
}
8.查看块大小:
mongos> use config
switched to db config
mongos> db.settings.find()
{ "_id" : "chunksize", "value" : 100 }
查看有哪些块:
mongos> use config
switched to db config
mongos> db.chunks.find()
{ "_id" : "xubo.resume_meta_data-_id_MinKey", "lastmod" : Timestamp(3, 2), "lastmodEpoch" : ObjectId("55cd8aa4e940b6d9c82cd1bf"), "ns" : "xubo.resume_meta_data", "min" : { "_id" : { "$minKey" : 1 } }, "max" : { "_id" : NumberLong("-6148914691236517204") }, "shard" : "shard0002" }
{ "_id" : "xubo.resume_meta_data-_id_-3074457345618258602", "lastmod" : Timestamp(3, 4), "lastmodEpoch" : ObjectId("55cd8aa4e940b6d9c82cd1bf"), "ns" : "xubo.resume_meta_data", "min" : { "_id" : NumberLong("-3074457345618258602") }, "max" : { "_id" : NumberLong(0) }, "shard" : "shard0000" }
{ "_id" : "xubo.resume_meta_data-_id_3074457345618258602", "lastmod" : Timestamp(3, 6), "lastmodEpoch" : ObjectId("55cd8aa4e940b6d9c82cd1bf"), "ns" : "xubo.resume_meta_data", "min" : { "_id" : NumberLong("3074457345618258602") }, "max" : { "_id" : NumberLong("6148914691236517204") }, "shard" : "shard0001" }
{ "_id" : "xubo.resume_meta_data-_id_-6148914691236517204", "lastmod" : Timestamp(3, 3), "lastmodEpoch" : ObjectId("55cd8aa4e940b6d9c82cd1bf"), "ns" : "xubo.resume_meta_data", "min" : { "_id" : NumberLong("-6148914691236517204") }, "max" : { "_id" : NumberLong("-3074457345618258602") }, "shard" : "shard0002" }
{ "_id" : "xubo.resume_meta_data-_id_0", "lastmod" : Timestamp(3, 5), "lastmodEpoch" : ObjectId("55cd8aa4e940b6d9c82cd1bf"), "ns" : "xubo.resume_meta_data", "min" : { "_id" : NumberLong(0) }, "max" : { "_id" : NumberLong("3074457345618258602") }, "shard" : "shard0000" }
{ "_id" : "xubo.resume_meta_data-_id_6148914691236517204", "lastmod" : Timestamp(3, 7), "lastmodEpoch" : ObjectId("55cd8aa4e940b6d9c82cd1bf"), "ns" : "xubo.resume_meta_data", "min" : { "_id" : NumberLong("6148914691236517204") }, "max" : { "_id" : { "$maxKey" : 1 } }, "shard" : "shard0001" }
{ "_id" : "testdb.users-username_MinKey", "lastmod" : Timestamp(2, 0), "lastmodEpoch" : ObjectId("55d444f3a2c399eae2842423"), "ns" : "testdb.users", "min" : { "username" : { "$minKey" : 1 } }, "max" : { "username" : 2 }, "shard" : "shard0000" }
{ "_id" : "testdb.users-username_2.0", "lastmod" : Timestamp(3, 0), "lastmodEpoch" : ObjectId("55d444f3a2c399eae2842423"), "ns" : "testdb.users", "min" : { "username" : 2 }, "max" : { "username" : 10 }, "shard" : "shard0002" }
{ "_id" : "testdb.users-username_10.0", "lastmod" : Timestamp(3, 1), "lastmodEpoch" : ObjectId("55d444f3a2c399eae2842423"), "ns" : "testdb.users", "min" : { "username" : 10 }, "max" : { "username" : { "$maxKey" : 1 } }, "shard" : "shard0001" }
{ "_id" : "testdb.table1-id_MinKey", "lastm
————————————————
版权声明:本文为CSDN博主「KeepLearningBigData」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xubo245/article/details/47782557

mongoDB 分片集群常用指令的更多相关文章

  1. MongoDB分片集群常用操作

    下架主节点: db.adminCommand({replSetStepDown : 1, force : true}) 删除节点: rs.remove("IP:PORT") 新增节 ...

  2. CentOS7+Docker+MangoDB下部署简单的MongoDB分片集群

    简单的在Docker上快速部署MongoDB分片集群 前言 文中使用的环境如下 OS:CentOS Linux release 7.5.1804 (Core) Docker:Docker versio ...

  3. MongoDB分片集群原理、搭建及测试详解

    随着技术的发展,目前数据库系统对于海量数据的存储和高效访问海量数据要求越来越高,MongoDB分片机制就是为了解决海量数据的存储和高效海量数据访问而生. MongoDB分片集群由mongos路由进程( ...

  4. mongodb分片集群

    第一章 1.mongodb 分片集群解释和目的 一组Mongodb复制集,就是一组mongod进程,这些进程维护同一个数据集合.复制集提供了数据冗余和高等级的可靠性,这是生产部署的基础. 第二章 1. ...

  5. TiDB和MongoDB分片集群架构比较

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 最近阅读了TiDB源码的说明文档,跟MongoDB的分片集群做了下简单对比. 首先展示TiDB的整体架构 M ...

  6. 搭建MongoDB分片集群

    在部门服务器搭建MongoDB分片集群,记录整个操作过程,朋友们也可以参考. 计划如下: 用5台机器搭建,IP分别为:192.168.58.5.192.168.58.6.192.168.58.8.19 ...

  7. 网易云MongoDB分片集群(Sharding)服务已上线

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. MongoDB sharding cluster(分片集群)是MongoDB提供的数据在线水平扩展方案,包括 ...

  8. MongoDB 分片集群实战

    背景 在如今的互联网环境下,海量数据已随处可见并且还在不断增长,对于如何存储处理海量数据,比较常见的方法有两种: 垂直扩展:通过增加单台服务器的配置,例如使用更强悍的 CPU.更大的内存.更大容量的磁 ...

  9. Windows 搭建MongoDB分片集群(二)

    在本篇博客中我们主要讲描述分片集群的搭建过程.配置分片集群主要有两个步骤,第一启动所有需要的mongod和mongos进程.第二步就是启动一个mongos与集群通信.下面我们一步步来描述集群的搭建过程 ...

随机推荐

  1. 【原】python-jenkins信息

    1.官方文档: https://python-jenkins.readthedocs.io/en/latest/examples.html#example-1-get-version-of-jenki ...

  2. 一、Python概念知识点汇总

    一.编译型语言和解释性语言的区别 二.Python的设计目标 1.一门简单直观的语言并与主要竞争者一样强大 2.开源,以便使任何人都可以为它做贡献 3.代码像纯英文那样容易理解 4.适用于短期开发的日 ...

  3. 【C语言】求1到100的和

    #include<stdio.h> int main() { ; ; ; ) { sum = sum + number; number = number + ; i = i + ; } p ...

  4. PHP如何实现处理过期或者超时订单的,并还原库存

    订单是我们在日常开发中经常会遇到的一个功能,最近在做一个订单过期与超时的开发.订单过期与超时就不用我解释了吧,其实两者都是同一个问题来着,就是订单未支付的处理,我们要做的是对这些未支付的订单到了一定时 ...

  5. [0CTF 2016] piapiapia

    一道非常有意思的反序列化漏洞的题目 花费了我不少时间理解和记忆 这里简单记录其中精髓 首先打开是一个登陆页面 dirsearch扫描到了www.zip源码备份 update.php <?php ...

  6. 「题解」「POJ1322」Chocolate

    目录 题目 原题目 简易题意 思路分析 代码 练习题 题目 原题目 点这里 简易题意 包裹里有无限个分布均匀且刚好 \(c\) 种颜色的巧克力,现在要依次拿 \(n\) 个出来放到桌子上.每次如果桌子 ...

  7. 【代码总结】PHP面向对象之类与对象

    一.类和对象的关系 类的实体化结果是对象,而对象的抽象就是类.在开发过程中,我们通常都是先抽象(幻想)出一个类,再用该类去创建对象(实现幻想的内容).在程序中,直接使用的是我们(实现幻想)的对象,而不 ...

  8. Codeforces Round #621 (Div. 1 + Div. 2) C. Cow and Message

    Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is ...

  9. 在tomcat启动时解析xml文件,获取特定标签的属性值,并将属性值设置到静态变量里

    这里以解析hibernate.cfg.xml数据库配置信息为例,运用dom4j的解析方式来解析xml文件. 1.在javaWeb工程里新建一个java类,命名为GetXmlValue.java,为xm ...

  10. 疫情对国内5G发展的影响

    导读 2020年春节期间,“新型冠状病毒”引发了自SARS之后又一次全国性疫情,而世卫组织也将之列为国际关注的突发公共卫生事件,随后多国发布了针对中国的旅行警告和入境限制,从当前情况来看,疫情对中国各 ...