How to manage max connections in MongoDB ?

Would like to know – how to manage (max) connections in MongoDB ?

1) How to know how many configured ?
2) How to change ?
3) How to know how many connections are being used ?

 

Answer :

There are number of ways to find connection information. You can use db.serverStatus() to find connection stats such as number of current connections, available connections, total created.

For more in depth analysis, you can make use of following query that will return IP address (or hostname) and the ephemeral port of the client connection that is running an active or idle operation. It is making use of db.currentOp() that returns the in-progress operations for the database instance.

db.currentOp(true).inprog.forEach(function(d){if (d.client)printjson(d.client)})

To monitor the stats such as pool size, wait queue size, number of connections etc, you can use Java Driver monitoring that makes use of ConnectionPoolStatisticsMBean (which is an instance of MXBean). Note that monitoring is available from Java driver v3.0.

Note that you can control the maximum number of allowed connections by passing maxConns parameter during server startup. Or, if you are using configuration file, you can make use of maxIncomingConnections. However, this setting will have no effect if it is higher than your operating system’s configured maximum file descriptor and user process limit.

[root@dbversity bin]# lsof | wc -l
14387
[root@dbversity bin]#
[root@dbversity bin]# lsof | grep mongo | wc -l
651
[root@dbversity bin]#

[root@dbversity bin]# ./mongo –port 10000
MongoDB shell version: 2.4.11
connecting to: 127.0.0.1:10000/test
mongos>
mongos> db.serverStatus().connections

{ “current” : 1, “available” : 19999, “totalCreated” : NumberLong(11) }

mongos>
}
mongos>
bye
[root@dbversity bin]# cat /proc/sys/fs/file-max
6552837
[root@dbversity bin]#

[root@dbversity bin]# cat /etc/sysctl.conf | grep file-max
fs.file-max = 6815744
fs.file-max=6552837
[root@dbversity bin]#

The above file descriptors on the system should honour the UNIX limits configuration: https://docs.mongodb.org/manual/reference/ulimit/

Also refer to MongoDB ulimit post at : http://dbversity.com/mongodb-linux-ulimit-settings/

 

  • Ask Question