MongoDb에 대한 현재 연결 수 확인


90

특정 MongoDB 서버에 연결된 클라이언트 수를 가져 오는 명령은 무엇입니까?

답변:


163

관리 데이터베이스에 연결하고 다음을 실행하십시오 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 서버에 작성된 모든 수신 연결의 수입니다. 이 숫자에는 이후 닫힌 연결이 포함됩니다.


25

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

반환 목록에서 "내부"IP의 의미를 설명해 주시겠습니까?
carton.swing

E QUERY [js] TypeError: db.currentOp(...).inprog is undefined :
mongo

@ carton.swing 설명과 명령으로 답변을 업데이트했습니다.
SuperGoTeam

@otong 당신은 단지 db.currentOp(true)무엇을 돌려 받습니까?
SuperGoTeam

:이 MongoDB를지도 책에 의해 거부 것처럼 보인다 { "ok" : 0, "errmsg" : "Using $all for currentOp is disallowed in this atlas tier", "code" : 8000, "codeName" : "AtlasError" }
otong

19

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


1
이 명령은 단일 연결에 대해 여러 항목을 반환합니다. stackoverflow.com/a/42930337/1843751
ignite

3
lsof에 -i 플래그를 사용하는 것이 좋습니다. 그런 다음 각 연결에 1 개의 항목 만 가져 오며 TCP에 대해 grep 할 필요가 없습니다. 즉sudo lsof -i | grep mongod
datdo

7

다음 명령으로 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   

7

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의 답변을 참조하십시오 ( 방금 편집했습니다 ).


7

그냥 사용할 수 있습니다

db.serverStatus().connections

또한이 기능은 Mongo DB에 연결된 IP 주소를 찾는 데 도움이 될 수 있습니다.

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

1
절대적으로 아름답습니다-감사합니다! 내가 찾던 바로 그것.
ProsperousHeart

4

또한 다음과의 연결에 대한 자세한 내용은 다음과 같습니다. db.currentOp(true)

출처 : https://jira.mongodb.org/browse/SERVER-5085


나는 모든 답변을 읽었고 갑자기 이것이 가장 도움이되는 것 중 하나입니다. 지정된 경우는 연결 시간, 현재 테이블, 드라이버 버전 및 플랫폼, 심지어 프로그램 응용을 포함해야 같은 쿼리는 내부 세부 사항의 톤을 제공합니다
드미트리 Gusarov을

2

mongo-shell을 사용하여 MongoDB에 연결하고 다음 명령을 실행하십시오.

db.serverStatus().connections

예 :

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

2

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

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

    },
    "replicaSets" : {

    },
    "ok" : 1
}

그것은, 나 또한 그 요청에 대한 ZERO 값을 quate 흥미로운 받게되고, 내가 :) 원하는되지 않습니다 MongoMonitoringController : { "numClientConnections" : 0 , "numAScopedConnections" : 0 , "totalInUse" : 0 , "totalAvailable" : 0 , "totalCreated" : 0 , "totalRefreshing" : 0 , "pools" : { } , "hosts" : { } , "replicaSets" : { } , "ok" : 1.0}
알렉스 Efimov

2

이것은 이전 게시물이며 현재 이전보다 더 많은 옵션이 있기 때문에 죄송합니다.

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 }

1

로컬 시스템에서 mongodb 인스턴스와 연결

  1. sudo mongo "mongodb : // MONGO_HOST_IP : 27017"--authenticationDatabase 관리자

연결된 모든 클라이언트와 세부 정보를 알려줍니다.

  1. db.currentOp (true)


0

또는 Mongo Atlas 에 로그인 한 다음 클러스터로 이동하여 연결 상태를 확인할 수 있습니다.

여기에 이미지 설명 입력

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.