检查当前与 MongoDb 的连接数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8975531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:26:03  来源:igfitidea点击:

Check the current number of connections to MongoDb

mongodb

提问by DafaDil

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

获取连接到特定 MongoDB 服务器的客户端数量的命令是什么?

回答by milan

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

连接到管理数据库并运行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().connectionsresponse mean, read the documentation here.

要了解 MongoDb 的db.serverStatus().connections响应是什么意思,请阅读此处的文档。

connections

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

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

connections.currentThe 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.availableThe 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.totalCreatedCount of all incoming connections created to the server. This number includes connections that have since closed.

连接

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

连接报告连接状态的文档。使用这些值来评估服务器的当前负载和容量要求。

connection.current从客户端到数据库服务器的传入连接数。这个数字包括当前的 shell 会话。考虑connections.available 的值以向该数据添加更多上下文。

该值将包括所有传入连接,包括任何 shell 连接或来自其他服务器的连接,例如副本集成员或 mongos 实例。

connections.available可用的未使用的传入连接的数目。将此值与connections.current 的值结合考虑以了解数据库上的连接负载,以及有关可用连接的系统阈值的更多信息的UNIX ulimit 设置文档。

connection.totalCreated创建到服务器的所有传入连接的计数。此数字包括已关闭的连接。

回答by Hitesh Mundra

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.

db.serverStatus()没有提供打开和可用的连接,但没有显示来自哪个客户端的连接。有关更多信息,您可以使用此命令sudo lsof | grep mongod | grep TCP。当我进行复制并且主节点有许多客户端连接大于辅助节点时,我需要它。

$ 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).

这表明我目前有五个连接打开到我计算机上的 MongoDB 端口 (27017)。在我的情况下,我从 Scalatra 服务器连接到 MongoDB,并且我使用的是 MongoDB Casbah 驱动程序,但是无论使用什么客户端,您都会看到相同的 lsof TCP 连接(只要它们使用 TCP/知识产权)。

回答by SuperGoTeam

Connection Count by ClientIP, with Total

ClientIP 的连接数,总计

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!

我们使用它来查看 IPAddress 的连接数和总连接数。这对于调试问题真的很有帮助......在达到最大连接数之前到达那里!

For Mongo Shell:

对于 Mongo 外壳:

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)

(Atlas内部监控的192.xxx地址)

"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);

回答by Kyaw Min Thu L

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

我尝试通过以下命令查看 mongo 数据库的所有连接。

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

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

此命令可以更详细地显示 mongodb 的每个 tcp 连接。

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   

回答by Inanc Gumus

In OS X, too see the connections directly on the network interface, just do:

在 OS X 中,也可以直接在网络接口上查看连接,只需执行以下操作

$ lsof -n -i4TCP:27017

mongod     2191 inanc    7u  IPv4 0xab6d9f844e21142f  0t0  TCP 127.0.0.1:27017 (LISTEN)
mongod     2191 inanc   33u  IPv4 0xab6d9f84604cd757  0t0  TCP 127.0.0.1:27017->127.0.0.1:56078 (ESTABLISHED)
stores.te 18704 inanc    6u  IPv4 0xab6d9f84604d404f  0t0  TCP 127.0.0.1:56078->127.0.0.1:27017 (ESTABLISHED)
  • No need to use grepetc, just use the lsof's arguments.

  • Too see the connections on MongoDb's CLI, see @milan's answer (which I just edited).

  • 无需使用grep等,只需使用lsof's 参数。

  • 太查看 MongoDb 的 CLI 上的连接,请参见 @milan 的答案(我刚刚编辑过)。

回答by nixsix6

You can just use

你可以使用

db.serverStatus().connections

Also, this function can help you spot the IP addresses connected to your Mongo DB

此外,此功能可以帮助您发现连接到您的 Mongo DB 的 IP 地址

db.currentOp(true).inprog.forEach(function(x) { print(x.client) })

回答by mitsos1os

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

还有一些关于连接的更多细节: db.currentOp(true)

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

摘自:https: //jira.mongodb.org/browse/SERVER-5085

回答by arnav

db.runCommand( { "connPoolStats" : 1 } )

db.runCommand( { "connPoolStats" : 1 } )

{
    "numClientConnections" : 0,
    "numAScopedConnections" : 0,
    "totalInUse" : 0,
    "totalAvailable" : 0,
    "totalCreated" : 0,
    "hosts" : {

    },
    "replicaSets" : {

    },
    "ok" : 1
}

回答by Thushan

Connect to MongoDB using mongo-shell and run following command.

使用 mongo-shell 连接到 MongoDB 并运行以下命令。

db.serverStatus().connections

e.g:

例如:

mongo> db.serverStatus().connections
{ "current" : 3, "available" : 816, "totalCreated" : NumberLong(1270) }

回答by JuanM

Sorry because this is an old post and currently there is more options than before.

抱歉,因为这是一个旧帖子,目前有比以前更多的选择。

db.getSiblingDB("admin").aggregate( [
   { $currentOp: { allUsers: true, idleConnections: true, idleSessions: true } }
  ,{$project:{
            "_id":0
           ,client:{$arrayElemAt:[ {$split:["$client",":"]}, 0 ] }
           ,curr_active:{$cond:[{$eq:["$active",true]},1,0]}
           ,curr_inactive:{$cond:[{$eq:["$active",false]},1,0]}
           }
   }
  ,{$match:{client:{$ne: null}}}
  ,{$group:{_id:"$client",curr_active:{$sum:"$curr_active"},curr_inactive:{$sum:"$curr_inactive"},total:{$sum:1}}}
  ,{$sort:{total:-1}}
] )

Output example:

输出示例:

{ "_id" : "xxx.xxx.xxx.78", "curr_active" : 0, "curr_inactive" : 1428, "total" : 1428 }
{ "_id" : "xxx.xxx.xxx.76", "curr_active" : 0, "curr_inactive" : 1428, "total" : 1428 }
{ "_id" : "xxx.xxx.xxx.73", "curr_active" : 0, "curr_inactive" : 1428, "total" : 1428 }
{ "_id" : "xxx.xxx.xxx.77", "curr_active" : 0, "curr_inactive" : 1428, "total" : 1428 }
{ "_id" : "xxx.xxx.xxx.74", "curr_active" : 0, "curr_inactive" : 1428, "total" : 1428 }
{ "_id" : "xxx.xxx.xxx.75", "curr_active" : 0, "curr_inactive" : 1428, "total" : 1428 }
{ "_id" : "xxx.xxx.xxx.58", "curr_active" : 0, "curr_inactive" : 510, "total" : 510 }
{ "_id" : "xxx.xxx.xxx.57", "curr_active" : 0, "curr_inactive" : 459, "total" : 459 }
{ "_id" : "xxx.xxx.xxx.55", "curr_active" : 0, "curr_inactive" : 459, "total" : 459 }
{ "_id" : "xxx.xxx.xxx.56", "curr_active" : 0, "curr_inactive" : 408, "total" : 408 }
{ "_id" : "xxx.xxx.xxx.47", "curr_active" : 1, "curr_inactive" : 11, "total" : 12 }
{ "_id" : "xxx.xxx.xxx.48", "curr_active" : 1, "curr_inactive" : 7, "total" : 8 }
{ "_id" : "xxx.xxx.xxx.51", "curr_active" : 0, "curr_inactive" : 8, "total" : 8 }
{ "_id" : "xxx.xxx.xxx.46", "curr_active" : 0, "curr_inactive" : 8, "total" : 8 }
{ "_id" : "xxx.xxx.xxx.52", "curr_active" : 0, "curr_inactive" : 6, "total" : 6 }
{ "_id" : "127.0.0.1", "curr_active" : 1, "curr_inactive" : 0, "total" : 1 }
{ "_id" : "xxx.xxx.xxx.3", "curr_active" : 0, "curr_inactive" : 1, "total" : 1 }