My mongod runs out of connections from time to time. But when I restart the instance, it becomes normal.

I check the log and found some connections were created and never be ended. This cause the connection depleted quickly.

My mongo cluster consists of of 3 shards, each with 3 replicasets. So there are 9 mongod instance and 5 mongos instance. The problem occurs in one of the mongod instance.

I am running mongo version 2.4.

Here is part of the log:

Tue Sep  1 23:24:09.245 [initandlisten] connection accepted from #987213 (291 connections now open)
Tue Sep  1 23:24:09.687 [conn987214] end connection (339 connections now open)
  • Erm, on my Laptop, the number of possible connections is about 8000. How do you come to the conclusion that you run out of connections? Sep 7, 2015 at 10:05
  • @MarkusWMahlberg I find the connection grow to thousands. And the program complains about socket error
    – Sisyphus
    Sep 7, 2015 at 10:14

2 Answers 2


Find out how many connections are there to the database vs how many available. This command in Mongo Shell will tell you exactly that:


  • NO need to mention 'connections' in db.serverStatus().connections. Even from 'db.serverStatus()' you shall get all details . Nov 20, 2017 at 13:43
  • Even from db.runCommand( { serverStatus: 1 } ) command get all details of server. Nov 20, 2017 at 13:46

@Sisyphus, I have gone through you log , as i would like to say that first you know that

Connection pools

Creating new authenticated connections to the database is expensive. So, instead of creating and destroying connections for each request to the database, you want to re-use existing connections as much as possible. This is where connection pooling comes in.

A Connection Pool is a cache of database connections maintained by your driver so that connections can be re-used when new connections to the database are required. When properly used, connection pools allow you to minimize the frequency and number of new connections to your database.

Opening too many connections

Alternately, although less common, is the problem of creating too many MongoClient objects that are never closed. In this case, instead of churn, you get a steady increase in the number of connections to your database such that you have tens of thousands of connections open when your application could almost certainly do with far fewer. Since each connection takes RAM, you may find yourself wasting a good portion of your memory on connections which will also adversely affect your application's performance.

Although every application is different and the total number of connections to your database will greatly depend on how many client processes or application servers are connected, in our experience, any connection count greater than 1000 - 1500 connections should raise an eyebrow, and most of the time your application will require far fewer than that.

MongoClient and connection pooling

Most MongoDB language drivers implement the MongoClient class which, if used properly, will handle connection pooling for you automatically.

The syntax differs per language, but often you do something like this to create a new connection-pool-enabled client to your database:

mongoClient = new MongoClient(URI, connectionOptions);

Here the mongoClient object holds your connection pool, and will give your app connections as needed. You should strive to create this object once as your application initializes and re-use this object throughout your application to talk to your database. The most common connection pooling problem we see results from applications that create a MongoClient object way too often, sometimes on each database request. If you do this you will not be using your connection pool as each MongoClient object maintains a separate pool that is not being reused by your application.

For further your ref Here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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