Tuesday, February 18, 2020

How to see current connections to mongodb

Use below 2 commands to total count of connections:


testRepl:PRIMARY> var status=db.serverStatus()
testRepl:PRIMARY> status.connections
{ "current" : 9, "available" : 51191, "totalCreated" : 29 }

OR


testRepl:PRIMARY> db.serverStatus().connections
{ "current" : 9, "available" : 51191, "totalCreated" : 29 }
testRepl:PRIMARY> 


 To get connection per client you can 


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 }
)
OUTPUT:
testRepl:PRIMARY> 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 }
... )
{
        "TOTAL_CONNECTION_COUNT" : 59,
        "172.31.30.18" : 5,
        "172.31.16.25" : 3,
        "172.31.31.40" : 1,
        "Internal" : 50
}
testRepl:PRIMARY> 
You can see internal as 50. These are all mongodb internal connections, If you wish to
see those, use below commands.
db.currentOp(true).inprog.filter(connection => !connection.client).map(connection => connection.desc);
OUTPUT:testRepl:PRIMARY> db.currentOp(true).inprog.filter(connection => !connection.client).map(connection => connection.desc);[ "NoopWriter", "replication-3", "replexec-15", "replexec-14", "repl writer worker 14", "repl writer worker 13", "repl writer worker 10", "repl writer worker 9", "repl writer worker 7", "repl writer worker 6", "repl writer worker 5", "WT RecordStoreThread: local.oplog.rs", "repl writer worker 5", "WTOplogJournalThread", "repl writer worker 12", "SessionKiller", "repl writer worker 8", "ReplBatcher", "repl writer worker 4", "WTCheckpointThread", "monitoring keys for HMAC", "WTIdleSessionSweeper", "TTLMonitor", "repl writer worker 0", "WTJournalFlusher", "clientcursormon", "rsBackgroundSync", "repl writer worker 4", "rsSync", "ftdc", "repl writer worker 2", "repl writer worker 6", "repl writer worker 10", "repl writer worker 11", "repl writer worker 7", "SyncSourceFeedback", "initandlisten", "LogicalSessionCacheRefresh", "repl writer worker 3", "repl writer worker 3", "repl writer worker 11", "repl writer worker 12", "repl writer worker 8", "repl writer worker 1", "LogicalSessionCacheReap", "repl writer worker 15", "repl writer worker 9", "ApplyBatchFinalizerForJournal", "repl writer worker 0", "repl writer worker 2"]testRepl:PRIMARY>



0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home