Check the current number of connections to MongoDb

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

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

12条回答
  •  萌比男神i
    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);
    

提交回复
热议问题