Mongodb as a Replica Set
We would be creating mongodb as a replica set having 3 instances. One instance would be primary and the other 2 instances would be secondary.
We would be creating mongodb as a replica set having 3 instances. One instance would be primary and the other 2 instances would be secondary.
In production environment where in you have a dedicated mongodb instance running on a single server you can reuse the same port numbers.
- Create data directories ( path where mongodb data would be stored in a file)
- mkdir c:\data\server1 (datafile path for instance 1)
- mkdir c:\data\server2 (datafile path for instance 2)
- mkdir c:\data\server3 (datafile path for instance 3)
2. a. Start the first mongod instance
- Open command prompt and type the following press enter.
mongod --replSet s0 --dbpath c:\data\server1 --port 37017 --smallfiles --oplogSize 10
The above command associates the instance of mongodb to a replicaSet name “s0” and the starts the first instance of mongodb on port 37017 with oplogSize 100MB
2. b. Similarly start the second instance of Mongodb
mongod --replSet s0 --dbpath c:\data\server2 --port 37018 --smallfiles --oplogSize 100
The above command associates the instance of mongodb to a replicaSet name “s0” and the starts the first instance of mongodb on port 37018 with oplogSize 100MB
2. c. Now start the third instance of Mongodb
mongod --replSet s0 --dbpath c:\data\server3 --port 37019 --smallfiles --oplogSize 100
The above command associates the instance of mongodb to a replicaSet name “s0” and the starts the first instance of mongodb on port 37019 with oplogSize 100MB
With all the 3 instances started, these 3 instances are independent of each other currently. We would now need to group these instances as a replica set. We do this with the help of a config object.
3.a Connect to any of the mongod servers via the mongo shell. To do that open the command prompt and type.
mongo --port 37017
Once connected to the mongo shell, create a config object
var config = {"_id":"s0", members[]};
this config object has 2 attributes
- _id: the name of the replica Set ( “s0” )
- members: [] (members is an array of mongod instances. lets keep this blank for now, we will add
members via the push command
3.b To Push(add) mongod instances to the members array in the config object. On the mongo shell type
config.members.push({"_id":0,"host":"localhost:37017"});
config.members.push({"_id":1,"host":"localhost:37018"});
config.members.push({"_id":2,"host":"localhost:37019"});
We assign each mongod instance an _id and an host. _id can be any unique number and the host should be the hostname of the server on which its running followed by the port number.
4.Initiate the config object by the following command in the mongo shell.
rs.initiate(config)
- Give it a few seconds and we have a replica set of 3 mongod instances running on the server. type the following command to check the status of the replica set and to identify which one is primary and which one is secondary.
rs.status();
Check MongoDB Replica Set states
Use the below command to check the replica set status.
Command : rs.status()
Connect any one of replica member and fire this command it will give the full state of the replica set
Example :
{
"set" : "ReplicaName",
"date" : ISODate("2016-09-26T07:36:04.935Z"),
"myState" : 1,
"term" : NumberLong(-1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "<IP>:<PORT>,
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 5953744,
"optime" : Timestamp(1474875364, 36),
"optimeDate" : ISODate("2016-09-26T07:36:04Z"),
"electionTime" : Timestamp(1468921646, 1),
"electionDate" : ISODate("2016-07-19T09:47:26Z"),
"configVersion" : 6,
"self" : true
},
{
"_id" : 1,
"name" : "<IP>:<PORT>",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 5953720,
"optime" : Timestamp(1474875364, 13),
"optimeDate" : ISODate("2016-09-26T07:36:04Z"),
"lastHeartbeat" : ISODate("2016-09-26T07:36:04.244Z"),
"lastHeartbeatRecv" : ISODate("2016-09-26T07:36:03.871Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.9.52.55:10050",
"configVersion" : 6
},
{
"_id" : 2,
"name" : "<IP>:<PORT>",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 5953696,
"lastHeartbeat" : ISODate("2016-09-26T07:36:03.183Z"),
"lastHeartbeatRecv" : ISODate("2016-09-26T07:36:03.715Z"),
"pingMs" : NumberLong(0),
"configVersion" : 6
},
{
"_id" : 3,
"name" : "<IP>:<PORT>",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1984305,
"optime" : Timestamp(1474875361, 16),
"optimeDate" : ISODate("2016-09-26T07:36:01Z"),
"lastHeartbeat" : ISODate("2016-09-26T07:36:02.921Z"),
"lastHeartbeatRecv" : ISODate("2016-09-26T07:36:03.793Z"),
"pingMs" : NumberLong(22),
"lastHeartbeatMessage" : "syncing from: 10.9.52.56:10050",
"syncingTo" : "10.9.52.56:10050",
"configVersion" : 6
}
],
"ok" : 1
}
From the above we can know the entire replica set status