Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the current number of connections to MongoDb

Tags:

mongodb

What is the command to get the number of clients connected to a particular MongoDB server?

like image 789
DafaDil Avatar asked Jan 23 '12 17:01

DafaDil


2 Answers

connect to the admin database and run db.serverStatus():

> var status = db.serverStatus() > status.connections    {"current" : 21, "available" : 15979} >  

You can directly get by querying

db.serverStatus().connections 

To understand what does MongoDb's db.serverStatus().connections response mean, read the documentation here.

connections

"connections" : {    "current" : <num>,    "available" : <num>,    "totalCreated" : NumberLong(<num>) }, 

connections A document that reports on the status of the connections. Use these values to assess the current load and capacity requirements of the server.

connections.current The number of incoming connections from clients to the database server . This number includes the current shell session. Consider the value of connections.available to add more context to this datum.

The value will include all incoming connections including any shell connections or connections from other servers, such as replica set members or mongos instances.

connections.available The number of unused incoming connections available. Consider this value in combination with the value of connections.current to understand the connection load on the database, and the UNIX ulimit Settings document for more information about system thresholds on available connections.

connections.totalCreated Count of all incoming connections created to the server. This number includes connections that have since closed.

like image 142
milan Avatar answered Oct 15 '22 19:10

milan


Connection Count by ClientIP, with Total

We use this to view the number of connections by IPAddress with a total connection count. This was really helpful in debugging an issue... just get there before hit max connections!

For Mongo Shell:

db.currentOp(true).inprog.reduce((accumulator, connection) => { ipaddress = connection.client ? connection.client.split(":")[0] : "Internal"; accumulator[ipaddress] = (accumulator[ipaddress] || 0) + 1; accumulator["TOTAL_CONNECTION_COUNT"]++; return accumulator; }, { TOTAL_CONNECTION_COUNT: 0 }) 

Formatted:

db.currentOp(true).inprog.reduce(   (accumulator, connection) => {     ipaddress = connection.client ? connection.client.split(":")[0] : "Internal";     accumulator[ipaddress] = (accumulator[ipaddress] || 0) + 1;     accumulator["TOTAL_CONNECTION_COUNT"]++;     return accumulator;   },   { TOTAL_CONNECTION_COUNT: 0 } ) 

Example return:

{     "TOTAL_CONNECTION_COUNT" : 331,     "192.168.253.72" : 8,     "192.168.254.42" : 17,     "127.0.0.1" : 3,     "192.168.248.66" : 2,     "11.178.12.244" : 2,     "Internal" : 41,     "3.100.12.33" : 86,     "11.148.23.34" : 168,     "81.127.34.11" : 1,     "84.147.25.17" : 3 } 

(the 192.x.x.x addresses at Atlas internal monitoring)

"Internal" are internal processes that don't have an external client. You can view a list of these with this:

db.currentOp(true).inprog.filter(connection => !connection.client).map(connection => connection.desc); 
like image 39
SuperGoTeam Avatar answered Oct 15 '22 19:10

SuperGoTeam