Skip to content

Guide to MongoDB sharding using Docker that works also in offline mode.

License

Notifications You must be signed in to change notification settings

giuseppe998e/mongodb-sharding

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoDB Sharding using Docker containers

Automatic Setup

$ chmod +x build.sh
$ ./build.sh

Manual Setup

Local network

Create a new docker network

$ docker network create mongodb-net

(It allows servers to communicate with each other even in offline mode)

Config servers

Start config servers (3 member replica set)

$ docker-compose -f config-server/docker-compose.yaml up -d

Initiate replica set

$ docker exec -it cfgsvr1 mongo
> rs.initiate(
    {
      _id: "cfgrs",
      configsvr: true,
      members: [
        { _id: 0, host: "cfgsvr1" },
        { _id: 1, host: "cfgsvr2" },
        { _id: 2, host: "cfgsvr3" }
      ]
    }
  )

> rs.status()

Shard 1 servers

Start shard 1 servers (3 member replicas set)

$ docker-compose -f shard1/docker-compose.yaml up -d

Initiate replica set

$ docker exec -it shard1svr1 mongo
> rs.initiate(
    {
      _id: "shard1rs",
      members: [
        { _id: 0, host: "shard1svr1" },
        { _id: 1, host: "shard1svr2" },
        { _id: 2, host: "shard1svr3" }
      ]
    }
  )

> rs.status()

Mongos Router

Start mongos query router

$ docker-compose -f mongos/docker-compose.yaml up -d

Add shard to the cluster

Connect to mongos

$ docker exec -it mongos mongo

Add shard

> sh.addShard("shard1rs/shard1svr1,shard1svr2,shard1svr3")

> sh.status()

Add another shard

Start shard 2 servers (3 member replicas set)

$ docker-compose -f shard2/docker-compose.yaml up -d

Initiate replica set

$ docker exec -it shard2svr1 mongo
> rs.initiate(
    {
      _id: "shard2rs",
      members: [
        { _id: 0, host: "shard2svr1" },
        { _id: 1, host: "shard2svr2" },
        { _id: 2, host: "shard2svr3" }
      ]
    }
  )

> rs.status()

Repeat "Add shard to the cluster " replacing shard1 with shard2 for each occurrence.

Notices

License

  • This guide is released under the CC BY-SA 4.0 license.
  • The codes included with this guide are released under the MIT license.

Attribution

This guide is based on justmeandopensource/learn-mongodb (unlicensed).