前面介绍了sharding的基本搭建和大量插入数据测试,本文介绍一些sharding相关查询操作
1 判断是否是shard 集群
mongos> db.runCommand({ isdbgrid : 1});
{ "isdbgrid" : 1, "hostname" : "rac4", "ok" : 1 }
mongos> db.runCommand({ismaster:1});
{
"ismaster" : true,
"msg" : "isdbgrid",
"maxBsonObjectSize" : 16777216,
"ok" : 1
2 查看已经存在的shard集群:必须在admin数据库上
mongos> db.runCommand({ listShards : 1});
{ "ok" : 0, "errmsg" : "access denied - use admin db" }
mongos> use admin
switched to db admin
mongos> db.runCommand({ listShards : 1});
{
"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"
}
],
"ok" : 1
}
3 查看一个数据库是否进行了shard
mongos> config = db.getSisterDB("config")
config
mongos> 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> 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> test = db.getSisterDB("test")
test
mongos> test.system.namespaces.find()
{ "name" : "test.system.indexes" }
{ "name" : "test.yql" }
{ "name" : "test.yql.$_id_" }
mongos数据库没有进行拆分
mongos> mongos = db.getSisterDB("mongos")
mongos
mongos> mongos.system.namespaces.find()
4 查看整个mongodb 数据库shard的详细信息
db.printShardingStatus();
这个命令已经不陌生了,对于此命令的输出:
mongos> printShardingStatus();
--- Sharding Status ---
第一部分:
sharding version: { "_id" : 1, "version" : 3 }
第二部分:
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和端口号。
第三部分:
所有在这个集群上的数据库。
_id 数据库的名字
partitioned 表示是否进行shard 值为true或false,数据库admin没有进行shard,在config服务器上
primary:表示基片,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 } } -->> { "_id" : ObjectId("4eb298b3adbd9673afee95e3") } on : shard0000 { "t" : 2000, "i" : 1 }
{ "_id" : ObjectId("4eb298b3adbd9673afee95e3") } -->> { "_id" : ObjectId("4eb2a64640643e5bb60072f7") } on : shard0000 { "t" : 1000, "i" : 3 }
{ "_id" : ObjectId("4eb2a64640643e5bb60072f7") } -->> { "_id" : ObjectId("4eb2a65340643e5bb600e084") } on : shard0002 { "t" : 3000, "i" : 1 }
{ "_id" : ObjectId("4eb2a65340643e5bb600e084") } -->> { "_id" : { $maxKey : 1 } } on : shard0001 { "t" : 3000, "i" : 0 }