MongoDB How to know Primary DB server ip in a replica set?

I am running mongodb replica set on 3 nodes , lets their ip's are 192.168.1.100 , 192.168.1.101,192.168.1.102

now in current replica set 192.168.1.100 is primary and 192.168.1.101 and 192.168.1.102 are secondary , my application connect with 192.168.1.100 for write operations .now after 2 days 192.168.1.100 is down and mongodb select 192.168.1.101 as primary . how my application knows 192.168.1.101 is primary .

is their is any floating ip concept in mongodb so that no manual work needed when primary server switched in a replica set .


Solution 1:

Apparently, you should be able to use your "driver" (the mongo cli tool or your preferred language binding, ex node-mongo) to connect to ANY member of a replica set. Once connected, just ask the current mongod serve for the other members of the set:

> db.runCommand("ismaster")
{
     "ismaster" : false,
     "secondary" : true,
     "hosts" : [
             "ny1.acme.com",
             "ny2.acme.com",
             "sf1.acme.com"
     ],
     "passives" : [
          "ny3.acme.com",
          "sf3.acme.com"
     ],
     "arbiters" : [
         "sf2.acme.com",
     ]
     "primary" : "ny2.acme.com",
     "ok" : true
}

For my use, it's important NOT to connect to the primary. Like the OP, I want to minize the number of connections required to find a secondary member. This method should work for you, but the documentation here might be a little dated.

http://docs.mongodb.org/meta-driver/latest/legacy/connect-driver-to-replica-set/

Solution 2:

rs.isMaster().primary

If you are into one liners, get on any of the mongo instances and run that rascal

Solution 3:

When your client connects to any given member in the replica set ("seed"), it'll query the replica set for the other members in the set. So if you connect to .100, it'll query the set and find that .101 and .102 are valid members of the set, too.

If the driver loses its connection to .100, it'll run through the other seeds that it has discovered and try to find a connection. Once it does, it'll query the replica set, find out who the current master is, and connect to it. This all happens transparently.

You can test this by logging into the master and running the following:

rs.stepDown(60)

This will cause that machine to step down as the master (and cause a new master to be elected). It will not be eligible for re-election for 60 seconds. You can use this to test the behavior of your app in circumstances where the primary node is changed.

When you're setting up a replica set connection, you will generally specify multiple hosts to connect to. These all serve as seeds for the driver to use to map the replica set, so that you don't have a single point of failure in case you restart your application while the single host you have configured is down. The specifics of this will depend on which driver you're using, though - check your driver's documentation.

Solution 4:

To figure out which is the current primary server's IP from command line run:

rs.status().members.find(r=>r.state===1).name

Depending on your application's stack and driver, you might need to specify all hosts in the replica for it to auto-select primary for writes.