Operations for manual sharding
Automatic sharding can result in degraded performance. Therefore, it is reasonable to use manual shards. and use with tag together.
# for 4 shard programs, pre-processed instructions
1. Join the Shard server
Sh.addshard ("192.168.1.60:27017")
Sh.addshard ("192.168.1.61:27017")
Sh.addshard ("192.168.1.62:27017")
Sh.addshard ("192.168.1.63:27017")
2. Start the collection shard and specify the Sharding key
Db.location.ensureIndex ({"HostID": 1})
Sh.enablesharding ("MyDB")
Sh.shardcollection ("Mydb.location", {"HostID": 1})
Run Sh.status ()
{"_id": "MyDB", "partitioned": true, "PRIMARY": "Shard0003"}
Mydb.location
Shard key: {"HostID": 1}
Chunks
shard00031
{"HostID": {"$minKey": 1}}-->> {"HostID": {"$maxKey": 1}} on:shard0003 Timestamp (1, 0)
Sh.addshardtag ("shard0000", "TAG0")
Sh.addshardtag ("shard0001", "TAG1")
Sh.addshardtag ("shard0002", "TAG2")
Sh.addshardtag ("shard0003", "TAG3")
# Specific operation
1. Join Tagrange,
Join two +
Sh.addtagrange ("Mydb.location", {hostid: "0000000"}, {hostid: "3100000"}, "TAG0")
Sh.addtagrange ("Mydb.location", {hostid: "3100000"}, {hostid: "3200000"}, "TAG1")
Due to the current primary bit shard0003,
Sh.addtagrange ("Mydb.location", {hostid: "3200000"}, {hostid: "3300000"}, "TAG2")
Sh.addtagrange ("Mydb.location", {hostid: "3300000"}, {hostid: "3500000"}, "TAG3")
Sh.addtagrange ("Mydb.location", {hostid: "3500000"}, {$maxKey: 1}, "TAG3")
4. Add Chunck
Put the primary on the second shard
Db.runcommand ({moveprimary: "MyDB", To: "shard0001"})
View Shard Condition
Db.chunks.find ({ns: "Mydb.location"})
Manually establishing an empty chunk
for (var x=300; x<350; x + +) {
var prefix = String (x*10000);
Sh.splitat ("Mydb.location", {"HostID":p Refix})
}
Then move the chunk to the corresponding place, move the chunk command
Sh.movechunk ("Mydb.location", {hostid: "3000000"}, "shard0000")
==shard0000
for (var x=300; x<310; x + +) {
var prefix = String (x*10000);
Sh.movechunk ("Mydb.location", {"HostID":p Refix}, "shard0000")
}
==shard0001 310-320
==shard0002 320-330
==shard0003 330-350
Some other query operations
Find data that is greater than 340000, and
Db.chunks.find ({ns: "mydb.location", min:{$gte: {"HostID": "3400000"}})
Db.getsiblingdb ("admin"). RunCommand ({movechunk: "Mydb.location",
Find: {"HostID": "3400000"},
To: "Shard0003"})
1. Building an Index
Use MyDB
Db.location.ensureIndex ({"HostID": 1})
Db.location.ensureIndex ({"Postime": 1})
Db.location.ensureIndex ({"Hostno": 1})
Db.location.createIndex ({loc: "2dsphere"})
Db.location.ensureIndex ({"Postime": 1, "loc": "2dsphere"})
Db.location.ensureIndex ({"Postime": 1, "Hostno": 1, "loc": "2dsphere"})
Db.location.getIndexes ()
A summary of the commands for MongoDB manual sharding