0

I have three instances on EC2, each one with service mongo, set up with replica set, ie, a primary (192.168.1.1), a secondary (192.168.1.2) and an arbiter (192.168.1.3).

I have several applications that connect with mongo.

My question is as follows:

If I add another member in the replica, I have to change all applications in the connection setup to include the new member?

Because I ask it.

Suppose ip with final .1 be primary, .2 is secondary, then add a new member to the end ip .4. And I do not include this new member in the application database settings. And did the primary falls, the arbiter chooses ip with .4 to be the new primary. All applications will begin throw exception.

How can I "fix" it, in a way that always I have to add a new member does not need to be changing the configuration of the application database. Imagine if I have to add 'n' members.

Do you have any way to configure the application, knowing that my application is in PHP, to identify the primary alone without having to add the host configuration?

pedrosalpr
  • 155
  • 1
  • 9
  • It's difficult; unless you are lucky enough that your initial set of hosts remain included in the replica set forever, and there's always at least one of them active and available, then you *will* have to change the application config eventually. The various solutions to this problem are called [tag:service-discovery], and involve your application contacting a 3rd system to find out where it can connect to the replicaset. – Vince Bowdren Oct 20 '16 at 11:11

1 Answers1

0

No need to change anything in application. You just add new node to replica set with command rs.add("address:port"). If you have three nodes (primary and 2x secondary), you should NOT have any arbiter, if all those nodes are voting members. (vote count should be always odd number)

You can "choose" primary heritage order with priority values. If you set your .1 nodes priority f.ex. to 3, .2 nodes priority to 2 and keep last secondary as priority 1, .1 stays primary if it can and when it cannot, .2 is selected as primary... Point is that, it's enough that your application connects to one node of replica set and it (application) get's automatically information, what are other nodes of that replica set and "who" is primary.

With three node replica set, you must have always two nodes up and running (majority; 2/3) or your last node would not be elected as primary.

JJussi
  • 1,540
  • 12
  • 12
  • This will work fine, as long as there are only few changes to the replicaset. If you end up making lots of changes (e.g. adding five new hosts, and then removing the original three hosts) then the application's connection string will no longer be valid because it doesn't include the address of any of the active hosts. – Vince Bowdren Oct 20 '16 at 11:05