MongoDB Sharding - Step By Step With Example

Author
April 10, 2022

In this article i will explain you step by step to create replica set in mongodb 5.0 community edition.Sharding is a method for distributing a single dataset across multiple databases, which can then be stored on multiple machines.

Step 1: Create directories for shards

We will create two shards named shard1, shard2 so create two folders with the name shard1 and shard2 and inside shard1 folder create three folder db11, db12, db13 and inside shard2 folder create three folder db21, db22, db23 you can choose any location and directories in your computer to create these folders.

sharding-directories1

sharding-directories2

Step 2: Creating mongodb instances for shard1 replica set and initiating replica set

Now we will create mongodb instances for db11, db22, db23 on port 27117, 27127, 27137

Command for creating mongodb instances with replica set shard1 on port 27117, 27127, 27137

/* replace the port number and folder path here
  and execute the same command for port 27127, 27137 and in separate tab/window of terminal */
mongod --shardsvr --dbpath `your folder path for db11` --replSet shard1 --port 27117

creating-mongdb-instance

After all the three mongodb instance 27117, 27127, 27137 has been created/started then initiate the replca set

// start this mongo instance in new tab or window of terminal don't close the above server you have started
mongo --port 27117

// type this command after you have started the instance
var configuration = {
  _id: "shard1",
  members: [
    { _id: 0, host: "localhost:27117" },
    { _id: 1, host: "localhost:27127" },
    { _id: 2, host: "localhost:27137" },
  ],
};

// type this after above variable initialized
rs.initiate(configuration);

initiate-replica-set

Step 3: Creating mongodb instances for shard2 replica set and initiating replica set

/*replace the port number and folder path here and execute
the same command for port 27227, 27237 and in separate tab/window of terminal
also remember to put --shardsvr flag
*/

mongod --shardsvr --dbpath `your folder path for db21` --replSet shard2 --port 27217

After all the three mongodb instance 27217, 27227, 27237 has been created/started then initiate the replca set

// start this mongo instance in new tab or window of terminal don't close the above server you have started
mongo --port 27217

// type this command after you have started the instance
var configuration = {
  _id: "shard2",
  members: [
    { _id: 0, host: "localhost:27217" },
    { _id: 1, host: "localhost:27227" },
    { _id: 2, host: "localhost:27237" },
  ],
};

// type this after above variable initialized
rs.initiate(configuration);

initiate-replica-set

Step 4: Creating config replica set

Config replica set stores all the important configuration needed to run the sharded cluster and it has following restrictions

no arbiters

no delayed members

all members must build indexes

We need to create three directories for configuration server same as we did for shard1 and shard2 replica set you can choose the folder names as per your choice

config-replica-set

Same as above we need to start the three mongod server on port 30117, 30127, 30137 and replica set name configReplica

/*replace the port number and folder path here and execute
the same command for port 30127, 30137 and in separate tab/window of terminal
also make sure to pass the flag --configsvr which makes these server config server
also remember to put --shardsvr flag
*/

mongod --dbpath `your folder path for config1` --replSet configReplica --port 30117 --configsvr

After all the three mongodb instance 30117, 30127, 30137 has been created/started then initiate the replca set

// start this mongo instance in new tab or window of terminal don't close the above server you have started
mongo --port 30117

// type this command after you have started the instance
var configuration = {
  _id: "configReplica",
  configsvr:true,
  members: [
    { _id: 0, host: "localhost:30117" },
    { _id: 1, host: "localhost:30127" },
    { _id: 2, host: "localhost:30137" },
  ],
};

// type this after above variable initialized
rs.initiate(configuration);

config-replica-set-initiate

Step 5: Starting mongos query router and connect to it

We need to start our query router i.e mongos to start serving queries from different shard mongos which is our query router sends the query to each shards and fetch the result

We will start the mongos on port 27017 which is default port

mongos --configdb configReplica/localhost:30117,localhost:30127,localhost:30137

start-mongos

Step 6: Connect to mongos and add replica set to the shard

After executing above steps we need connect to mongos and add the replica set to the shard

mongo;

//executing this command after connection successful shard1 is the name of replica set
sh.addShard("shard1/localhost:27117");

//adding second replica set that is shard2
sh.addShard("shard2/localhost:27217");

After the above steps you are able to successfully create sharded cluster with two shards shard1 and shard2

Step 7: Watch our you tube video to follow the above steps