MongoError: no connection available to server

Hi,

I found very strange issue while reading the document from a collection .
From MongoDB Atlas dashboard I can see number of connections are available , When I tried db.collection(collectionname).countDocuments API then it responds error

MongoError: no connection available to server myclustername--shard-00-02.lp8yd.mongodb.net:27017.

The detailed log is provided below:

This is first time I observed this issue and after this it did not reproduce .

Did anyone face this kind of issue that lot of connections are available and still atlas sends “no connection available error” .

I am using MongoDB Nodejs Driver and detailed logs are here.

at disconnectHandler (/var/task/node_modules/mongodb/lib/core/topologies/server.js:271:14)
    at Server.command (/var/task/node_modules/mongodb/lib/core/topologies/server.js:625:7)
    at CountDocumentsOperation.executeCommand (/var/task/node_modules/mongodb/lib/operations/command_v2.js:95:12)
    at CountDocumentsOperation.execute (/var/task/node_modules/mongodb/lib/operations/aggregate.js:93:11)
    at CountDocumentsOperation.execute (/var/task/node_modules/mongodb/lib/operations/count_documents.js:22:11)
    at /var/task/node_modules/mongodb/lib/operations/execute_operation.js:144:17
    at _selectServer (/var/task/node_modules/mongodb/lib/core/topologies/replset.js:1160:5)
    at ReplSet.selectServer (/var/task/node_modules/mongodb/lib/core/topologies/replset.js:1163:3)
    at ReplSet.selectServer (/var/task/node_modules/mongodb/lib/topologies/topology_base.js:342:32)
    at executeWithServerSelection (/var/task/node_modules/mongodb/lib/operations/execute_operation.js:131:12)

Hi Ajay,

This is very odd indeed: out of curiosity did you create a support case? You were connected to the full replica set rather than an individual node in the cluster right?
-Andrew

Hi Andrew,
I am using Atlas mongodb and I believe by default atlas creates replica set.
For connecting the DB from my application I am using connect API with these below options.
It should connect with full replica set, or is it connecting with any one cluster node ?
MongoClient.connect(“mongodb+srv://UserName:@myclustername.lp8yd.mongodb.net/myFirstDatabase?retryWrites=true&w=majority”, {useNewUrlParser: true, useUnifiedTopology: true},

of course I changed usename , pwd and clustername with the real values.
Regards
Ajay

Hi Ajay,

By default that modern SRV connection string will get you access to the full replica set: SRV allows for each node’s public DNS hostname to be discovered inside the SRV record itself automatically (this is supported by newer generation MongoDB drivers). This makes for a more concise connection string.

Cheers
-Andrew

Hi Andrew,
Thanks for your reply. It is confirmed that I was connected with the full replica set.
Now the issue is when there are plenty of connection counts are available then why mongodb atlas replied "no connection available to server " ?
This is very strange , Please help to know why it misbehaved and how this can be handled.

Regards
Ajay

Hi Ajay, Please open a support case or use the lower-right chat in the UI: that team will be better positioned to help you with troubleshooting.

Cheers
-Andrew

Hi Andrew,
First I contacted the support person by using lower-right chat in the UI.
The support person first says that “It is due to election between nodes”, then I replied him that while going election it does not impact the read operations.

After this he says, yes it should not impact the read operations and finally he said " I am afraid, we are not able to provide a root cause for this matter from within this chat. You may wish to check out our community support resources, where our other MongoDB users like yourself are frequently answering questions "

After reading his answer I was disappointed and I created this post here and hoping that I will get the right solution for this.

Regards
Ajay

Is it possible you’re using an earlier version of the MongoDB driver? later versions use retryable reads by default https://docs.mongodb.com/manual/core/retryable-reads/

Hi Andrew,
Thanks for your reply.
There was only one single read request sent from my application , so I am not sure any use case of multiple reads here.
May be Mongo atlas have any mechanism for multiple reads for single read request from the replica set or any network issue at mongo end due to that it required retry able reads .

To eliminate any possibilities I checked the driver version using with the application and it is Node.js 3.1.13 driver that is compatible with MongoDB 4.0 .
Should is update my driver, if yes then 3.6.5 will be a good choice ?

The API interfaces are common between 3.1.13 and 3.6.5 or is there any interface change those need to be updated at the application level ?

Regards
Ajay

Hi @Ajay_P,

Wondering whether you have had any success in connecting to your Atlas cluster ?

Should is update my driver, if yes then 3.6.5 will be a good choice ?

It would be ideal if you could update your MongoDB Node.JS driver to the current stable version (v3.6.6). Below is a simple example snippet:

var run = async function() {
  var conn = await MongoClient.connect('mongodb+srv://user:pwd@cluster-1234.mongodb.net/test?retryWrites=true', { useNewUrlParser: true, useUnifiedTopology: true })
  console.log(await conn.db('test').collection('test').countDocuments({}));
}().catch(function(err) {
  console.log(err)
})

If you are still encountering the same issue, could you please provide a minimal reproducible code example.

Regards,
Wan.

1 Like

Hi Won,

As I described earlier, this issue is not preproducing, but i have logs that proves that there is an issue and it may occur in future as well.
To eliminate all the possibilities I have updated to the the app to Node.js driver version v3.6.6.
I am hoping that this issue will not occur with v3.6.6 driver.
The string i am using for this connection is
MongoClient.connect(‘mongodb+srv://user:pwd@cluster-1234.mongodb.net/test?retryWrites=true&w=majority’, { useNewUrlParser: true, useUnifiedTopology: true })

The only difference between your suggestion and my implantation is w=majority .

Regards
Ajay

Hi @Ajay_P , @Andrew_Davidson . We have ran into the same issue. From past 3 days, our production servers are automatically loosing connection to the MongoDB server at a fixed time (around 8pm, IST). For 5 mins, there are only GET calls happening but no PUT or POST. Even the clusters show they are online (in Atlas).

It’s becoming scary for us, as the clients are panicking. I"m attaching my logs as well for some to look at.

To give a context, the MongoDB instance is been running since 9+months now and no DB related configurations have been changed. This is happening out of now where.

name: 'MongoNetworkError',
0|myapp-p |   errorLabels: [ 'TransientTransactionError' ],
0|myapp-p |   status: 400,
0|myapp-p |   [Symbol(mongoErrorContextSymbol)]: { isGetMore: true }
0|myapp-p | }
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/app.js:76:17
0|myapp-p |     at Layer.handle_error (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/layer.js:71:5)
0|myapp-p |     at trim_prefix (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:315:13)
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:284:7
0|myapp-p |     at Function.process_params (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:335:12)
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:275:10)
0|myapp-p |     at Layer.handle_error (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/layer.js:67:12)
0|myapp-p |     at trim_prefix (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:315:13)
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:284:7
0|myapp-p |     at Function.process_params (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:335:12)
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:275:10)
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:635:15
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:260:14)
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/route.js:127:14)
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/app/contollers/subOrder.controller.js:357:20
0|myapp-p |     at bound (domain.js:419:14)
0|myapp-p |     at runBound (domain.js:432:12)
0|myapp-p |     at tryCatcher (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/util.js:16:23)
0|myapp-p |     at Promise._settlePromiseFromHandler (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/promise.js:517:31)
0|myapp-p |     at Promise._settlePromise (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/promise.js:574:18)
0|myapp-p |     at Promise._settlePromise0 (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/promise.js:619:10)
0|myapp-p |     at Promise._settlePromises (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/promise.js:695:18)
0|myapp-p |     at _drainQueueStep (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/async.js:138:12)
0|myapp-p |     at _drainQueue (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/async.js:131:9)
0|myapp-p |     at Async._drainQueues (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/async.js:147:5)
0|myapp-p |     at Immediate.Async.drainQueues [as _onImmediate] (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/async.js:17:14)
0|myapp-p |     at processImmediate (internal/timers.js:439:21)
0|myapp-p |     at process.topLevelDomainCallback (domain.js:130:23) 
0|myapp-p | info: GET /api/admin/sub-orders?status=OPEN,WAITING,CANCELED,ACCEPTED,COOKED,SERVED,COOKING 400 229900ms 
0|myapp-p | error: Trace: MongoNetworkError: connection 10 to cluster0-shard-00-01-anxgh.mongodb.net:27017 closed
0|myapp-p |     at TLSSocket.<anonymous> (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:352:9)
0|myapp-p |     at Object.onceWrapper (events.js:313:26)
0|myapp-p |     at TLSSocket.emit (events.js:223:5)
0|myapp-p |     at TLSSocket.EventEmitter.emit (domain.js:475:20)
0|myapp-p |     at net.js:664:12
0|myapp-p |     at TCP.done (_tls_wrap.js:481:7) {
0|myapp-p |   name: 'MongoNetworkError',
0|myapp-p |   errorLabels: [ 'TransientTransactionError' ],
0|myapp-p |   status: 400,
0|myapp-p |   [Symbol(mongoErrorContextSymbol)]: { isGetMore: true }
0|myapp-p | }
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/app.js:76:17
0|myapp-p |     at Layer.handle_error (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/layer.js:71:5)
0|myapp-p |     at trim_prefix (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:315:13)
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:284:7
0|myapp-p |     at Function.process_params (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:335:12)
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:275:10)
0|myapp-p |     at Layer.handle_error (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/layer.js:67:12)
0|myapp-p |     at trim_prefix (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:315:13)
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:284:7
0|myapp-p |     at Function.process_params (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:335:12)
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:275:10)
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:635:15
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:260:14)
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/route.js:127:14)
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/app/contollers/subOrder.controller.js:357:20
0|myapp-p |     at bound (domain.js:419:14)
0|myapp-p |     at runBound (domain.js:432:12)
0|myapp-p |     at tryCatcher (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/util.js:16:23)
0|myapp-p |     at Promise._settlePromiseFromHandler (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/promise.js:517:31)
0|myapp-p |     at Promise._settlePromise (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/promise.js:574:18)
0|myapp-p |     at Promise._settlePromise0 (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/promise.js:619:10)
0|myapp-p |     at Promise._settlePromises (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/promise.js:695:18)
0|myapp-p |     at _drainQueueStep (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/async.js:138:12)
0|myapp-p |     at _drainQueue (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/async.js:131:9)
0|myapp-p |     at Async._drainQueues (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/async.js:147:5)
0|myapp-p |     at Immediate.Async.drainQueues [as _onImmediate] (/home/ubuntu/apps/myapp-servers/node_modules/bluebird/js/release/async.js:17:14)
0|myapp-p |     at processImmediate (internal/timers.js:439:21)
0|myapp-p |     at process.topLevelDomainCallback (domain.js:130:23) 
0|myapp-p | info: GET /api/admin/sub-orders?status=OPEN,WAITING,CANCELED,ACCEPTED,COOKED,SERVED,COOKING 400 221108ms 
0|myapp-p | error: Trace: MongoNetworkError: connection 10 to cluster0-shard-00-01-anxgh.mongodb.net:27017 closed
0|myapp-p |     at TLSSocket.<anonymous> (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:352:9)
0|myapp-p |     at Object.onceWrapper (events.js:313:26)
0|myapp-p |     at TLSSocket.emit (events.js:223:5)
0|myapp-p |     at TLSSocket.EventEmitter.emit (domain.js:475:20)
0|myapp-p |     at net.js:664:12
0|myapp-p |     at TCP.done (_tls_wrap.js:481:7) {
0|myapp-p |   name: 'MongoNetworkError',
0|myapp-p |   errorLabels: [ 'TransientTransactionError' ],
0|myapp-p |   status: 400,
0|myapp-p |   [Symbol(mongoErrorContextSymbol)]: { isGetMore: true }
0|myapp-p | }
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/app.js:76:17
0|myapp-p |     at Layer.handle_error (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/layer.js:71:5)
0|myapp-p |     at trim_prefix (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:315:13)
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:284:7
0|myapp-p |     at Function.process_params (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:335:12)
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:275:10)
0|myapp-p |     at Layer.handle_error (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/layer.js:67:12)
0|myapp-p |     at trim_prefix (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:315:13)
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:284:7
0|myapp-p |     at Function.process_params (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:335:12)
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:275:10)
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:635:15
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:260:14)
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/route.js:127:14)
0|myapp-p |     at exports.createByAdmin (/home/ubuntu/apps/myapp-servers/app/contollers/subOrder.controller.js:121:9)
0|myapp-p |     at runMicrotasks (<anonymous>)
0|myapp-p |     at processTicksAndRejections (internal/process/task_queues.js:94:5) 
0|myapp-p | info: POST /api/admin/sub-order-admin 400 269099ms 
0|myapp-p | error: Trace: MongoError: no connection available to server cluster0-shard-00-01-anxgh.mongodb.net:27017
0|myapp-p |     at disconnectHandler (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:264:14)
0|myapp-p |     at Server.insert (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:653:7)
0|myapp-p |     at executeWriteOperation (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/replset.js:1183:37)
0|myapp-p |     at Object.handler [as cb] (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/replset.js:1162:14)
0|myapp-p |     at connectionFailureHandler (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:240:33)
0|myapp-p |     at Connection.Pool._connectionCloseHandler (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:152:5)
0|myapp-p |     at Connection.emit (events.js:223:5)
0|myapp-p |     at Connection.EventEmitter.emit (domain.js:475:20)
0|myapp-p |     at TLSSocket.<anonymous> (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:350:12)
0|myapp-p |     at Object.onceWrapper (events.js:313:26)
0|myapp-p |     at TLSSocket.emit (events.js:223:5)
0|myapp-p |     at TLSSocket.EventEmitter.emit (domain.js:475:20)
0|myapp-p |     at net.js:664:12
0|myapp-p |     at TCP.done (_tls_wrap.js:481:7) {
0|myapp-p |   name: 'MongoError',
0|myapp-p |   status: 400,
0|myapp-p |   [Symbol(mongoErrorContextSymbol)]: {}
0|myapp-p | }
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/app.js:76:17
0|myapp-p |     at Layer.handle_error (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/layer.js:71:5)
0|myapp-p |     at trim_prefix (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:315:13)
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:284:7
0|myapp-p |     at Function.process_params (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:335:12)
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:275:10)
0|myapp-p |     at Layer.handle_error (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/layer.js:67:12)
0|myapp-p |     at trim_prefix (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:315:13)
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:284:7
0|myapp-p |     at Function.process_params (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:335:12)
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:275:10)
0|myapp-p |     at /home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:635:15
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/index.js:260:14)
0|myapp-p |     at next (/home/ubuntu/apps/myapp-servers/node_modules/express/lib/router/route.js:127:14)
0|myapp-p |     at exports.customerMe (/home/ubuntu/apps/myapp-servers/app/contollers/user.controller.js:601:9)
0|myapp-p |     at runMicrotasks (<anonymous>)
0|myapp-p |     at processTicksAndRejections (internal/process/task_queues.js:94:5) 
0|myapp-p | info: PUT /api/customer/me 400 228086ms 
0|myapp-p | error: Trace: MongoError: no connection available to server cluster0-shard-00-01-anxgh.mongodb.net:27017
0|myapp-p |     at disconnectHandler (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:264:14)
0|myapp-p |     at Server.insert (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:653:7)
0|myapp-p |     at executeWriteOperation (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/replset.js:1183:37)
0|myapp-p |     at Object.handler [as cb] (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/replset.js:1162:14)
0|myapp-p |     at connectionFailureHandler (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:240:33)
0|myapp-p |     at Connection.Pool._connectionCloseHandler (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:152:5)
0|myapp-p |     at Connection.emit (events.js:223:5)
0|myapp-p |     at Connection.EventEmitter.emit (domain.js:475:20)
0|myapp-p |     at TLSSocket.<anonymous> (/home/ubuntu/apps/myapp-servers/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:350:12)
0|myapp-p |     at Object.onceWrapper (events.js:313:26)
0|myapp-p |     at TLSSocket.emit (events.js:223:5)
0|myapp-p |     at TLSSocket.EventEmitter.emit (domain.js:475:20)
0|myapp-p |     at net.js:664:12
0|myapp-p |     at TCP.done (_tls_wrap.js:481:7) {
0|myapp-p |   name: 'MongoError',
0|myapp-p |   status: 400,
0|myapp-p |   [Symbol(mongoErrorContextSymbol)]: {}
0|myapp-p | }