This document provides complete instructions for setting up a production-ready MongoDB sharded cluster using Docker Compose. The cluster includes:
- 3-node Config Server Replica Set
- 3 Shards, each with 3-node Replica Sets
- 2 MongoDB Query Routers (mongos)
- Docker and Docker Compose installed
- MongoDB Shell (mongosh) installed on your host machine
- Installation:
brew install mongosh(macOS) or follow instructions at MongoDB Shell
┌─────────────────────────────────────────────────────────────────────────────┐
│ MONGODB SHARDED CLUSTER │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ CLIENT APPLICATIONS │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Application │ │ Application │ │ Application │ │
│ │ #1 │ │ #2 │ │ #3 │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │ │ │
│ └───────┬──────┴───────┬──────┘ │
│ │ │ │
│ ┌──────▼──────┐ ┌────▼──────┐ │
│ │ MONGOS │ │ MONGOS │ QUERY ROUTERS │
│ │ ROUTER │ │ ROUTER │ (Load Balancers) │
│ │ (mongos1) │ │ (mongos2) │ │
│ │ port:27017 │ │ port:27018│ │
│ └──────┬──────┘ └────┬──────┘ │
│ │ │ │
│ ┌───────┴──────────────┴───────┐ │
│ │ │ │
│ ┌──────▼──────┐ ┌──────▼──────┐ │
│ │ CONFIG │ │ SHARDS │ │
│ │ SERVERS │ │ │ │
│ │ Replica Set │ │ ┌─────────────────────────────────┐ │
│ │ (cfgRS) │ │ │ SHARD 1 (shard1RS) │ │
│ │ │ │ │ ┌─────────┬─────────┬─────────┐ │ │
│ │ ┌─────────┐ │ │ │ │ shard1a │ shard1b │ shard1c │ │ │
│ │ │configsvr1│◄────────────────┼──┼─► 27018 │ 27018 │ 27018 │ │ │
│ │ │ 27019 │ │ │ │ └─────────┴─────────┴─────────┘ │ │
│ │ ├─────────┤ │ │ │ │ │
│ │ │configsvr2││ │ │ SHARD 2 (shard2RS) │ │
│ │ │ 27019 │ │ │ │ ┌─────────┬─────────┬─────────┐ │ │
│ │ ├─────────┤ │ │ │ │ shard2a │ shard2b │ shard2c │ │ │
│ │ │configsvr3│◄────────────────┼──┼─► 27018 │ 27018 │ 27018 │ │ │
│ │ │ 27019 │ │ │ │ └─────────┴─────────┴─────────┘ │ │
│ │ └─────────┘ │ │ │ │ │
│ └─────────────┘ │ │ SHARD 3 (shard3RS) │ │
│ ▲ │ │ ┌─────────┬─────────┬─────────┐ │ │
│ │ │ │ │ shard3a │ shard3b │ shard3c │ │ │
│ │ │ │ │ 27018 │ 27018 │ 27018 │ │ │
│ └────────────────────────┼──┼─┴─────────┴─────────┴─────────┘ │ │
│ │ └─────────────────────────────────┘ │
│ │ │
└─────────────────────────────────┴───────────────────────────────────────────┘
cd MongoDB-archopenssl rand -base64 756 > mongodb-keyfile
chmod 400 mongodb-keyfiledocker-compose up -ddocker-compose pscd MongoDB-archchmod +x init-cluster.sh./init-cluster.shcd MongoDB-archchmod +x create-admin-user.sh./create-admin-user.shcd MongoDB-archchmod +x configure-cluster.sh./configure-cluster.shcd MongoDB-schemachmod +x configure-billing-db-schema.sh./configure-billing-db-schema.shcd MongoDB-RBACchmod +x load-rbac.sh./load-rbac.shTo see all the configured roles, login to MongoDB compass and execute this command

cd MongoDB-indexeschmod +x load-indexes.sh./load-indexes.shcd MongoDB-documentschmod +x load-data.sh./load-data.shCreate a file named setup-cluster.sh with the following content:
chmod +x setup-cluster.sh./setup-cluster.shUse MongoDB Compass Download this tool from this URL: https://www.mongodb.com/products/tools/compass
and once installed
Configure this MongoDB database like this in MongoDB Compass:

cd MongoDB-querieschmod +x Generic_run_query.sh./Generic_run_query.sh query_2After executing the above command, the result will be present in the 'query_results' folder with name as '<query_name>_result.js'. Make sure the 'query_2.js' is present in the folder 'queries'.
You will find postman collection of the simple spring boot application with name as 'MongoDB Production.postman_collection.json'
Can import in postman to test the simple spring boot application
Then run the simple spring boot application, which will start at port 8080
View logs:
docker-compose logs [service_name]Bring down the cluster:
docker-compose down -v