Check the current number of connections to MongoDb

前端 未结 12 1094
南方客
南方客 2020-12-12 14:27

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

相关标签:
12条回答
  • 2020-12-12 15:12

    I tried to see all connections for mongo database by following command.

    netstat -anp --tcp --udp | grep mongo
    

    This command can show every tcp connection for mongodb in more detail.

    tcp        0      0 10.26.2.185:27017           10.26.2.1:2715              ESTABLISHED 1442/./mongod       
    tcp        0      0 10.26.2.185:27017           10.26.2.1:1702              ESTABLISHED 1442/./mongod  
    tcp        0      0 10.26.2.185:27017           10.26.2.185:39506           ESTABLISHED 1442/./mongod       
    tcp        0      0 10.26.2.185:27017           10.26.2.185:40021           ESTABLISHED 1442/./mongod       
    tcp        0      0 10.26.2.185:27017           10.26.2.185:39509           ESTABLISHED 1442/./mongod 
    tcp        0      0 10.26.2.185:27017           10.26.2.184:46062           ESTABLISHED 1442/./mongod       
    tcp        0      0 10.26.2.185:27017           10.26.2.184:46073           ESTABLISHED 1442/./mongod       
    tcp        0      0 10.26.2.185:27017           10.26.2.184:46074           ESTABLISHED 1442/./mongod   
    
    0 讨论(0)
  • 2020-12-12 15:15

    Connect with your mongodb instance from local system

    1. sudo mongo "mongodb://MONGO_HOST_IP:27017" --authenticationDatabase admin

    It ll let you know all connected clients and their details

    1. db.currentOp(true)

    0 讨论(0)
  • 2020-12-12 15:26

    Alternatively you can check connection status by logging into Mongo Atlas and then navigating to your cluster.

    0 讨论(0)
  • 2020-12-12 15:27

    db.serverStatus() gives no of connections opend and avail but not shows the connections from which client. For more info you can use this command sudo lsof | grep mongod | grep TCP. I need it when i did replication and primary node have many client connection greater than secondary.

    $ sudo lsof | grep mongod | grep TCP
    mongod    5733             Al    6u     IPv4 0x08761278       0t0       TCP *:28017 (LISTEN)
    mongod    5733             Al    7u     IPv4 0x07c7eb98       0t0       TCP *:27017 (LISTEN)
    mongod    5733             Al    9u     IPv4 0x08761688       0t0       TCP 192.168.1.103:27017->192.168.1.103:64752 (ESTABLISHED)
    mongod    5733             Al   12u     IPv4 0x08761a98       0t0       TCP 192.168.1.103:27017->192.168.1.103:64754 (ESTABLISHED)
    mongod    5733             Al   13u     IPv4 0x095fa748       0t0       TCP 192.168.1.103:27017->192.168.1.103:64770 (ESTABLISHED)
    mongod    5733             Al   14u     IPv4 0x095f86c8       0t0       TCP 192.168.1.103:27017->192.168.1.103:64775 (ESTABLISHED)
    mongod    5733             Al   17u     IPv4 0x08764748       0t0       TCP 192.168.1.103:27017->192.168.1.103:64777 (ESTABLISHED)
    

    This shows that I currently have five connections open to the MongoDB port (27017) on my computer. In my case I'm connecting to MongoDB from a Scalatra server, and I'm using the MongoDB Casbah driver, but you'll see the same lsof TCP connections regardless of the client used (as long as they're connecting using TCP/IP).

    0 讨论(0)
  • 2020-12-12 15:33

    Also some more details on the connections with: db.currentOp(true)

    Taken from: https://jira.mongodb.org/browse/SERVER-5085

    0 讨论(0)
  • 2020-12-12 15:34

    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);
    
    0 讨论(0)
提交回复
热议问题