특정 MongoDB 서버에 연결된 클라이언트 수를 가져 오는 명령은 무엇입니까?
답변:
관리 데이터베이스에 연결하고 다음을 실행하십시오 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>) },연결 연결 상태를보고하는 문서입니다. 이 값을 사용하여 서버의 현재로드 및 용량 요구 사항을 평가합니다.
connections.current 클라이언트에서 데이터베이스 서버로 들어오는 연결 수. 이 번호에는 현재 셸 세션이 포함됩니다. 이 데이텀에 더 많은 컨텍스트를 추가하려면 connections.available 값을 고려하십시오.
이 값에는 복제 세트 구성원 또는 mongos 인스턴스와 같은 다른 서버의 연결 또는 쉘 연결을 포함한 모든 수신 연결이 포함됩니다.
connections.available 사용 가능한 사용되지 않은 수신 연결 수입니다. 이 값을 connections.current 값과 결합하여 데이터베이스의 연결로드를 이해하고 사용 가능한 연결의 시스템 임계 값에 대한 자세한 정보는 UNIX ulimit 설정 문서를 고려하십시오.
connections.totalCreated 서버에 작성된 모든 수신 연결의 수입니다. 이 숫자에는 이후 닫힌 연결이 포함됩니다.
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 })
포맷 :
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.xxx 주소)
"내부"는 외부 클라이언트가없는 내부 프로세스입니다. 다음과 같이 목록을 볼 수 있습니다.
db.currentOp(true).inprog.filter(connection => !connection.client).map(connection => connection.desc);
E QUERY [js] TypeError: db.currentOp(...).inprog is undefined :
db.currentOp(true)무엇을 돌려 받습니까?
{ "ok" : 0, "errmsg" : "Using $all for currentOp is disallowed in this atlas tier", "code" : 8000, "codeName" : "AtlasError" }
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)
이것은 현재 컴퓨터의 MongoDB 포트 (27017)에 대해 5 개의 연결이 열려 있음을 보여줍니다. 제 경우에는 Scalatra 서버에서 MongoDB에 연결하고 MongoDB Casbah 드라이버를 사용하고 있지만 사용 된 클라이언트에 관계없이 동일한 TCP 연결을 볼 수 있습니다 (TCP /를 사용하여 연결하는 한). IP).
sudo lsof -i | grep mongod
다음 명령으로 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의 답변을 참조하십시오 ( 방금 편집했습니다 ).
그냥 사용할 수 있습니다
db.serverStatus().connections
또한이 기능은 Mongo DB에 연결된 IP 주소를 찾는 데 도움이 될 수 있습니다.
db.currentOp(true).inprog.forEach(function(x) { print(x.client) })
또한 다음과의 연결에 대한 자세한 내용은 다음과 같습니다.
db.currentOp(true)
db.runCommand ({ "connPoolStats": 1})
{
"numClientConnections" : 0,
"numAScopedConnections" : 0,
"totalInUse" : 0,
"totalAvailable" : 0,
"totalCreated" : 0,
"hosts" : {
},
"replicaSets" : {
},
"ok" : 1
}
MongoMonitoringController : { "numClientConnections" : 0 , "numAScopedConnections" : 0 , "totalInUse" : 0 , "totalAvailable" : 0 , "totalCreated" : 0 , "totalRefreshing" : 0 , "pools" : { } , "hosts" : { } , "replicaSets" : { } , "ok" : 1.0}
이것은 이전 게시물이며 현재 이전보다 더 많은 옵션이 있기 때문에 죄송합니다.
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 }
또는 Mongo Atlas 에 로그인 한 다음 클러스터로 이동하여 연결 상태를 확인할 수 있습니다.