---
title: "【7】MongoDB分片管理（Sharding Administration）"
author: "chakey"
date: "2010-11-08T23:17:33Z"
tags: ["Blog", "MongoDB", "配置管理"]
categories: ["数据库"]
slug: "7-mongodb-sharding-administration"
draft: false
lastmod: "2010-11-08T23:17:33Z"
id: 806106
url: "https://datalife.iteye.com/blog/806106"
---

【7】MongoDB分片管理（Sharding Administration）

下面介绍几个非常有用的命令，可以获取一个分片集群的一些配置信息。

关于如何配置分片请查看：

http://datalife.iteye.com/blog/805201

 

一：如果判断是否是分片集群？

// Test if we're speaking to a mongos process or 

// straight to a mongod process

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

 

// if connected to mongos, this command returns { ismaster: 0.0, msg: "isdbgrid" }

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

 

二：如何列举出所有存在的分片？

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

{"servers" :

 [{"\_id" :  ObjectId( "4a9d40c981ba1487ccfaa634")  ,

   "host" : "localhost:10000"},

  {"\_id" :  ObjectId( "4a9d40df81ba1487ccfaa635")  ,

   "host" : "localhost:10001"}

 ],

 "ok" : 1

}

 

三：如何列举出哪些数据库被分片了？

这里我们需要查询config 数据库，getSisterDB命令用于返回config数据库。

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

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

 

四：如果查看分片的详细信息？

&gt; use admin

&gt; db.printShardingStatus();

 

// A very basic sharding configuration on localhost

sharding version: { "\_id" : 1, "version" : 2 }

  shards:

      { "\_id" : ObjectId("4bd9ae3e0a2e26420e556876"), "host" : "localhost:30001" }

      { "\_id" : ObjectId("4bd9ae420a2e26420e556877"), "host" : "localhost:30002" }

      { "\_id" : ObjectId("4bd9ae460a2e26420e556878"), "host" : "localhost:30003" }

 

  databases:

{ "name" : "admin", "partitioned" : false, 

          "primary" : "localhost:20001", 

          "\_id" : ObjectId("4bd9add2c0302e394c6844b6") }

my chunks

        { "name" : "foo", "partitioned" : true,

          "primary" : "localhost:30002", 

          "sharded" : { "foo.foo" : { "key" : { "\_id" : 1 }, "unique" : false } },

          "\_id" : ObjectId("4bd9ae60c0302e394c6844b7") }

        my chunks

        foo.foo { "\_id" : { $minKey : 1 } } --&gt;&gt; { "\_id" : { $maxKey : 1 } } 

                  on : localhost:30002 { "t" : 1272557259000, "i" : 1 }

 

第一个数据库是主数据库（admin database）,没有分区，位于config服务器，端口20001.

第二个数据库，有分区，因为foo数据库没有数据，所有只有一个块（chunk）。

 

五:块操作（chunk operations）

MongoDB 1.6版本的是可以自动的控制块的大小的，当然也是可以手动调整的。

db.runCommand( { moveChunk : "test.blog.posts" , 

                 find : { author : "eliot" } , 

                 to : "shard1" } )

三个参数：

movechunk:包含数据库名称的全命名空间的collection

find: 属于将被移动的块的查询语句

to: 将被移动的块的shard id