Unable to reach primary for set Cluster0-shard-0

mongodbnode.js

I've set up a MongoDB cluster on Atlas and have been able to connect to it from Compass and a NodeJs app I'm building for about a week. today I started getting consistently the "transient" error below from the NodeJs app:

{ MongoNetworkError: connection 5 to
cluster0-shard-00-01-z4j9e.azure.mongodb.net:27017 closed
at TLSSocket. (/Users/ekkis/dev/tst/node_modules/mongodb-core/lib/connection/connection.js:276:9)
at Object.onceWrapper (events.js:277:13)
at TLSSocket.emit (events.js:194:15)
at _handle.close (net.js:610:12)
at TCP.done (_tls_wrap.js:386:7) name: 'MongoNetworkError', errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }

even though Compass seems to work fine. it's very slow sometimes but it works. so I tried installing the shell (v4.0.0 on OSX) and got this:

$ mongo "mongodb+srv://cluster0-z4j9e.azure.mongodb.net/test" --username tst

MongoDB shell version v4.0.0 Enter password: connecting to:
mongodb+srv://cluster0-z4j9e.azure.mongodb.net/test
2019-02-24T16:48:07.460-0800 I NETWORK [js] Starting new replica set
monitor for
Cluster0-shard-0/cluster0-shard-00-00-z4j9e.azure.mongodb.net.:27017,cluster0-shard-00-01-z4j9e.azure.mongodb.net.:27017,cluster0-shard-00-02-z4j9e.azure.mongodb.net.:27017
2019-02-24T16:48:09.971-0800 W NETWORK [js] Unable to reach primary
for set Cluster0-shard-0 2019-02-24T16:48:09.971-0800 I NETWORK [js]
Cannot reach any nodes for set Cluster0-shard-0. Please check network
connectivity and the status of the set. This has happened for 1 checks
in a row. 2019-02-24T16:48:13.260-0800 W NETWORK [js] Unable to reach
primary for set Cluster0-shard-0 2019-02-24T16:48:13.260-0800 I
NETWORK [js] Cannot reach any nodes for set Cluster0-shard-0. Please
check network connectivity and the status of the set. This has
happened for 2 checks in a row. 2019-02-24T16:48:16.880-0800 W NETWORK
[js] Unable to reach primary for set Cluster0-shard-0
2019-02-24T16:48:16.880-0800 I NETWORK [js] Cannot reach any nodes
for set Cluster0-shard-0. Please check network connectivity and the
status of the set. This has happened for 3 checks in a row.
2019-02-24T16:48:22.303-0800 W NETWORK [js] Unable to reach primary
for set Cluster0-shard-0 2019-02-24T16:48:22.303-0800 I NETWORK [js]
Cannot reach any nodes for set Cluster0-shard-0. Please check network
connectivity and the status of the set. This has happened for 4 checks
in a row. 2019-02-24T16:48:22.303-0800 E QUERY [js] Error: connect
failed to replica set
Cluster0-shard-0/cluster0-shard-00-00-z4j9e.azure.mongodb.net.:27017,cluster0-shard-00-01-z4j9e.azure.mongodb.net.:27017,cluster0-shard-00-02-z4j9e.azure.mongodb.net.:27017
: connect@src/mongo/shell/mongo.js:251:13 @(connect):1:6 exception:
connect failed

I can't seem to get it to work. should I recreate the cluster? what else could I do to troubleshoot?

Best Answer

I just had the same behavior trying to connect to a MongoDB Atlas cluster from Linux Mongo client.

The reason I was having this issue was related to version mismatch from client to the cluster. Mongo client was on v4.0.5 while the DB cluster was running on v4.0.6.

Updating Mongo client to version v4.0.6 has fixed the issue for me.

As for the issue with your node.js application, it could be unrelated to the mongo client issue you are seeing or it could be related to versioning just as well, so I would suggest seeing if the database connector you use has an update available.

EDIT: To expand a little on the error you got from your node application, there is this StackOverflow question talking about the exact error code you are seeing.