Dear all,
I’ve been following the book “MongoDB The Definitive Guide, 3rd Ed” to grasp some knowledge about sharding. Please take a look at the following steps that I did based on the book’s example:
use sample
for (var i=0; i<100000; i++) {
db.users.insert({“username”: “user”+i, “created_at”: new Date()});
}sh.enableSharding(“sample”)
db.users.createIndex({“username”: 1})
sh.shardCollection(“sample.users”, {“username” : 1})
db.adminCommand( { flushRouterConfig: “sample.users” } );
After a while, I ran the sh.status() command to check whether data get distributed across shards.
mongos> sh.status()
— Sharding Status —
sharding version: {
“_id” : 1,
“minCompatibleVersion” : 5,
“currentVersion” : 6,
“clusterId” : ObjectId(“6107bf46d86674a86df43f0c”)
}
shards:
{ “_id” : “rs1”, “host” : “rs1/mongo-01:27011,mongo-02:27011,mongo-03:27011”, “state” : 1, “tags” : [ “zone1”, “zone2” ] }
{ “_id” : “rs2”, “host” : “rs2/mongo-01:27012,mongo-02:27012,mongo-03:27012”, “state” : 1, “tags” : [ “zone2” ] }
most recently active mongoses:
“4.4.3” : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ “_id” : “config”, “primary” : “config”, “partitioned” : true }
config.system.sessions
shard key: { “_id” : 1 }
unique: false
balancing: true
chunks:
rs1 512
rs2 512
too many chunks to print, use verbose if you want to force print
{ “_id” : “sample”, “primary” : “rs2”, “partitioned” : true, “version” : { “uuid” : UUID(“99233e26-17ba-440d-b8ca-a1e6a206e2f5”), “lastMod” : 1 } }
sample.users
shard key: { “username” : 1 }
unique: false
balancing: true
chunks:
rs2 1
{ “username” : { “$minKey” : 1 } } -->> { “username” : { “$maxKey” : 1 } } on : rs2 Timestamp(1, 0)mongos> db.getSiblingDB(“sample”).users.getShardDistribution();
Shard rs2 at rs2/mongo-01:27012,mongo-02:27012,mongo-03:27012
data : 6.28MiB docs : 100000 chunks : 1
estimated data per chunk : 6.28MiB
estimated docs per chunk : 100000Totals
data : 6.28MiB docs : 100000 chunks : 1
Shard rs2 contains 100% data, 100% docs in cluster, avg obj size on shard : 65B
As you can see from the output, data reside on only 1 shard. I’ve been studying on the MongoDB forum and the internet, but haven’t found the solution. So I’ve created the topic, please have a look and help me out.
Thanks,
Thang