MongoDB Profiler能夠收集MongoDB操作的操作信息,尤其是分析慢操作時的利器。MongoDB Profiler將收集到的數據存到system.profile集合中,它是一個capped collection(固定大小的集合)。
Profiling Levels
設置Profiler的級別有以下幾個選項:
- 0:設置爲off,這是默認級別,即關閉profiler。但是mongod始終將執行時間超過slowOpThresholdMs參數值的操作記錄到log中。
- 1:收集慢操作的信息。默認操作超過100ms即 爲慢操作,這個時間可以設置。
-2:收集所有操作的信息。
開啓Profiling,設置級別
可以通過配置文件、shell命令、驅動等方式設置,這裏介紹配置文件和shell命令方式。
配置文件設置
- operationProfiling.slowOpThresholdMs
Type:integer
Default:100
這個參數設置慢操作的閾值,時間爲ms,默認100ms。MongoDB始終記錄慢操作到日誌中,哪怕profiler關閉。如果profiler是on狀態,那麼就將
慢操作信息寫入system.profile集合。
- operationProfiling.mode
Type: string
Default: off
profiling級別設置:
operationProfiling.mode的設置只對mongod有效。
官方說明profiling不同的級別對性能的影響不同,建議慎重考慮。
Database profiling can impact database performance. Enable this option only after careful consideration.
shell Command
db.setProfilingLevel(level, slowms)
可以通過該命令設置級別和慢操作閾值。
注意,這個命令不能在mongos是執行,會報如下錯誤,需要在mongod節點上執行:
查看Profiler數據
當我設置了profile級別後,在test庫中有一個system.profile集合,可以查看這個庫中的數據,字段含義參考官方文檔:https://docs.mongodb.com/manual/reference/database-profiler/
參考
https://docs.mongodb.com/manual/reference/command/profile/
https://docs.mongodb.com/manual/reference/configuration-options/