Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mongodb

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 .

like image 851
Rajnish Avatar asked Apr 30 '13 04:04

Rajnish


People also ask

How do I find the IP address of my MongoDB server?

To find the public IP address for any node in your cluster, use the nslookup tool from the command line. The IP address is shown in the Address portion of the output.

How does replica set connect to MongoDB?

To connect to a replica set deployment, specify the hostname and port numbers of each instance, separated by commas, and the replica set name as the value of the replicaSet parameter in the connection string. In the following example, the hostnames are host1 , host2 , and host3 , and the port numbers are all 27017 .

Which can be used to check the replica set configuration in MongoDB?

You can access the configuration of a replica set using the rs. conf() method or the replSetGetConfig command.

How do I find the replicas name in MongoDB?

On MongoDB Atlas project page, select the Deployment link on the left hand side menu. Under Processes tab, with Topology view you should see the replica set name (this should be the default view presented when you click on Deployment ). Generally, the replica set name is the cluster name plus -shard-0 .


1 Answers

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/

like image 57
Rustavore Avatar answered Oct 02 '22 11:10

Rustavore