This repository uses docker compose to create 9 container
- sh0-mongo1, sh0-mongo2, sh0-mongo3
- sh1-mongo1, sh1-mongo2, sh1-mongo3
- configdb1, configdb2
- mongos
Three root users will be created with same password
- tom-sh0
- tom-sh1
- tom-mongos
- Build up all containers
docker compose up -d --build
- Run script at container and in the order of sh0-mongo1, sh1-mongo1, configdb1, mongos.
/config/init.sh
-
Access mongos with url:
mongodb://tom-mongos:jerry@localhost:27041/?directConnection=true&authMechanism=DEFAULT
and runsh.status()
- you can try to run
db.getUsers()
to check if they are using different user data
- you can try to run
-
Run command at mongos shell to insert test data
sh.enableSharding('test')
sh.shardCollection("test.sales", { "_id": "hashed" })
// inserting data for testing
var largeArray = []
for (let index = 0; index < 777; index++) {
largeArray.push(
{ 'item': Math.random().toString().substring(2, 9), 'price': Math.random() * 100000, 'quantity': Math.random() * 100, 'date': new Date() }
)
}
db.sales.insertMany(largeArray)
db.adminCommand( { flushRouterConfig: "test.sales" } );
db.getSiblingDB("test").sales.getShardDistribution();
This what i got after inserted 233K documents
[direct: mongos] test> db.getSiblingDB("test").sales.getShardDistribution();
Shard sh0 at sh0/sh0-mongo1:27017,sh0-mongo2:27017,sh0-mongo3:27017
{
data: '9.66MiB',
docs: 116506,
chunks: 2,
'estimated data per chunk': '4.83MiB',
'estimated docs per chunk': 58253
}
---
Shard sh1 at sh1/sh1-mongo1:27017,sh1-mongo2:27017,sh1-mongo3:27017
{
data: '9.69MiB',
docs: 116825,
chunks: 2,
'estimated data per chunk': '4.84MiB',
'estimated docs per chunk': 58412
}
---
Totals
{
data: '19.35MiB',
docs: 233331,
chunks: 4,
'Shard sh0': [
'49.93 % data',
'49.93 % docs in cluster',
'87B avg obj size on shard'
],
'Shard sh1': [
'50.06 % data',
'50.06 % docs in cluster',
'87B avg obj size on shard'
]
}