Sharding is a method for distributing data across multiple machines. MongoDB uses sharding to support deployments with very large data sets and high throughput operations. Database systems with large data sets or high throughput applications can challenge the capacity of a single server.

In this exercise, will be using one single server with three set mongodb shard instances each with three replica sets running under different port number with a total of nine replica sets. The environment in this instance is Microsoft Windows, but this can easily be reproduced in Linux servers.

Step 1 – Create three replica sets for Shard Set 1 (S1)

  • start C:\MongoDB\Server\3.2\bin\mongod.exe –shardsvr –port 27017 –logpath “C:\data\Shard\S1\R1\log\shard1_1.log” –logappend –dbpath “C:\data\Shard\S1\R1\db” –replSet shard1_replset
  • start C:\MongoDB\Server\3.2\bin\mongod.exe –shardsvr –port 27117 –logpath “C:\data\Shard\S1\R2\log\shard1_2.log” –logappend –dbpath “C:\data\Shard\S1\R2\db” –replSet shard1_replset
  • start C:\MongoDB\Server\3.2\bin\mongod.exe –shardsvr –port 27217 –logpath “C:\data\Shard\S1\R3\log\shard1_3.log” –logappend –dbpath “C:\data\Shard\S1\R3\db” –replSet shard1_replset
  • Login to primary set S1R1
    • mongo.exe –host localhost –port 27017
  • Initiate Replica Set for S1R1
    • rs.initiate(
    • {
    • _id: “shard1_replset”,
    • members: [
    • { _id : 0, host : “CLUSTER_NODE:27017” },
    • { _id : 1, host : “CLUSTER_NODE:27117” },
    • { _id : 2, host : “CLUSTER_NODE:27217” }
    • ]
    • }
    • )

Step 2 – Create three replica sets for Shard Set 2 (S2)

  • start C:\MongoDB\Server\3.2\bin\mongod.exe –shardsvr –port 28017 –logpath “C:\data\Shard\S2\R1\log\shard1_1.log” –logappend –dbpath “C:\data\Shard\S2\R1\db” –replSet shard2_replset
  • start C:\MongoDB\Server\3.2\bin\mongod.exe –shardsvr –port 28117 –logpath “C:\data\Shard\S2\R2\log\shard1_2.log” –logappend –dbpath “C:\data\Shard\S2\R2\db” –replSet shard2_replset
  • start C:\MongoDB\Server\3.2\bin\mongod.exe –shardsvr –port 28217 –logpath “C:\data\Shard\S2\R3\log\shard1_3.log” –logappend –dbpath “C:\data\Shard\S2\R3\db” –replSet shard2_replset
  • Login to primary set (–port 28017) on Shard 2 S2R1
    • mongo.exe –host localhost –port 28017
  • Initiate Replica Set for S2R1
    • rs.initiate(
    • {
    • _id: “shard2_replset”,
    • members: [
    • { _id : 0, host : “CLUSTER_NODE:28017” },
    • { _id : 1, host : “CLUSTER_NODE:28117” },
    • { _id : 2, host : “CLUSTER_NODE:28217” }
    • ]
    • }
    • )

Step 3 – Create three replica sets for Shard Set 3 (S3)

  • start C:\MongoDB\Server\3.2\bin\mongod.exe –shardsvr –port 29017 –logpath “C:\data\Shard\S3\R1\log\shard1_1.log” –logappend –dbpath “C:\data\Shard\S3\R1\db” –replSet shard3_replset
  • start C:\MongoDB\Server\3.2\bin\mongod.exe –shardsvr –port 29117 –logpath “C:\data\Shard\S3\R2\log\shard1_2.log” –logappend –dbpath “C:\data\Shard\S3\R2\db” –replSet shard3_replset
  • start C:\MongoDB\Server\3.2\bin\mongod.exe –shardsvr –port 29217 –logpath “C:\data\Shard\S3\R3\log\shard1_3.log” –logappend –dbpath “C:\data\Shard\S3\R3\db” –replSet shard3_replset
  • Login to primary set (–port 29017) on Shard 3 S3R1
    • mongo.exe –host localhost –port 29017
  • Initiate Replica Set for S3R1
    • rs.initiate(
    • {
    • _id: “shard3_replset”,
    • members: [
    • { _id : 0, host : “CLUSTER_NODE:29017” },
    • { _id : 1, host : “CLUSTER_NODE:29117” },
    • { _id : 2, host : “CLUSTER_NODE:29217” }
    • ]
    • }
    • )

Step 4 – Create a Configuration Server

  • start C:\MongoDB\Server\3.2\bin\mongod.exe –configsvr –port 47017 –logpath “C:\data\Shard\ConfigSrv\Svr1\log\configsvr1.log” –logappend –dbpath “C:\data\Shard\ConfigSrv\Svr1\db” –replSet configserver1_replset
  • start C:\MongoDB\Server\3.2\bin\mongod.exe –configsvr –port 47117 –logpath “C:\data\Shard\ConfigSrv\Svr2\log\configsvr2.log” –logappend –dbpath “C:\data\Shard\ConfigSrv\Svr2\db” –replSet configserver1_replset
  • start C:\MongoDB\Server\3.2\bin\mongod.exe –configsvr –port 47217 –logpath “C:\data\Shard\ConfigSrv\Svr3\log\configsvr3.log” –logappend –dbpath “C:\data\Shard\ConfigSrv\Svr3\db” –replSet configserver1_replset
  • Login to primary config server set (–port 47017) on configuration server
    • mongo.exe –host localhost –port 47017
  • Run replicaset initiate
    • rs.initiate(
      {
      _id: “configserver1_replset”,
      members:
      [
      { _id : 0, host : “CLUSTER_NODE:47017” },
      { _id : 1, host : “CLUSTER_NODE:47117” },
      { _id : 2, host : “CLUSTER_NODE:47217” }
      ]
      }
      )

Step 5 – Create Mongos Server

  • mongos.exe –configdb configserver1_replset/CLUSTER_NODE:47017,CLUSTER_NODE:47117,CLUSTER_NODE:47217 –port 1000
  • mongos.exe –configdb configserver1_replset/CLUSTER_NODE:47017,CLUSTER_NODE:47117,CLUSTER_NODE:47217 –port 1001
  • mongos.exe –configdb configserver1_replset/CLUSTER_NODE:47017,CLUSTER_NODE:47117,CLUSTER_NODE:47217 –port 1002
  • Login to primary config server set (–port 1000) on configuration server
    • mongo.exe –host CLUSTER_NODE –port 1000
  • Add replicaset to Shard Server
    • sh.addShard(“shard1_replset/CLUSTER_NODE:27017,CLUSTER_NODE:27117,CLUSTER_NODE:27217”)
    • sh.addShard(“shard2_replset/CLUSTER_NODE:28017,CLUSTER_NODE:28117,CLUSTER_NODE:28217”)
    • sh.addShard(“shard3_replset/CLUSTER_NODE:29017,CLUSTER_NODE:29117,CLUSTER_NODE:29217”)
  • Enable Sharding for Database “Fruit”
    • sh.enableSharding(“Fruit_mds”)
  • Enable Sharding for Collection “company_name” in Database “Fruit”
    • sh.shardCollection(“Fruit.company”,{“company_name”:1});
  • Set a db instance that you want to sharded
    • mongos> db.runCommand({ enablesharding:”Fruit” })
  • Which db to be sharded
    • mongos> db.runCommand({ shardcollection: “Fruit.log”, key: { id:1,time:1}})