如何在MongoDB分片环境中记录所有查询?

时间:2017-06-14 13:23:36

标签: mongodb

为了符合新规定,我需要使用发起人的用户名记录发送到数据库的所有查询。 在非分片环境中,我只需使用命令db.setProfilingLevel(2)将分析级别设置为2。我可以在我的集群的每个节点上执行相同操作,但后来我不知道如何访问system.profile集合。

有没有办法直接在日志文件中记录mongos收到的所有查询?我看到企业版中有一些审计参数,有没有办法与社区版做类似的事情?

理想的日志包含:

  • 分贝/收集
  • 经过身份验证的用户
  • 时间戳
  • 查询

1 个答案:

答案 0 :(得分:0)

通过将threshold for 'slow' operations设置为零,您可以使分析功能将每个查询记录到日志中:

db.setProfilingLevel(0, 0);

这不会启用完整的profiler,但会使其记录有关每个查询的一些数据,并将其写入服务器日志,如下所示:

  

2017-06-15T09:15:58.809 + 0000 I COMMAND [conn305112] query mydatabasename.mycollectionname planSummary:COLLSCAN ntoreturn:1000 ntoskip:0 nscanned:0 nscannedObjects:6 keyUpdates:0 writeConflicts:0 numYields:0 nreturned:6 reslen:4863 lock:{Global:{acquireCount:{r:2}},Database:{acquireCount:{r:1}},Collection:{acquireCount:{r:1}}} 1ms

这并不能真正为您提供所要求的信息,但如果您需要,那么我认为您必须为MongoDB Enterprise付费才能获得auditing functionality