1

With node.js mongodb ver. 2.2.25, I use the following connection string to connect to a replica set with primary node down but failed:

mongodb://user:pass@host1:port1,host2:port2/dbname

However, I can use the same connection string with a PHP mongo client and successfully read data. So is there something wrong with the node.js driver? I also tried adding

?replicaSet=rs1&readPreference=primaryPreferred

to the connection string but also failed.

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
konghou
  • 557
  • 7
  • 20
  • When you see the replica set has "primary down", exactly what do you mean: that the usual primary is down and another node has taken over the role? Or that the replica set is in the short-lived transitioning process after the primary has gone down? – Vince Bowdren Apr 18 '17 at 08:36
  • In order for a replica set to elect a primary, you need at least three members. You will need to have all the members on your connection string. You will be able to read data from a secondary member, however not write to it. – joaofs Apr 19 '17 at 23:09
  • Hey konghou, you will need at least 3 servers in replica set to elect other member as primary in case of primary failure. Connection string will be something like `mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?replicaSet=[replicaName]]`. More can be find here http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect. – HDB Apr 21 '17 at 11:50
  • 1
    Well the manual [here](http://php.net/manual/en/mongo.connecting.rs.php) for PHP mongo client says that it too will throw an error if primary is not found. So I think the behavior is consistent with the Node counterpart. – tbking Apr 21 '17 at 19:28

0 Answers0