网络编程:基于Mongodb进行分布式数据存储(2)

http://www.itjxue.com  2015-07-17 21:29  来源:未知  点击次数: 

  > dnt_mongodb=db.getSisterDB("dnt_mongodb");

  dnt_mongodb

  > db.runCommand({enablesharding:"dnt_mongodb"})

  { "ok" : 1 }

  注:一旦enable了个数据库,mongos将会把数据库里的不同数据集放在不同的分片上。除非数据集被分片(下面会设置),否则一个数据集的所有数据将放在一个分片上。

  > db.printShardingStatus();

  --- Sharding Status ---

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

  shards:

  { "_id" : "shard0000", "host" : "10.0.4.85:27020" }

  { "_id" : "shard0001", "host" : "10.0.4.85:27021" }

  databases:

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

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

  > db.runCommand( { shardcollection : "dnt_mongodb.posts1", key : {_id : 1}, unique: true } )

  { "collectionsharded" : "dnt_mongodb.posts1", "ok" : 1 }

  --使用shardcollection 命令分隔数据集,key自动生成。

  如果要进行GridFS sharding,则需进行如下设置:

  db.runCommand( { shardcollection : "test.fs.chunks", key : { _id : 1 } } )

  {"ok" : 1} ,更多内容参见http://eshilin.blog.163.com/blog/static/13288033020106215227346/

  > db.printShardingStatus()

  --- Sharding Status ---

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

  shards:

  { "_id" : "shard0000", "host" : "localhost:27020" }

  { "_id" : "shard0001", "host" : "localhost:27021" }

  databases:

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

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

  dnt_mongodb.posts1e chunks:

  { "name" : { $minKey : 1 } } -->> { "name" : { $maxKey :

  1 } } on : shard0000 { "t" : 1000, "i" : 0

  下面我用一个工具来批量向dnt_mongodb数据库的 posts1表中导入数据,大约是16万条数据。导入过程中mongos会显示类似如下信息:

  Tue Sep 07 12:13:15 [conn14] autosplitting dnt_mongodb.posts1 size: 47273960 shard: ns:dnt_mongodb.posts1 at: shard0000:10.0.4.85:27020 lastmod: 1|0 min: { _id: MinKey } max: { _id: MaxKey } on: { _id: 19 }(splitThreshold 47185920)

  Tue Sep 07 12:13:15 [conn14] config change: { _id: "4_85-2010-09-07T04:13:15-0", server: "4_85", time: new Date(1283832795994), what: "split", ns: "dnt_mongodb.posts1", details: { before: { min: { _id: MinKey }, max: { _id: MaxKey } }, left: { min: { _id: MinKey }, max: { _id: 19 } }, right: { min: { _id: 19 }, max: {_id: MaxKey } } } }

  Tue Sep 07 12:13:16 [conn14] moving chunk (auto): ns:dnt_mongodb.posts1 at: shard0000:10.0.4.85:27020 lastmod: 1|1 min: { _id: MinKey } max: { _id: 19 } to: shard0001:10.0.4.85:27021 #objects: 0

  Tue Sep 07 12:13:16 [conn14] moving chunk ns: dnt_mongodb.posts1 moving ( ns:dnt_mongodb.posts1 at: shard0000:10.0.4.85:27020 lastmod: 1|1 min: { _id: MinKey }max: { _id: 19 }) shard0000:10.0.4.85:27020 -> shard0001:10.0.4.85:27021

  Tue Sep 07 12:13:23 [WriteBackListener] ~ScopedDBConnection: _conn != null

  Tue Sep 07 12:13:23 [WriteBackListener] ERROR: splitIfShould failed: ns: dnt_mongodb.posts1 findOne has stale config

  Tue Sep 07 12:13:28 [WriteBackListener] autosplitting dnt_mongodb.posts1 size: 54106804 shard: ns:dnt_mongodb.posts1 at: shard0000:10.0.4.85:27020 lastmod: 2|1min: { _id: 19 } max: { _id: MaxKey } on: { _id: 71452 }(splitThreshold 47185920)

  Tue Sep 07 12:13:28 [WriteBackListener] config change: { _id: "4_85-2010-09-07T04:13:28-1", server: "4_85", time: new Date(1283832808738), what: "split", ns: "dnt_mongodb.posts1", details: { before: { min: { _id: 19 }, max: { _id: MaxKey }}, left: { min: { _id: 19 }, max: { _id: 71452 } }, right: { min: { _id: 71452 }, max: { _id: MaxKey } } } }

  在完成自动sharding之后,可以使用mongo看一下结果:

  > use dnt_mongodb

  switched to db dnt_mongodb

  > show collections

  posts1

  system.indexes

  > db.posts1.stats()

  {

  "sharded" : true,

  "ns" : "dnt_mongodb.posts1",

  "count" : 161531,

  "size" : 195882316,

  "avgObjSize" : 1212.6608267143768,

  "storageSize" : 231467776,

  "nindexes" : 1,

  "nchunks" : 5,

  "shards" : {

(责任编辑:IT教学网)

更多

推荐编程综合文章