How to set the Replicat set in MongoDB

Connect with

MongoDB
Setting of replication on production is very easy with minimum of 3 or 2 ( if 2 then one node need to define as arbiter node which don’t keep data at all ).

Follow below following steps and you are done with replication.

Step 1. Change the replicaSet name in mongod.config file

Step 2. login into mongo shell

## run following to login into mongo client from terminal

mongo

or if running in authentication mode, then run following on terminal.

mongo -u "username" -p "xxxxxxx" --authenticationDatabase "admin"

Step 3. Initiate the Replica set Member and Add the member to replica set


> config = {"_id" : "myrs",
    "members" : [
                  { "_id" : 0, "host":"mongonod1.mysoftkey.com:27017", "priority":5}, 
                  { "_id" : 1, "host":"mongonod2.mysoftkey.com:27017", "priority":2}, 
                 { "_id" : 2, "host":"mongonod3.mysoftkey.com:27017", "priority":1} 
                ]
       }
> rs.initiate(config);
{ "ok" : 1 }

Step : Add secondary node in replica Set and check status

you can provide either domain name /sub-domain name or IP address , while adding into replicaset.

rs1:PRIMARY> rs.add('mongonod2.mysoftkey.com:27017')
{ "ok" : 1 }
rs1:PRIMARY> rs.add('mongonod3.mysoftkey.com:27017')
{ "ok" : 1 }

rs1:PRIMARY> rs.status();
{
        "set" : "rs1",
        "date" : ISODate("2016-04-15T06:20:52Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "mongonod1.mysoftkey.com:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 747,
                        "optime" : Timestamp(1460701252, 1),
                        "optimeDate" : ISODate("2016-04-15T06:20:52Z"),
                        "electionTime" : Timestamp(1460700687, 2),
                        "electionDate" : ISODate("2016-04-15T06:11:27Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "mongonod2.mysoftkey.com:27017",
                        "health" : 1,
                        "state" : 0,
                        "stateStr" : "STARTUP",
                        "uptime" : 470,
                        "optime" : Timestamp(0, 0),
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2016-04-15T06:20:50Z"),
                        "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
                        "pingMs" : 0,
			"syncingTo" : "mongonod1.mysoftkey.com:27017"
                },
                {
                        "_id" : 2,
                        "name" : "mongonod3.mysoftkey.com:27017",
                        "health" : 1,
                        "state" : 0,
                        "stateStr" : "STARTUP",
                        "uptime" : 463,
                        "optime" : Timestamp(0, 0),
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2016-04-15T06:20:51Z"),
                        "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
                        "pingMs" : 0,
			"syncingTo" : "mongonod1.mysoftkey.com:27017"
                }
        ],
        "ok" : 1
}

In the above json , “stateStr” : “STARTUP” means , now data start replicating to both the secondary replicset node. once it get converted to

Reference

  1. MongoDB Replication

Suggestions or your comments are welcome to encourage and improve this post.


Connect with

Leave a Reply

Your email address will not be published. Required fields are marked *