检查MongoDb的当前连接数

Daf*_*Dil 66 mongodb

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

mil*_*lan 128

连接到管理数据库并运行db.serverStatus():

> var status = db.serverStatus()
> status.connections
   {"current" : 21, "available" : 15979}
> 
Run Code Online (Sandbox Code Playgroud)

你可以通过查询直接获得

db.serverStatus().connections
Run Code Online (Sandbox Code Playgroud)

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

连接

"connections" : {
   "current" : <num>,
   "available" : <num>,
   "totalCreated" : NumberLong(<num>)
},
Run Code Online (Sandbox Code Playgroud)

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

connections.current 从客户端到数据库服务器的传入连接数.此数字包括当前的shell会话.考虑connections.available的值,为此数据添加更多上下文.

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

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

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


Hit*_*dra 18

db.serverStatus()不提供连接opend和avail但不显示客户端的连接.有关更多信息,您可以使用此命令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)
Run Code Online (Sandbox Code Playgroud)

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

  • 我建议为lsof使用-i标志.然后,每个连接只能获得1个条目,而不需要grep for TCP.即`sudo lsof -i | grep mongod` (2认同)

Sup*_*eam 11

ClientIP的连接计数,总计

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

对于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 })
Run Code Online (Sandbox Code Playgroud)

格式:

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 }
)
Run Code Online (Sandbox Code Playgroud)

返回示例:

{
    "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
}
Run Code Online (Sandbox Code Playgroud)

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

“内部”是没有外部客户端的内部流程。您可以使用以下命令查看这些列表:

db.currentOp(true).inprog.filter(connection => !connection.client).map(connection => connection.desc);
Run Code Online (Sandbox Code Playgroud)


Kya*_*u L 6

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

netstat -anp --tcp --udp | grep mongo
Run Code Online (Sandbox Code Playgroud)

此命令可以更详细地显示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   
Run Code Online (Sandbox Code Playgroud)


Ina*_*mus 6

在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)
Run Code Online (Sandbox Code Playgroud)
  • 不需要使用grep等,只需使用lsof的参数.

  • 在MongoDb的CLI上看到连接,请参阅@milan的回答(我刚刚编辑过).


小智 6

抱歉,因为这是一篇旧文章,目前有比以前更多的选项。

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}}
] )
Run Code Online (Sandbox Code Playgroud)

输出示例:

{ "_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 }
Run Code Online (Sandbox Code Playgroud)


mit*_*1os 5

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

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


nix*_*ix6 5

你可以用

db.serverStatus().connections
Run Code Online (Sandbox Code Playgroud)

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

db.currentOp(true).inprog.forEach(function(x) { print(x.client) })
Run Code Online (Sandbox Code Playgroud)