- Config Server:
configsvr01
- 2 Shards (each a
PS
replica set):shard01-a
,shard01-b
shard02-a
,shard02-b
- 1 Routers (mongos):
router01
docker compose up -d
docker compose exec configsvr01 sh -c "mongosh < /scripts/init-configserver.js"
docker compose exec shard01-a sh -c "mongosh < /scripts/init-shard01.js"
docker compose exec shard02-a sh -c "mongosh < /scripts/init-shard02.js"
Note: Wait a bit for the config server and shards to elect their primaries before initializing the router
docker compose exec router01 sh -c "mongosh < /scripts/init-router.js"
docker compose exec router01 mongosh --port 27017
// Enable sharding for database `MyDatabase`
sh.enableSharding("MyDatabase")
// Lower chunk size to help force shading
use config
db.settings.updateOne({ _id: "chunksize" }, { $set: { _id: "chunksize", value: 5 } }, { upsert: true })
// Setup shardingKey for collection `MyCollection`**
db.adminCommand( { shardCollection: "MyDatabase.MyCollection", key: { oemNumber: "hashed", zipCode: 1, supplierId: 1 } } )
docker compose exec router01 mongosh --port 27017
sh.status()
You should see 1 PRIMARY, 1 SECONDARY
docker exec -it shard-01-node-a bash -c "echo 'rs.status()' | mongosh --port 27017"
docker exec -it shard-02-node-a bash -c "echo 'rs.status()' | mongosh --port 27017"
docker compose exec router01 mongosh --port 27017
use MyDatabase
db.stats()
db.MyCollection.getShardDistribution()
docker exec -it mongo-config-01 bash -c "echo 'rs.status()' | mongosh --port 27017"
docker exec -it shard-01-node-a bash -c "echo 'rs.help()' | mongosh --port 27017"
docker exec -it shard-01-node-a bash -c "echo 'rs.status()' | mongosh --port 27017"
docker exec -it shard-01-node-a bash -c "echo 'rs.printReplicationInfo()' | mongosh --port 27017"
docker exec -it shard-01-node-a bash -c "echo 'rs.printSlaveReplicationInfo()' | mongosh --port 27017"
Modified version of MongoDB (6.0.1) Sharded Cluster with Docker Compose