Complete MongoDB sharded cluster development environment using Docker. Easy for local development and testing.
A complete MongoDB sharded cluster Docker solution designed for local development environments. Run a full MongoDB sharded cluster in a single Docker container.
| Component | Port Range | Description |
|---|---|---|
| mongos routers | 27017-27019 | 3 mongos instances providing client connection endpoints |
| config servers | 27020-27022 | 3 config server replica set members |
| shard servers | 27023-27025 | 3 replica set members for the first shard |
Clone the repository
git clone https://github.com/deadjoe/mongodb-sharded-cluster-dev.git
cd mongodb-sharded-cluster-dev
Build Docker image
docker build -t local-mongo-cluster .
Run the cluster
docker run -d --name mongo-cluster-dev \
-p 27017:27017 \
-p 27018:27018 \
-p 27019:27019 \
-p 27020:27020 \
-p 27021:27021 \
-p 27022:27022 \
-p 27023:27023 \
-p 27024:27024 \
-p 27025:27025 \
local-mongo-cluster
View startup logs
docker logs mongo-cluster-dev -f
Start|Stop container
docker start|stop mongo-cluster-dev
Remove container
docker rm mongo-cluster-dev
Connect to the cluster using mongosh and check status:
mongosh mongodb://localhost:27017
Execute in mongosh:
// View sharded cluster status
sh.status()
// View replica set status
rs.status()
If you want to use a graphical interface to manage your MongoDB cluster, you can install MongoDB Compass:
Install using Homebrew (recommended):
brew install mongodb-compass
Official installation:
Visit the MongoDB Compass website to download the version appropriate for your operating system.
Connect to cluster:
After launching MongoDB Compass, use the following connection string:
mongodb://localhost:27017
mongodb://localhost:27017mongodb://localhost:27017,localhost:27018,localhost:27019mongodb://localhost:27023,localhost:27024,localhost:27025// Node.js application example
const { MongoClient } = require('mongodb');
const uri = "mongodb://localhost:27017,localhost:27018,localhost:27019";
const client = new MongoClient(uri);
async function run() {
try {
await client.connect();
console.log("Connected to MongoDB sharded cluster");
// Use database
const db = client.db("myapp");
const collection = db.collection("users");
// Perform operations...
} finally {
await client.close();
}
}
// Connect to mongos
use myapp
// Enable database sharding
sh.enableSharding("myapp")
// Create shard key for collection
sh.shardCollection("myapp.users", {"_id": "hashed"})
Following the detailed instructions in the init-shard.js file, you can easily add more shards:
Dockerfile to add new data directories and portsstart-cluster.sh to start new shardssh.addShard()// Check shard status
sh.status()
// Check replica set status
rs.status()
// Check balancer status
sh.getBalancerState()
/data/mongos1.log, /data/mongos2.log, /data/mongos3.log/data/config1/config1.log, /data/config2/config2.log, /data/config3/config3.log/data/shard1a/shard1a.log, /data/shard1b/shard1b.log, /data/shard1c/shard1c.logsh.startBalancer() to manually trigger balancing| Filename | Type | Description |
|---|---|---|
| Markdown Documentation | ||
| README.md | .md | Main project documentation with complete usage instructions and configuration guide |
| CONFIG.md | .md | MongoDB sharded cluster test script configuration guide, detailing test-cluster.sh configuration parameters |
| JavaScript Configuration Scripts | ||
| init-replica.js | .js | Config server replica set initialization script for setting up configReplSet |
| init-shard.js | .js | Shard replica set initialization script for setting up shard1, includes detailed guidance for extending to a second shard |
| init-router.js | .js | mongos router configuration script for adding shards to the cluster |
| Shell Scripts | ||
| start-cluster.sh | .sh | Cluster startup script that starts all MongoDB sharded cluster components in the correct order |
| test-cluster.sh | .sh | Complete MongoDB sharded cluster test script with 11 test phases and detailed debugging capabilities |
| validate-tests.sh | .sh | Test script validation tool for verifying all test commands in test-cluster.sh |
.
├── Dockerfile # Docker image definition
├── start-cluster.sh # Cluster startup script
├── init-replica.js # Config server replica set initialization
├── init-shard.js # Shard replica set initialization
├── init-router.js # mongos router configuration
├── test-cluster.sh # Cluster test script
├── validate-tests.sh # Test validation tool
├── CONFIG.md # Test script configuration guide
├── README.md # Project documentation
└── LICENSE # MIT License
If you encounter issues during usage:
docker logs mongo-cluster-devThis project is licensed under the MIT License. See the LICENSE file for details.
Issues and Pull Requests are welcome. Please ensure:
Note: This project is primarily for development and testing environments. When using in production, please adjust configuration and security settings according to actual requirements.