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/