技术开发 频道

基于MongoDB进行分布式数据存储的步骤

  接下来创建相应数据库并设置其"可以sharding",新建自动切片的库user001:

  > config = connect("10.0.4.85:27022")

  
> config = config.getSisterDB("config")

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

  dnt_mongodb

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

  {
"ok" : 1 }

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

  > db.printShardingStatus();

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->   --- 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自动生成 [必须为唯一索引unique index]。

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

  db.runCommand( { shardcollection : "dnt_mongodb.attach_gfstream.chunks", key : { files_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 } } } }
0
相关文章