mongoDB-3.x Data Management

mongoDB-3.x Data Management



Create Chunks in a Sharded Cluster



Split Chunks in a Sharded Cluster


Migrate Chunks in a Sharded Cluster
db.adminCommand( { moveChunk : "test.test_collection",
                   find : {"user_id" : 2},
                   to : "rs0/192.168.192.11:27018,192.168.192.21:27018" } )



Merge Chunks in a Sharded Cluster
1.Verify a Chunk is Empty
use test
db.runCommand({
   "dataSize": "test.test_collection",
   "keyPattern": { number: 1 },
   "min": { "number" : 100000 },
   "max": { "number" : 110000 }
})
merge前
mongoDB-3.x <wbr>Data <wbr>Management


要确保包涵empty chunk
2.Merge Chunks
use admin
db.runCommand( { mergeChunks: "test.test_collection",
                 bounds: [ { "number" : 2400},
                           { "number" : 5519} ]
             } )
sh.status
mongoDB-3.x <wbr>Data <wbr>Management
merge后
mongoDB-3.x <wbr>Data <wbr>Management


Clear jumbo Flag
sh.splitFind("test.test_collection",{ "number" : 1 })
sh.splitAt("test.test_collection",{ "number" : 6000 })
sh.status()
mongoDB-3.x <wbr>Data <wbr>Management

mongoDB-3.x <wbr>Data <wbr>Management

Indivisible Chunks
1.Stop the balancer
sh.stopBalancer(true)
mongoDB-3.x <wbr>Data <wbr>Management

2.Create a backup of config database
mongodump --db config --port 27019 --out mongo_conf
3.Find the jumbo Chunk
sh.status()
4.Update chunks collection
db.getSiblingDB("config").chunks.update(
   { ns: "test.test_collection", min: { "number" : 6000 }, jumbo: true },
   { $unset: { jumbo: "" } }
)

Jumbo Chunks

During chunk migration, if the chunk exceeds the  specified chunk size or if the number of documents in the chunk exceeds Maximum Number of Documents Per Chunk to Migrate, MongoDB does not migrate the chunk. Instead, MongoDB attempts to split the chunk. If the split is unsuccessful, MongoDB labels the chunk as jumbo to avoid repeated attempts to migrate the chunk.

5.Restart the balancer
sh.startBalancer(true)
6.Optional. Clear current cluster meta information
db.adminCommand({flushRouterConfig: 1})


Manage Shard Tags
Associate tags with a particular shard using the sh.addShardTag() method when connected to amongos instance. A single shard may have multiple tags, and multiple shards may also have the same tag.
Tag a Shard
sh.addShardTag("rs0","P")
sh.addShardTag("rs1","S")
mongoDB-3.x <wbr>Data <wbr>Management

sh.removeShardTag("rs1","S2")

Tag a Shard Key Range
use test
sh.addTagRange("test.test_collection", { "user_id" : 40 }, { "user_id" : 95 }, "NYC")
sh.addTagRange("test.test_collection", { "user_id" : 150 }, { "user_id" : 165 }, "SF0")
mongoDB-3.x <wbr>Data <wbr>Management


Remove a Tag From a Shard Key Range
use config
db.tags.remove({ _id: { ns: "test.test_collection", min: { "user_id" : 40 }}, tag: "NYC" })
db.tags.remove({ _id: { ns: "test.test_collection", min: { "user_id" : 150 }}, tag: "SF0" })

View Existing Shard Tags
use config
db.shards.find({ tags: "NYC" })
db.tags.find({ tag: "NYC" })
mongoDB-3.x <wbr>Data <wbr>Management


Enforce Unique Keys for Sharded Collections
Unique Constraints on the Shard Key
use admin
db.runCommand( { shardCollection : "test.test_collection" , key : { number : 1 } , unique : true } )
db.runCommand( { shardCollection : "test.test_collection"  )
mongoDB-3.x <wbr>Data <wbr>Management

Unique Constraints on Arbitrary Fields
sh.enableSharding("records")
db.runCommand( { shardCollection : "records.proxy" ,
                 key : { email : 1 } ,
                 unique : true } );
db = db.getSiblingDB('records');
var primary_id = ObjectId();
db.proxy.insert({
   "_id" : primary_id,
   "email" : "example@example.net"
})
db.information.insert({
   "_id" : primary_id,
   "email": "example@example.net"
})
mongoDB-3.x <wbr>Data <wbr>Management

Shard GridFS Data Store

IMPORTANT

{ files_id : 1 , n : 1 } and {  files_id : 1 } are the only supported shard keys for the chunks collection of a GridFS store.

Shard GridFS Data Store
db.fs.chunks.createIndex( { files_id : 1 , n : 1 } )
db.runCommand( { shardCollection : "test.fs.chunks" , key : { files_id : 1 , n : 1 } } )
db.runCommand( { shardCollection : "test.fs.chunks" , key : {  files_id : 1 } } )
mongoDB-3.x <wbr>Data <wbr>Management

posted @ 2016-01-18 13:19  李庆喜  阅读(169)  评论(0编辑  收藏  举报