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",
Merge Chunks
in a Sharded Cluster
1.Verify a Chunk is
Empty
use test
db.runCommand({
})
merge前
要确保包涵empty chunk
2.Merge Chunks
use admin
db.runCommand( { mergeChunks: "test.test_collection",
sh.status
Clear jumbo
Flag
sh.splitFind("test.test_collection",{ "number" : 1 })
sh.splitAt("test.test_collection",{ "number" : 6000 })
sh.status()
Indivisible
Chunks
1.Stop the balancer
sh.stopBalancer(true)
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(
)
Jumbo Chunks
During chunk migration, if the chunk exceeds
the
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 a mongos instance.
A single shard may have multiple tags, and multiple shards may also
have the same tag.
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")
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" })
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"
)
sh.enableSharding("records")
db.runCommand( { shardCollection : "records.proxy" ,
db = db.getSiblingDB('records');
var primary_id = ObjectId();
db.proxy.insert({
})
db.information.insert({
})
Shard GridFS
Data Store
IMPORTANT
{
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 } } )