天天看点

【Mongodb】Mongodb sharding 管理之一

前面介绍了sharding的基本搭建和大量插入数据测试,本文介绍一些sharding相关查询操作

<b>1 判断是否是shard 集群</b>

mongos&gt; db.runCommand({ isdbgrid : 1});

{ "isdbgrid" : 1, "hostname" : "rac4", "ok" : 1 }

mongos&gt; db.runCommand({ismaster:1});

{

        "ismaster" : true,

        "msg" : "isdbgrid",

        "maxBsonObjectSize" : 16777216,

        "ok" : 1

<b>2 查看已经存在的shard集群:必须在admin数据库上</b>

mongos&gt; db.runCommand({ listShards : 1});

{ "ok" : 0, "errmsg" : "access denied - use admin db" }

mongos&gt; use admin

switched to db admin

        "shards" : [

                {

                        "_id" : "shard0000",

                        "host" : "10.250.7.225:27018"

                },

                        "_id" : "shard0001",

                        "host" : "10.250.7.249:27019"

                        "_id" : "shard0002",

                        "host" : "10.250.7.241:27020"

                }

        ],

}

<b>3 查看一个数据库是否进行了shard</b>

mongos&gt; config = db.getSisterDB("config")

config

mongos&gt; config.system.namespaces.find()

{ "name" : "config.version" }

{ "name" : "config.system.indexes" }

{ "name" : "config.version.$_id_" }

{ "name" : "config.settings" }

{ "name" : "config.settings.$_id_" }

{ "name" : "config.chunks" }

{ "name" : "config.chunks.$_id_" }

{ "name" : "config.chunks.$ns_1_min_1" }

{ "name" : "config.chunks.$ns_1_shard_1_min_1" }

{ "name" : "config.chunks.$ns_1_lastmod_1" }

{ "name" : "config.shards" }

{ "name" : "config.shards.$_id_" }

{ "name" : "config.shards.$host_1" }

{ "name" : "config.mongos" }

{ "name" : "config.mongos.$_id_" }

{ "name" : "config.lockpings" }

{ "name" : "config.lockpings.$_id_" }

{ "name" : "config.locks" }

{ "name" : "config.locks.$_id_" }

{ "name" : "config.lockpings.$ping_1" }

has more

mongos&gt; it

{ "name" : "config.databases" }

{ "name" : "config.databases.$_id_" }

{ "name" : "config.collections" }

{ "name" : "config.collections.$_id_" }

{ "name" : "config.changelog", "options" : { "create" : "changelog", "size" : NumberLong(10485760), "capped" : true } }

查看test数据库,进行了拆分

mongos&gt; test = db.getSisterDB("test")

test

mongos&gt; test.system.namespaces.find()

{ "name" : "test.system.indexes" }

{ "name" : "test.yql" }

{ "name" : "test.yql.$_id_" }

mongos数据库没有进行拆分

mongos&gt; mongos = db.getSisterDB("mongos")

mongos

mongos&gt; mongos.system.namespaces.find()

<b>4 查看整个mongodb 数据库shard的详细信息 </b>

db.printShardingStatus();

 这个命令已经不陌生了,对于此命令的输出:

mongos&gt;<b> printShardingStatus();</b>

--- Sharding Status --- 

<b>第一部分:</b>

  sharding version: { "_id" : 1, "version" : 3 }

<b>第二部分:</b>

  shards:

        {  "_id" : "shard0000",  "host" : "10.250.7.225:27018" }

        {  "_id" : "shard0001",  "host" : "10.250.7.249:27019" }

        {  "_id" : "shard0002",  "host" : "10.250.7.241:27020" }

 介绍了整个sharding 集群的名称,和分布的服务器的ip和端口号。

<b>第三部分:</b>

所有在这个集群上的数据库。

_id 数据库的名字

<b>partitioned</b> 表示是否进行shard 值为true或false,数据库admin没有进行shard,在config服务器上

<b>primary:</b>表示基片,test库有三个shard:shard0000,shard0001,shard0002,其基片为shard0000 表示test数据库在shard000

test.yql chunks:表示collection的分片信息,分为几个区间,哪一个区间在哪一个shard上面。

  databases:

        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }

        {  "_id" : "mongos",  "partitioned" : false,  "primary" : "shard0000" }

        {  "_id" : "test",   "partitioned" : true,   "primary" : "shard0000" }

                test.yql chunks:

                                shard0000       2

                                shard0002       1

                                shard0001       1

                        { "_id" : { $minKey : 1 } } --&gt;&gt; { "_id" : ObjectId("4eb298b3adbd9673afee95e3") } on : shard0000 { "t" : 2000, "i" : 1 }

                        { "_id" : ObjectId("4eb298b3adbd9673afee95e3") } --&gt;&gt; { "_id" : ObjectId("4eb2a64640643e5bb60072f7") } on : shard0000 { "t" : 1000, "i" : 3 }

                        { "_id" : ObjectId("4eb2a64640643e5bb60072f7") } --&gt;&gt; { "_id" : ObjectId("4eb2a65340643e5bb600e084") } on : shard0002 { "t" : 3000, "i" : 1 }

                        { "_id" : ObjectId("4eb2a65340643e5bb600e084") } --&gt;&gt; { "_id" : { $maxKey : 1 } } on : shard0001 { "t" : 3000, "i" : 0 }