Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MongoDB

Cách kiểm tra Storage Engine đang dùng trong MongoDB

Để biết storage engine nào đang được sử dụng, dùng lệnh db.serverStatus().storageEngine. Để xem cấu hình chi tiết, truy cập thuộc tính tên engine (ví dụ: wiredTiger).

Cú pháp

// Xem tên storage engine
db.serverStatus().storageEngine;

// Xem cấu hình chi tiết (thay wiredTiger bằng tên engine thực tế)
db.serverStatus().wiredTiger;

Ví dụ thực hành

Kiểm tra engine hiện tại:

> db.serverStatus().storageEngine;

Kết quả:

{
    "name" : "wiredTiger",
    "supportsCommittedReads" : true,
    "supportsSnapshotReadConcern" : true,
    "readOnly" : false,
    "persistent" : true
}

Xem cấu hình chi tiết WiredTiger:

> db.serverStatus().wiredTiger;

Kết quả (rút gọn các nhóm chính):

{
    "uri" : "statistics:",
    "cache" : { 
        "maximum bytes configured" : 4803526656,   // Cache size tối đa (bytes)
        "bytes currently in the cache" : 814060,
        "tracked dirty bytes in the cache" : 0,
        "pages currently held in the cache" : 349,
        "eviction worker thread active" : 4
    },
    "connection" : { "files currently open" : 170, ... },
    "cursor" : { "cursor create calls" : 536, ... },
    "lock" : { ... },
    "log" : { "log flush operations" : 68872, ... },
    "transaction" : { "transactions committed" : 40, "transactions rolled back" : 269, ... },
    "concurrentTransactions" : { 
        "write" : { "out" : 0, "available" : 128, "totalTickets" : 128 },
        "read" : { "out" : 1, "available" : 127, "totalTickets" : 128 }
    }
}

Các Storage Engine phổ biến

  • WiredTiger: Engine mặc định từ MongoDB 3.2. Hỗ trợ nén (compression), document-level concurrency, checkpoint, snapshot.
  • In-Memory: (Enterprise) Lưu dữ liệu hoàn toàn trong RAM, tốc độ cực nhanh nhưng mất dữ liệu khi restart.
  • Encrypted: (Enterprise) WiredTiger với mã hóa dữ liệu tại chỗ (Encryption at Rest).

Kiểm tra qua file cấu hình (mongod.conf)

storage:
  engine: wiredTiger
  wiredTiger:
    engineConfig:
      cacheSizeGB: 4
    collectionConfig:
      blockCompressor: snappy  # hoặc zlib, zstd, none
    indexConfig:
      prefixCompression: true

Lưu ý: Từ MongoDB 4.2+, MMAPv1 đã bị loại bỏ hoàn toàn.