Mongodb keeps reporting “no primary found”, but shell works fine

mongodbreplica-set

I have a simple mongodb setup in a replicaset, with 1 actual instance and 1 arbiter (to avoid conflicts). This is in anticipation of expanding the replicaset to include more instances as our load increases.

If I access mongodb through the shell, I correctly get the PRIMARY > prompt, and running rs.status() tells me everything is fine and dandy.

I have a node.js server that is accessing the database, using the mongodb-native driver found on github, version 0.9.9-8. However, occasionally it simply breaks down with the following error: Unable to connect to database: Error: no primary server found

The program is given the two adresses for the two instances (normal instance + arbiter), but otherwise neither the program log or mongodb's log gives any clue as to what is wrong. Any help is welcome!

Best Answer

OK, so first, don't give the driver the address of the arbiter, just the primary - there is no reason for the driver to ever talk to the arbiter, it has no data. With just the primary to connect to it will then connect to the primary only and your issue should disappear.

Your set up is another matter - having an arbiter with a single node buys you nothing and actually makes your set less reliable rather than more stable.

Scenario 1: the arbiter goes down/offline

  • The primary is the only node left up and voted for itself
  • 1/2 votes is not a majority, so the primary cannot be elected and becomes a secondary
  • Your set is now down and can't accept writes

Scenario 2: the primary goes down/offline

  • There is no node with data, the arbiter cannot vote for itself
  • 0/2 votes, no actual data nodes available, your set is offline

As you can see, you would actually be better off with just a single primary node - because all the arbiter does is introduce a way for your set to be unavailable when the primary is working just fine. The other option, of course, would be to add a secondary and go to 3 nodes - then you actually get the redundancy benefits of a replica set.

Related Topic