- Config Server:
configsvr01 - 2 Shards (each a 2 member
PSSreplica set):shard01-a,shard01-b,shard01-cshard02-a,shard02-b,shard02-c
- 1 Routers (mongos):
router01
docker-compose up -ddocker-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")
// Setup shardingKey for collection `MyCollection`**
db.adminCommand( { shardCollection: "MyDatabase.MyCollection", key: { oemNumber: "hashed", zipCode: 1, supplierId: 1 } } )
!!! If you want to add new shard to existed cluster, check more here
📋 Verify 🔝
✅ Verify the status of the sharded cluster 🔝
docker-compose exec router01 mongosh --port 27017
sh.status()✅ Verify status of replica set for each shard 🔝
You should see 1 PRIMARY, 2 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"