关闭很简单:
db.setProfilingLevel(0)
常用命令速查
| 操作 | 命令 |
|---|---|
| 开启(记录所有操作) | db.setProfilingLevel(2) |
| 开启(只记录慢查询,超过 100ms) | db.setProfilingLevel(1, { slowms: 100 }) |
| 开启(只记录特定操作) | db.setProfilingLevel(2, { filter: { op: { $in: ["insert", "update", "remove"] } } }) |
| 关闭 | db.setProfilingLevel(0) |
| 查看当前级别 | db.getProfilingStatus() |
清理已记录的日志
关闭 profiler 后,之前记录在 system.profile 里的数据还在,可以手动清理:
// 清空 profiling 数据
db.system.profile.drop()
// 重新创建空的 capped collection(可选,profiler 会自动重建)
db.createCollection("system.profile", { capped: true, size: 1048576 })
或者直接不管,capped collection 满了会自动覆盖老的。
确认已关闭
db.getProfilingStatus()
// 返回 { "was" : 0, "slowms" : 100 } 表示已关闭
如果之前开了 auditAuthorizationSuccess 参数,也记得关掉:
db.adminCommand({ setParameter: 1, auditAuthorizationSuccess: false })