How to add new server in replica set in production

database-replication, mongodb

Solution

The list of replica set hosts in your connection string is a "seed list", and does not have to include all of the members of your replica set.

The MongoDB client driver used by your application will iterate through the seed list until it can successfully connect to a host, and use that host to request the current replica set configuration which will list all current members of the replica set. Per the documentation, it is recommended to include at least two hosts in the connect string so that your driver can still connect in the event the first host happens to be down.

Any changes in replica set configuration (i.e. adding/removing members or election of a new primary) are automatically discovered by your client driver so you should not have to make any changes in the application configuration to add a new member to your replica set.

A change in replica set configuration may trigger an election for a new primary, so your application code should expect to handle transient errors for a few seconds during reconfiguration.

Some helpful `mongo` shell commands:

- `rs.conf()` - display the current replication configuration

- `db.isMaster().primary` - display the current primary

You should notice a version number in the configuration document returned by `rs.conf()`. This version is incremented on every configuration change so drivers and replica set nodes can check if they have a stale version of the config.

How my application will know about the new db4.example.net

Just `rs.add("db4.example.net")` and your application should discover this host automatically.

In your scenario, if you are replacing an entirely dead host you would likely also want to `rs.remove()` the original host (after adding the replacement) to maintain the voting majority for your replica set.

Alternatively, rather than adding a host with a new name you could replace the dead host with a new server using the same hostname as previously configured. For example, if `db3.example.net` died, you could replace it with a new `db3.example.net` and follow the steps to Resync a replica set member.

Problem

I am new to mongodb replica set. According to Replic Set Ref this should be connection string in my application to connect to mongodb ``` mongodb://db1.example.net,db2.example.net,db3.example.net:2500/?replicaSet=test ``` Suppose this is production replica set (i.e. I cannot change application code or stop all the mongo servers) And, I want to add another mongo db instance `db4.example.net` in `test` replica set. How will I do that? How my application will know about the new `db4.example.net` If you are looking for real-world scenario: In situation when any of existing server is down due to hardware failure etc, it is natural to add another db server to the replica set to preserve the redundancy. But, how to do that.

Original source