Programing

MongoDb에 대한 현재 연결 수 확인

lottogame 2020. 11. 23. 07:37
반응형

MongoDb에 대한 현재 연결 수 확인


특정 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 서버에 작성된 모든 수신 연결의 수입니다. 이 숫자에는 이후 닫힌 연결이 포함됩니다.


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


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
}

(the 192.x.x.x addresses at Atlas internal monitoring)

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

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

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

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

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   

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

$ 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 grep etc, just use the lsof's arguments.

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


You can just use

db.serverStatus().connections

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

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

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

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


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

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

    },
    "replicaSets" : {

    },
    "ok" : 1
}

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

db.serverStatus().connections

e.g:

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

Connect with your mongodb instance from local system

  1. sudo mongo "mongodb://MONGO_HOST_IP:27017" --authenticationDatabase admin

It ll let you know all connected clients and their details

  1. db.currentOp(true)

참고URL : https://stackoverflow.com/questions/8975531/check-the-current-number-of-connections-to-mongodb

반응형