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

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/

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)