How to add Secondary Member in running Replica Set in MongoDB

Connect with

Adding a Secondary node in running mongoDB replica set is very easy without downtime.

How to Add Secondary Node in Replica set?

prerequisite: all the node must be interconnected without password or put key file location in /etc/mongod.config file.

Step #1: connect MongoDB primary node

Step #2: connect to mongo shell

 mongo

Step #3: add host with priority (optional)

rs1:PRIMARY>rs.add({host:"10.72.179.198:27017", priority: 0})

Step #4: add host with priority (optional)

rs1:PRIMARY> rs.status()
{
        "set" : "rs1",
        "date" : ISODate("2017-03-04T16:51:41.805Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 1,
                        "name" : "MUMPOLAPP18V:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 377398,
                        "optime" : Timestamp(1488646300, 4),
                        "optimeDate" : ISODate("2017-03-04T16:51:40Z"),
                        "lastHeartbeat" : ISODate("2017-03-04T16:51:40.242Z"),
                        "lastHeartbeatRecv" : ISODate("2017-03-04T16:51:40.352Z"),
                        "pingMs" : 0,
                        "syncingTo" : "MUMPOLAPP17V:27017",
                        "configVersion" : 173206
                },
                {
                        "_id" : 2,
                        "name" : "MUMPOLAPP17V:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 878080,
                        "optime" : Timestamp(1488646301, 2),
                        "optimeDate" : ISODate("2017-03-04T16:51:41Z"),
                        "electionTime" : Timestamp(1488268902, 1),
                        "electionDate" : ISODate("2017-02-28T08:01:42Z"),
                        "configVersion" : 173206,
                        "self" : true
                },
                {
                        "_id" : 3,
                        "name" : "10.72.179.198:27017",
                        "health" : 1,
                        "state" : 3,
                        "stateStr" : "RECOVERING",
                        "uptime" : 365,
                        "optime" : Timestamp(1487952971, 7),
                        "optimeDate" : ISODate("2017-02-24T16:16:11Z"),
                        "lastHeartbeat" : ISODate("2017-03-04T16:51:40.243Z"),
                        "lastHeartbeatRecv" : ISODate("2017-03-04T16:51:40.351Z"),
                        "pingMs" : 0,
                        "configVersion" : 173206
                }
        ],
        "ok" : 1
}
rs1:PRIMARY>

Once, file in sync then RECOVERING state turns to SECONDARY.

sync data from another node

command:

rs.syncFrom("10.72.179.204:27017")

console:

rs1:RECOVERING> rs.syncFrom("MUMPOLAPP17V:27017")
{ "syncFromRequested" : "MUMPOLAPP17V:27017", "ok" : 1 }

Connect with

4 thoughts on “How to add Secondary Member in running Replica Set in MongoDB

Leave a Reply

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