MongoDB的sharding功能(二)

2014-11-24 11:51:59 · 作者: · 浏览: 1
ost:27020" }, { "_id" : ObjectId("4b9cd381c33000afad27718f"), "host" : "localhost:27021" } ], "ok" : 1 } 新建自动切片的库user001: > config = connect("localhost:27022") > config = config.getSisterDB("config") > user001=db.getSisterDB("user001"); user001 > db.runCommand({enablesharding:"user001"}) { "ok" : 1 } > db.printShardingStatus(); --- Sharding Status --- sharding version: { "_id" : ObjectId("4b9cd354c33000afad27718d"), "version" : 2 } shards: { "_id" : ObjectId("4b9cd380c33000afad27718e"), "host" : "localhost:27020" } { "_id" : ObjectId("4b9cd381c33000afad27718f"), "host" : "localhost:27021" } databases: { "name" : "admin", "partitioned" : false, "primary" : "localhost:27022", "_id" : ObjectId("4b9cd3776693dcfa468dec13") } { "name" : "user001", "partitioned" : true, "primary" : "localhost:27021", "_id" : ObjectId("4b9cde866693dcfa468dec17") } my chunks 我们来在user001中新建表,插入数据 > use user001 switched to db user001 > db.createCollection("user_001") { "ok" : 1 } > show collections system.indexes user_001 > db.user_001.insert({uid:1,username:"Falcon.C",sex:"男",age:25}); > db.user_001.find(); { "_id" : ObjectId("4b9ce1a6c84d7f20576c4df1"), "uid" : 1, "username" : "Falcon.C", "sex" : "男", "age" : 25 } 我们来看看user001库被分配到了哪个shard上 [falcon@www.fwphp.cn ~/mongodata]$ ls -R .: 27020 27021 27022 mongos.log ./27020: 27020.log mongod.lock test.0 test.1 test.ns _tmp ./27020/_tmp: ./27021: 27021.log mongod.lock _tmp user.0 user001.0 user001.1 user001.ns user.1 user.ns ./27021/_tmp: ./27022: 27022.log config.0 config.ns mongod.lock mongos.log _tmp ./27022/_tmp: [falcon@www.fwphp.cn ~/mongodata]$

从以上的文件可以看出,user001被分配到了27021的shard上了,但是通过mongos路由,我们并感觉不到是数据存放在哪个shard的chunk上
Sharding的管理命令
> db.$cmd.findOne({isdbgrid:1});                                                                           
{ "isdbgrid" : 1, "hostname" : "www.fwphp.cn", "ok" : 1 } 
> db.$cmd.findOne({ismaster:1}); 
{ "ismaster" : 1, "msg" : "isdbgrid", "ok" : 1 } 
> printShardingStatus(db.getSisterDB("config")) 
--- Sharding Status ---  
   sharding version: { "_id" : ObjectId("4b9cd354c33000afad27718d"), "version" : 2 } 
   shards: 
       { "_id" : ObjectId("4b9cd380c33000afad27718e"), "host" : "localhost:27020" } 
       { "_id" : ObjectId("4b9cd381c33000afad27718f"), "host" : "localhost:27021" } 
   databases: 
         { "name" : "admin", "partitioned" : false, "primary" : "localhost:27022", "_id" : ObjectId("4b9cd3776693dcfa468dec13") } 
                 my chunks 
         { "name" : "user001", "partitioned" : true, "primary" : "localhost:27021", "_id" : ObjectId("4b9cde866693dcfa468dec17") } 
                 my chunks 
> use admin 
switched to db admin 
> db.runCommand({netstat:1}) 
{ "configserver" : "localhost:27022", "isdbgrid" : 1, "ok" : 1 } 
>