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

MongoDB serverStatus() ดูจำนวน Connection ที่เปิดอยู่

Command serverStatus() ใน MongoDB คืนค่าข้อมูลสถานะเซิร์ฟเวอร์แบบครบถ้วน รวมถึงจำนวน Connection ใน Field connections เนื้อหานี้สาธิต Output และการอ่านค่า Connection

Command

> db.serverStatus();

ผลลัพธ์ (ย่อ - แสดงเฉพาะส่วนสำคัญ)

{
  "host": "DESKTOP-QN2RB3H",
  "version": "4.0.5",
  "process": "mongod",
  "pid": NumberLong(10540),
  "uptime": 74156,
  "connections": {
    "current": 1,
    "available": 999999,
    "totalCreated": 1
  },
  ...
}

คำอธิบาย Connections Fields

Fieldความหมาย
currentจำนวน Connection ที่เปิดอยู่ขณะนี้ (Active Connections)
availableจำนวน Connection ที่ยังสร้างได้ (maxConns - current)
totalCreatedจำนวน Connection ที่สร้างมาตั้งแต่ Server Start

ข้อมูลสำคัญอื่นๆ ใน serverStatus

  • opcounters: Insert, Query, Update, Delete, GetMore, Command Counts
  • mem: Memory Usage (resident, virtual, mapped)
  • wiredTiger.cache: Cache Usage, Eviction Stats
  • globalLock: Lock Queue, Active Clients
  • network: Bytes In/Out, Requests, Compression
  • metrics: Command Metrics, Cursor, Document, Operation Stats
  • repl: Replication Status (ถ้าเป็น Replica Set)

การ Monitor Connection

  • ดู current ใกล้ available → Connection Exhaustion Risk
  • Default Max Connections: 1,000,000 (Config: net.maxIncomingConnections)
  • Driver Connection Pool: ตั้ง maxPoolSize ให้เหมาะสม (Default 100)
  • ใช้ db.currentOp() ดู Operation ที่รันอยู่
  • MongoDB Atlas / Cloud Manager: มี Dashboard Monitor Connection แบบ Real-time

Query แบบสั้นสำหรับดู Connection เท่านั้น

> db.serverStatus().connections
{ "current": 1, "available": 999999, "totalCreated": 1 }