Step 1 Create two folders using the folder names given below in c:\data folder to for the config servers log and data files.
• rs1
• rs2
• rs3
Create Replica set by name test for Config Servers
start mongod --configsvr -replSet test -logpath \data\rs1\1.log --dbpath \data\rs1 --port 2001
start mongod --configsvr -replSet test -logpath \data\rs2\2.log --dbpath \data\rs2 --port 2002
start mongod --configsvr -replSet test -logpath \data\rs3\2.log --dbpath \data\rs3 --port 2003
From the command prompt connect to mongo instance 2001 by typing the following command
mongo - -port 2001
Type the config parameters as given below
config = {_id:"test",members:[
{_id:0,host:"localhost:2001"},
{_id:1,host:"localhost:2002"},
{_id:2,host:"localhost:2003"}]};
Initiate configuration as given below
rs.initiate(config);
{ "ok" : 1 }
Check the status of the replica set
rs.status()
Open a new command prompt and connect to Mongo instance on port number 2002 from the bin folder by running the following command
mongo --port 2002
Create two shards
Create two folders in c:\data\shard using the following folder names to hold the data for shards
• S0
• S1
Create a folder by name log in both the s0 and s1 folders to hold the log files.
Open command prompt and start two shards as given below
start mongod --shardsvr --replSet shardreplica --port 2005 -logpath \data\shard\s0\log\s0.log --dbpath \data\shard\s0
start mongod --shardsvr --replSet shardreplica --port 2006 -logpath \data\shard\s1\log\s1.log --dbpath \data\shard\s1
Configure shard 2 replica
config = {_id:"shardreplica",members:[
{_id:0,host:"localhost:2005"},
{_id:2,host:"localhost:2006"}]};
Initiate configuration as given below
rs.initiate(config);
{ "ok" : 1 }
1 row created.
Check the status of the replica set
rs.status();
mongo --port 2006
Shard 2
start mongod --shardsvr --replSet shardreplica2 --port 2007 -logpath \data\shard\s0\log\s0.log --dbpath \data\shard\s0
start mongod --shardsvr --replSet shardreplica2 --port 2008 -logpath \data\shard\s1\log\s1.log --dbpath \data\shard\s1
Configure shard 2 replica
config = {_id:"shardreplica2",members:[
{_id:0,host:"localhost:2007"},
{_id:2,host:"localhost:2008"}]};
Initiate configuration as given below
rs.initiate(config);
{ "ok" : 1 }
Check the status of the replica set
rs.status();
mongo --port 2008
start a routing Service
start mongos --port 2009 --configdb test/localhost:2001
Connect to mongos running on port 2009 using the following command
C:\Program Files\MongoDB\Server\3.4\bin>mongo --port 2009
Add shards to the router
mongos>
sh.addShard("localhost:2005")
sh.addShard("localhost:2007")
Enable Sharding
sh.enableSharding("vitdb")
Shard a Collection
sh.shardCollection("vitdb.employee", {"emp_id" : "hashed"})