Skip to content

gianni-smdt/mongodb-sharded-cluster-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

Prerequisites

  • Your device is running a recent version of Linux, Windows or macOS
  • Docker and Docker Compose are already installed on your device
  • You'll need a MongoDB client in order to connect to the cluster (you may use the MongoDB Shell or MongoDB Compass)

Start, Run & Connect

  1. Launch a command line terminal (e.g. bash, zsh, cmd) and start the container for the configuration server:
docker compose up -d configsvr
  1. Connect to the configuration server from the MongoDB Shell:
mongosh mongodb://<Your IP Address>:10001
  1. Initialize the configuration server:
rs.initiate(
  {
    _id: "config_rs",
    configsvr: true,
    members: [
      {_id: 0, host: "<Your IP Address>:10001"}
    ]
  }
)

If you want to check if the initialization was successful, you can use:

rs.status()

Leave the mongosh session with:

exit
  1. Start the containers for the shards:
docker compose up -d mongodb1 mongodb2 mongodb3
  1. Connect to any of the shards:
mongosh mongodb://<Your IP Address>:20001
  1. Initialize the shards:
rs.initiate(
  {
    _id: "shard_rs",
    members: [
      {_id: 0, host: "<Your IP Address>:20001"},
      {_id: 1, host: "<Your IP Address>:20002"},
      {_id: 2, host: "<Your IP Address>:20003"}
    ]
  }
)

Check if the initialization went well:

rs.status()

Leave the mongosh session with:

exit
  1. Start the container for the "MongoDB Shard Utility":
docker compose up -d mongos
  1. Connect to the router via mongosh:
mongosh mongodb://<Your IP Address>:30001
  1. Add the shards to the cluster:
sh.addShard("shard_rs/<Your IP Address>:20001,<Your IP Address>:20002,<Your IP Address>:20003")

Check if all shards were added:

rs.status()

Releases

No releases published

Packages

No packages published