在 Linux 上配置 MongoDB 审计(Audit),核心是通过 MongoDB Enterprise 的 Audit Log(社区版不支持完整审计,只能用慢查询日志等代替)。下面按「是否能用 Enterprise」和「社区版替代方案」说明。
✅ 只有 MongoDB Enterprise 支持完整审计功能
security:
auditLog:
destination: file
format: JSON
path: /var/log/mongodb/auditLog.json
或输出到 syslog:
security:
auditLog:
destination: syslog
format: JSON
例如只审计 admin 库和删除操作:
security:
auditLog:
destination: file
format: JSON
path: /var/log/mongodb/auditLog.json
filter: '{ "atype": { "$in": ["delete", "drop", "authCheck"] } }'
systemctl restart mongod
tail -f /var/log/mongodb/auditLog.json
示例记录:
{
"ts": { "$date": "2024-01-01T10:00:00Z" },
"op": "command",
"ns": "admin.$cmd",
"user": "admin",
"atype": "authenticate"
}
operationProfiling:
slowOpThresholdMs: 100
mode: all
查看:
tail -f /var/log/mongodb/mongod.log
⚠️ 只能看到执行语句,看不到“谁执行的”
auditctl -w /var/lib/mongodb -p rwxa -k mongodb
查看:
ausearch -k mongodb
db.currentOp() 分析实时操作如果你告诉我:
我可以给你一套 可直接用的配置模板。