DBAtlas · データベース運用アトラス

アプリケーションとクライアント別に接続数を数える (コネクションストームの診断)

MongoDBパフォーマンス

コマンド

// Total, available and rejected connection counts:
db.serverStatus().connections
// Count connections by application name and client IP:
db.getSiblingDB("admin").aggregate([
  { $currentOp: { allUsers: true, idleConnections: true } },
  { $group: { _id: { app: "$appName", client: { $arrayElemAt: [{ $split: ["$client", ":"] }, 0] } }, conns: { $sum: 1 }, active: { $sum: { $cond: ["$active", 1, 0] } } } },
  { $sort: { conns: -1 } },
  { $limit: 20 }
])

概要

サーバー上の合計接続数と利用可能な接続数を表示し、オープン中の接続を appName とクライアント IP ごとに、アクティブな数とともにグループ化する。

使用する場面

接続数が急増した場合、'connection refused' やプールのタイムアウトエラーの発生時、どのアプリケーションサーバーが接続をリークしているかを特定するため。

注意

idleConnections: true はすべてのアイドル接続も返すため、数千の接続があるシステムでは出力が大きくなる。そのため $group による要約が重要である。client が null の行は内部スレッドである。各接続は約 1 MB のメモリを消費する。コネクションストームは WiredTiger キャッシュ外のメモリも増大させる。

DBAtlas で開く →

関連項目