检查到 MongoDb 的当前连接数

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

178635 次浏览

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

> var status = db.serverStatus()
> status.connections
{"current" : 21, "available" : 15979}
>

您可以通过查询直接获得

db.serverStatus().connections

要理解 MongoDb 的 db.serverStatus().connections响应意味着什么,请阅读文档 给你

联系

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

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

连接,最新的 从客户端到数据库服务器的传入连接数。这个数字包括当前的 shell 会话。考虑 Connections.able 的值,以便为这个数据添加更多的上下文。

The value will include all incoming connections including any shell connections or connections from other servers, such as replica set members or mongos instances.

连接,可用 The 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.

连接,完全建立 创建到服务器的所有传入连接的计数。这个数字包括已经关闭的连接。

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

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

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

这个命令可以更详细地显示 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

在 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)
  • 不需要使用 grep等,只需使用 lsof的参数。

  • 也可以查看 MongoDb 的 CLI 上的连接,查看@milan 的答案(我刚剪辑过)。

RunCommand ({“ connPoolStats”: 1})

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


},
"replicaSets" : {


},
"ok" : 1
}

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

db.serverStatus().connections

例如:

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

还有一些更详细的连接: db.currentOp(true)

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

从本地系统连接您的 mongodb 实例

  1. Sudo MONGO“ mongob://MONGO _ HOST _ IP: 27017”—— enticationDatabase admin

它会让你知道所有连接的客户端和他们的详细信息

  1. db.currentOp(true)

你可以用

db.serverStatus().connections

此外,这个函数可以帮助您找到连接到您的 MongoDB 的 IP 地址

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

通过 ClientIP 进行连接计数,使用 Total

我们使用它来通过 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 })

格式:

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" : 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
}

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

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

或者,您可以通过登录到 蒙戈 · 阿特拉斯然后导航到您的集群来检查连接状态。

enter image description here

对不起,因为这是一个旧的职位,目前有更多的选择比以前。

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}}
] )

输出示例:

{ "_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 }