Skip to content

A production-ready MongoDB sharding cluster with Docker Compose, featuring 3 config servers, 7 shard servers, and a Mongos router. Includes automated initialization, resource optimization, persistent volumes, and MongoDB 8.0.4 support.

Notifications You must be signed in to change notification settings

imana97/MongoDB-docker-compose-cluster

Repository files navigation

MongoDB Sharding Cluster Setup

This guide explains how to set up a MongoDB sharding cluster using Docker Compose and how to use it in a Node.js application.

Prerequisites

  • Docker
  • Docker Compose
  • Node.js

Setup

Step 1: Start the Docker Containers

Run the following command to start the MongoDB containers:

docker-compose up -d

Step 2: Customize Docker Compose

You can customize the Docker Compose file to suit your needs. For example, you can change the memory limits, CPU limits, and volume paths.

Example for Windows

volumes:
  - F:/Database/MongoDBCluster/configs1:/data/db

Example for Linux

volumes:
  - /mnt/f/Database/MongoDBCluster/configs1:/data/db

Step 3: Initiate the Cluster

For Linux/MacOS

First, make the script executable:

chmod +x initiate-cluster.sh

Then, run the script:

./initiate-cluster.sh

For Windows

Run the batch script:

initiate-cluster.bat

This script will set up the config server replica set, the shard replica set, and add the shard to the cluster.

Step 4: Using the Cluster in a Node.js Application

To connect to the MongoDB sharding cluster from a Node.js application, use the following connection string:

const { MongoClient } = require('mongodb');

async function main() {
  const uri = "mongodb://localhost:30000";
  const client = new MongoClient(uri);

  try {
    await client.connect();
    console.log("Connected to MongoDB sharding cluster");
    // ... your application code ...
  } finally {
    await client.close();
  }
}

main().catch(console.error);

Replace // ... your application code ... with your actual application logic.

Step 5: Stopping the Cluster

To stop the MongoDB containers, run:

docker-compose down

This will stop and remove the containers.

Credits

Credit to Iman Far (imanfar.com)

About

A production-ready MongoDB sharding cluster with Docker Compose, featuring 3 config servers, 7 shard servers, and a Mongos router. Includes automated initialization, resource optimization, persistent volumes, and MongoDB 8.0.4 support.

Topics

Resources

Stars

Watchers

Forks