4

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.

Watt
  • 3,118
  • 14
  • 54
  • 85

2 Answers2

7

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:

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.

Stennie
  • 63,885
  • 14
  • 149
  • 175
1

A way to provide abstraction to your database is to set up a sharded cluster. In that case, the access point between your application and the database are the mongodb routers. What happens behind them is outside of the visibility of the application. You can add shards, remove shards, turn shards into replica-sets and change those replica-sets all you want. The application keeps talking with the routers, and the routers know which servers they need to forward them. You can change the cluster configuration at runtime by connecting to the routers with the mongo shell.

When you have questions about how to set up and administrate MongoDB clusters, please ask on http://dba.stackexchange.com.

But note that in the scenario you described, that wouldn't even be necessary. When one of your database servers has a hardware failure and your system administrators want to replace it without application downtime, they can just assign the same IP and hostname to the new server so the application doesn't even notice that it's a replacement.

When you want to know details about how to do this, you will find help on http://serverfault.com

Philipp
  • 67,764
  • 9
  • 118
  • 153
  • Properly configured replica sets provide for [high availability](http://docs.mongodb.org/manual/core/replica-set-high-availability/) using automatic failover. Sharding is definitely not a requirement for this use case. – Stennie Dec 01 '13 at 07:04