Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MongoDB

วิธีดู Connected Clients ใน MongoDB

การดู Connected Clients (การเชื่อมต่อปัจจุบัน) ใน MongoDB

ใช้ คำสั่ง db.currentOp(true) เพื่อดู Operations ที่กำลังทำงานอยู่ รวมถึง Client Connections ทั้งหมด

คำสั่งหลัก

> db.currentOp(true)

Parameter true: แสดง Operations ทั้งหมด (รวม Idle Connections) ไม่ใช่แค่ Active Operations

ผลลัพธ์ตัวอย่าง (สรุปสำคัญ)

{ 
    "inprog" : [ 
        { 
            "host" : "DESKTOP-QN2RB3H:27017",
            "desc" : "conn1",
            "connectionId" : 1,
            "client" : "127.0.0.1:61787",
            "appName" : "MongoDB Shell",
            "clientMetadata" : {
                "application" : { "name" : "MongoDB Shell" },
                "driver" : { "name" : "MongoDB Internal Client", "version" : "4.0.5" },
                "os" : { "type" : "Windows", "name" : "Microsoft Windows 10", "architecture" : "x86_64", "version" : "10.0 (build 17134)" }
            },
            "active" : true,
            "currentOpTime" : "2019-03-30T05:13:34.296+0530",
            "opid" : 12538,
            "lsid" : { "id" : UUID("da370b0f-093a-45f3-b1fe-697daca8ccd2") },
            "secs_running" : NumberLong(0),
            "microsecs_running" : NumberLong(160815),
            "op" : "command",
            "ns" : "admin.$cmd.aggregate",
            "command" : { "currentOp" : 1, "$all" : true, "$db" : "admin" },
            "numYields" : 0,
            "locks" : {},
            "waitingForLock" : false
        },
        { "host" : "DESKTOP-QN2RB3H:27017", "desc" : "LogicalSessionCacheRefresh", "active" : false, ... },
        { "host" : "DESKTOP-QN2RB3H:27017", "desc" : "SessionKiller", "active" : false, ... },
        { "host" : "DESKTOP-QN2RB3H:27017", "desc" : "TTLMonitor", "active" : false, ... },
        // ... Internal Threads
    ], 
    "ok" : 1 
}

ฟิลด์สำคัญใน Output

การกรองเฉพาะ Client Connections (ไม่ใช่ Internal Threads)

> db.currentOp(true).inprog.filter(function(op) { 
    return op.client && !op.desc.startsWith("initandlisten") && !op.desc.includes("Thread"); 
}).forEach(function(op) {
    print("Client: " + op.client + ", App: " + op.appName + ", DB: " + op.ns + ", Op: " + op.op);
});

คำสั่งย่อสำหรับดู Active Connections

> db.currentOp(true).inprog
    .filter(op => op.client && op.active)
    .forEach(op => print(op.client + " | " + op.appName + " | " + op.ns + " | " + op.op));

การ Kill Long-running Operation

// หา opid ของ Operation ที่รันนาน
> var ops = db.currentOp(true).inprog.filter(op => op.secs_running > 60);
> ops.forEach(op => print("Kill opid: " + op.opid + " - " + op.ns + " - " + op.secs_running + "s"));

// Kill Operation
> db.killOp(opid);

Alternative: db.serverStatus().connections

> db.serverStatus().connections;
{ "current" : 15, "available" : 81970, "totalCreated" : 12538 }
  • current : จำนวน Connection ปัจจุบัน
  • available : Connection ที่เหลือ (max - current)
  • totalCreated : สร้างมาทั้งหมดตั้งแต่ Startup

Monitoring Tools แนะนำ

  • MongoDB Atlas / Cloud Manager : Dashboard แสดง Connections, Operations Real-time
  • mongostat : CLI Real-time Stats (connections, ops/sec, etc.)
  • mongotop : Collection-level Read/Write Time
  • Prometheus + Grafana : Export Metrics ด้วย MongoDB Exporter

Best Practices

  • Monitor current Connections ใกล้ Limit (default 65536 หรือ ulimit)
  • ตั้งค่า maxIncomingConnections ใน mongod.conf
  • ใช้ Connection Pooling ใน Application (Driver Default 100)
  • Kill Long-running Queries ที่ Block Resources
  • Check waitingForLock: true เพื่อหา Blocking Issues

db.currentOp() เป็นคำสั่งสำคัญสำหรับ Database Administration และ Troubleshooting Performance

Fieldคำอธิบาย
clientIP:Port ของ Client ที่เชื่อมต่อ (เช่น 127.0.0.1:61787)
appNameApplication Name ที่ Client ส่งมา (Driver/Tool)
connectionIdConnection ID ภายใน MongoDB
descDescription ของ Connection/Thread
activetrue = กำลัง Execute Operation, false = Idle/Background Thread
opOperation Type: query, command, insert, update, delete, getmore, killcursors
nsNamespace (Database.Collection) ที่ Operation กำลังทำงาน
commandCommand Document ที่กำลัง Execute
secs_running / microsecs_runningเวลาที่ Operation รันอยู่ (Detect Long-running Queries)
waitingForLocktrue = รอ Lock (Potential Blocking)
clientMetadataข้อมูล Driver, OS, Application Version