Skip to content

Latest commit

 

History

History
121 lines (84 loc) · 2.33 KB

README.md

File metadata and controls

121 lines (84 loc) · 2.33 KB

Mongo Sharded Cluster with Docker Compose

Mongo Components

  • Config Server (3 member replica set): cfgsvr1, cfgsvr2, cfgsvr3
  • 3 Shards (each a 3 member replica set):
    • shard1svr1, shard1svr2, shard1svr3
    • shard2svr1, shard2svr2, shard2svr3
    • shard3svr1, shard3svr2, shard3svr3
  • Router (mongos): router

Setup

  • Cloud Server 1 docker-compose -f docker-compose1.yml -d

  • Cloud Server 2 docker-compose -f docker-compose2.yml -d

  • Cloud Server 3 docker-compose -f docker-compose3.yml -d

  • Cloud Router (Application Cloud) docker-compose -f docker-router.yml -d

Configuration

  • Config Server
docker exec -it cfgsvr1
mongo < /scripts/init-configserver.js
  • Shard1
docker exec -it shard1svr1
mongo < /scripts/init-shard1.js
  • Shard2
docker exec -it shard2svr1
mongo < /scripts/init-shard2.js
  • Shard3
docker exec -it shard3svr1
mongo < /scripts/init-shard3.js
  • Router
docker exec -it router
mongo < /scripts/init-router.js

Sharding a Mongo Collection

docker exec -it router
mongo
  • Check status
router> sh.status()

Enable Sharding of Database

sh.enableSharding("dbName")

Shard Empty Collection Data

sh.shardCollection( "dbName.myCollections", {"field": 1, ...} )
sh.shardCollection( "dbName.myCollections", {"field": "hashed", ...} )

Ex:

sh.shardCollection( "dbName.users", { _id: "hashed" } )
sh.shardCollection( "dbName.invoices", { tranType: "hashed", tranDate: "hashed" } )

Shard Exist Collection Data

  • Create Collection Index
db.myCollections.createIndex({"field": "hashed"})
  • Sharding Collection
sh.shardCollection( "dbName.myCollections", {"field": "hashed"} )

Check Collection Sharding

db.myCollections.getShardDistribution()

References