All stories
Java

Large Scale Deployment Scenario of MongoDB

H
hemant-kumar

July 20, 2012


Here in this example we will use Mongo DB for large scale deployment scenario. In this example if the primary node of any shard will go down then it would be automatically replaced by its secondary node (now secondary will start acting as a primary node). Also in this example we have configured 3 configuration servers. That means that the system will fine with one or two configuration servers down also. Also we have used two mongos routers. Therefore we can connect to the system using any of the routers.




 Shard 1
Run the 3 servers.
mongod --shardsvr --dbpath E:\MongoShards\A --port 20000 --rest --replSet setA
mongod --shardsvr --dbpath E:\MongoShards\Areplica --port 20010 --rest --replSet setA
mongod --shardsvr --dbpath E:\MongoShards\Aarb --port 20020 --rest --replSet setA

Connect to mongo on first node.
mongo --port 20000

Now initiate the node.
rs.initiate()

Add full nodes.
rs.add(“<your ip>:20010”)

Add arbitrary node
rs.addArb(“<your ip>:20020”)


Shard 2
Run the 3 servers.
mongod --shardsvr --dbpath E:\MongoShards\B --port 20001 --rest --replSet setB
mongod --shardsvr --dbpath E:\MongoShards\Breplica --port 20011 --rest --replSet setB
mongod --shardsvr --dbpath E:\MongoShards\Barb --port 20021 --rest --replSet setB

Connect to mongo on first node.
mongo --port 20001

Now initiate the node.
rs.initiate()

Add full nodes.
rs.add(“<your ip>:20011”)

Add arbitrary node
rs.addArb(“<your ip>:20021”)


Shard 3
Run the 3 servers.
mongod --shardsvr --dbpath E:\MongoShards\C --port 20002 --rest --replSet setC
mongod --shardsvr --dbpath E:\MongoShards\Creplica --port 20012 --rest --replSet setC
mongod --shardsvr --dbpath E:\MongoShards\Carb --port 20022 --rest --replSet setC

Connect to mongo on first node.
mongo --port 20002

Now initiate the node.
rs.initiate()

Add full nodes.
rs.add(“<your ip>:20012”)

Add arbitrary node
rs.addArb(“<your ip>:20022”)


Configuration Servers 
Run the 3 configuration servers on port no 20003, 20004 and 20005 respectively.
mongod --configsvr --dbpath E:\MongoShards\config1 --port 20003
mongod --configsvr --dbpath E:\MongoShards\config2 --port 20004
mongod --configsvr --dbpath E:\MongoShards\config3 --port 20005


Mongos Routers 
Now start the first mongos router on port no 20006.
mongos --configdb <your ip>:20003, <your ip>:20004, <your ip>:20005 --port 20006

Now connect to the Mongos router on port no 20006 using mongo.exe
mongo.exe –host <your ip> --port 20006

Now type
use admin
db.runCommand({addShard:"setA/Hemant-PC:20000"})
db.runCommand({addShard:"setB/Hemant-PC:20001"}) 
db.runCommand({addShard:"setC/Hemant-PC:20002"})

 Here “Hemant-PC” is the name of the primary node.

Check for the primary node.
http://<ip address>:<port no>/_replSet

Note: Here port no = port no on which mongod is running + 1000

Now enable sharding on database named “mydb”.
db.runCommand( { enablesharding : "mydb" } )
  
Now enable sharding on “_id” field of collection named “myCollection”.
db.runCommand( { shardcollection : "mydb.myCollection", key : { _id : 1 } } )

 Check for shard configuration formation.
 db.runCommand( { listshards : 1 } )

Note: Now start the second mongos router on port no 20007 and apply the same procedure.
Once done now connect to either of the routers and start working.


Java

0

If you found this helpful, give it some claps!

SHARE THIS ARTICLE

Share on X
LinkedIn

Responses0

Sign in to join the conversation

Sign in