I am working with multiple AWS intances connected to the same MongoDB database (inside Compose.io Elastic deployment). But i am getting the error server <url>:<port> sockets closed after a few minutes. Can anyone give me any hint about what may be wrong with the connection code?


var url = "mongodb://<user>:<password>@<url1>:<port1>,<url2>:<port2>/<dbName>?replicaSet=<replicaSetName>"; 
var options = { 
    server : {"socketOptions.keepAlive": 1}, 
    replSet : { "replicaSet": <replicaSetName>, "socketOptions.keepAlive": 1 } 
MongoClient.connect(url, options, function(err, db) { ... });


Potentially unhandled rejection [2] MongoError: server <url>:<port> sockets closed 
at null. (/var/app/current/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:328:47) 
at g (events.js:199:16) 
at emit (events.js:110:17) 
at null. (/var/app/current/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:101:12) 
at g (events.js:199:16) 
at emit (events.js:110:17) 
at Socket. (/var/app/current/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:142:12) 
at Socket.g (events.js:199:16) 
at Socket.emit (events.js:107:17) 
at TCP.close (net.js:485:12)

First a probably silly question, are you replacing all the , , etc placeholders with the actual values from your compose.io database?

If you are already doing that, maybe you can debug the connection using the mongo shell to connect to your DB instances.

If you have local install of mongodb you can use:

mongo host:port/database-name -u user -ppassword

This is also what compose.io docs recommend for connection debugging https://docs.compose.io/common-questions/how-to-connect-to-mongodb.html


It could be also that max connections is reached (if you configured that)

2015-12-08T08:49:10.964+0000 I NETWORK [initandlisten] connection accepted from #184 (26 connections now open)
2015-12-08T08:49:10.964+0000 I NETWORK [initandlisten] connection refused because too many open connections: 25

As per MongoDB BOL Here if you shall connect to a ReplicaSet consisting of one primary and 1 or more secondaries. To Do this we need to supply the driver with a seedlist of servers and the name of the ReplicaSet we wish to connect to. Let’s take a look at a code example.

var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017,localhost:27018/myproject?replicaSet=foo';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected correctly to server");


For further your ref Here & Connection String URI Format


I have no experience with MongoDB specifically, but I have noticed that AWS will sever TCP connections (each end thinks the other end closed it) after 5 minutes of idleness, and that the keepalive setting does not prevent this.

I've noticed this both with PostgreSQL streaming replication, 5 minutes like clockwork, as evidenced by the log entries that the connection was lost and reestablished. Also with ssh, but I haven't timed it there. Go make a cup of tea, come back and your connection is gone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.