Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

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 .

question from:https://stackoverflow.com/questions/16292391/mongodb-how-to-know-primary-db-server-ip-in-a-replica-set

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...