How to add Secondary Member in running Replica Set in MongoDB

Connect with

How to add member in replica set in MongoDB using rs.add()How to add member in replica set in MongoDB using rs.add() in running mongoDB replica set. How to add secondary member in running replica set in MongoDB.

1. Pre-requisit to add member in Replica set in MongoDB

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

2. Add Secondary Node in Replica set in MongoDB

Step #1: connect MongoDB primary node

There are different way of connecting MongoDB system where mongod installed. Either you can connect via MongoDB client or connect directly on server via ssh.

Step #2: connect to mongo shell

Once you connect MongoDB server you can use the following command to connect mongo shell by using the following command.

 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 to sync from (optional)

Following command is optional but if you required to sync from a specific node then you can run this from the node where you want sync.

> rs.syncFrom("10.72.179.204:27017")

console out put of sync commnad

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

Reference

You can visit docs.mongodb.com for more details.

Thanks for visiting this post for how to add secondary member in replica set in MongoDB. You can also visit MongoDB Tutorial Listing page for more articles on MongoDB document-oriented database.
You reached this point, I hope you enjoyed learning, how to add a node in MongoDB? Happy Learning!


Connect with

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

Leave a Reply to Adeagosob Cancel Reply

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