Our topology is (1 MongoDB server at each location)
- Primary Datacenter (main server, priority 10 )
- DR datacenter (active standby, priority 5)
- On-Prem datacenter ( When shiz hits the fan, priority .5)
We have the three servers set up in a replica set. They talk to each other through an MPLS connection. Before we go live with our new product that uses this, we're testing several failure scenarios. One scenario is our MPLS connection fails and the primary server cannot contact the other two servers. It quickly gets demoted to a SECONDARY, but then our application (Node) dies because it can't read from it. I've tried adding the connectWithNoPrimary
option set to true, but still no luck. I get the error
Unable to connect to the mongoDB server. Error: { MongoError: no primary found in replicaset ... }
Here's the code block I'm using to test it:
var mongodb = require('mongodb'); var MongoClient = mongodb.MongoClient; var url = "mongodb://webapp:pass@mongodb01.domain.com,mongodb02.domain.com,mongodb03.domain.com/authentication?authSource=authentication&authMechanism=SCRAM-SHA-1&replicaSet=repset&readPreference=primaryPreferred&connectWithNoPrimary=true"; MongoClient.connect(url, function (err, db) { if (err) { console.log('Unable to connect to the mongoDB server. Error:', err); } else { console.log('Connection established to', url); db.close(); } });
To simulate the failure, I simply shut two of the three servers off. I can connect to the third from the mongo shell and confirm it's in SECONDARY mode.
Our end goal is to at least have our replicaset readble in the instance that the primary server gets network isolated from the other two. Can anyone help me understand why my config isn't working and what I need to do to accomplish our goal?
UPDATE
I've since read that connectWithNoPrimary
isn't a URI option, but rather an OPTION of MongoClient.connect(). So my code block changed to have this:
MongoClient.connect(url, { connectWithNoPrimary: true }, function (err, db) { ....
And now it just hangs when it tries to connect. If I boot one of the other servers back up, it'll successfully connect once a Primary is elected.