特定のMongoDBサーバーに接続されているクライアントの数を取得するコマンドは何ですか?
回答:
adminデータベースに接続して、以下を実行します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の値と組み合わせて検討し、データベースの接続負荷を理解してください。使用可能な接続のシステムしきい値の詳細については、UNIXulimit設定のドキュメントを参照してください。
connections.totalCreated サーバーに対して作成されたすべての着信接続の数。この数には、その後閉じられた接続が含まれます。
ClientIPごとの接続数、合計
これを使用して、IPアドレスごとの接続数と合計接続数を表示します。これは問題のデバッグに非常に役立ちました...最大接続数に達する前にそこに到達してください!
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ドライバーを使用していますが、使用しているクライアントに関係なく、同じlsof 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
。's引数を使用するだけです。
MongoDbのCLIで接続が表示されすぎている場合は、@ milanの回答(編集したばかり)を参照してください。
あなたはただ使うことができます
db.serverStatus().connections
また、この機能は、MongoDBに接続されている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にログインしてからクラスターに移動することで、接続ステータスを確認できます。