这个问题既简单又基本。如何将所有查询记录在mongodb中的“尾部”日志文件中?

我试过:

设置概要级别 设置启动慢ms参数 使用-vv选项Mongod

/var/log/mongodb/mongodb.log一直显示当前活动连接的数量…


当前回答

您可以记录所有查询:

$ mongo
MongoDB shell version: 2.4.9
connecting to: test
> use myDb
switched to db myDb
> db.getProfilingLevel()
0
> db.setProfilingLevel(2)
{ "was" : 0, "slowms" : 1, "ok" : 1 }
> db.getProfilingLevel()
2
> db.system.profile.find().pretty()

来源:http://docs.mongodb.org/manual/reference/method/db.setProfilingLevel/

db.setProfilingLevel(2)表示“记录所有操作”。

其他回答

一旦使用db.setProfilingLevel(2)设置了分析级别。

下面的命令将打印最后执行的查询。 您也可以更改限制(5)以查看更少/更多的查询。 $nin -过滤概要文件和索引查询 此外,使用查询投影{'query':1}仅用于查看查询字段

db.system.profile.find(
{ 
    ns: { 
        $nin : ['meteor.system.profile','meteor.system.indexes']
    }
} 
).limit(5).sort( { ts : -1 } ).pretty()

只有查询投影的日志

db.system.profile.find(
{ 
    ns: { 
        $nin : ['meteor.system.profile','meteor.system.indexes']
    }
},
{'query':1}
).limit(5).sort( { ts : -1 } ).pretty()

我最终通过像这样开始mongod来解决这个问题(锤击和丑陋,是的……但适用于开发环境):

mongod --profile=1 --slowms=1 &

这将启用分析,并将“慢查询”的阈值设置为1毫秒,导致所有查询都被记录为文件的“慢查询”:

/var/log/mongodb/mongodb.log

现在我得到连续的日志输出使用命令:

tail -f /var/log/mongodb/mongodb.log

日志示例:

Mon Mar  4 15:02:55 [conn1] query dendro.quads query: { graph: "u:http://example.org/people" } ntoreturn:0 ntoskip:0 nscanned:6 keyUpdates:0 locks(micros) r:73163 nreturned:6 reslen:9884 88ms

MongoDB有一个复杂的性能分析功能。日志记录发生在系统中。配置文件收集。日志内容如下:

db.system.profile.find()

有3个日志级别(源):

Level 0 - the profiler is off, does not collect any data. mongod always writes operations longer than the slowOpThresholdMs threshold to its log. This is the default profiler level. Level 1 - collects profiling data for slow operations only. By default slow operations are those slower than 100 milliseconds. You can modify the threshold for “slow” operations with the slowOpThresholdMs runtime option or the setParameter command. See the Specify the Threshold for Slow Operations section for more information. Level 2 - collects profiling data for all database operations.

要查看数据库运行在哪个分析级别,请使用

db.getProfilingLevel()

并查看状态

db.getProfilingStatus()

要更改分析状态,使用该命令

db.setProfilingLevel(level, milliseconds)

其中level指的是分析级别,毫秒是需要记录查询持续时间的ms。若要关闭日志记录,请使用

db.setProfilingLevel(0)

在系统概要集合中查找耗时超过一秒的所有查询(按时间戳降序排序)的查询将为

db.system.profile.find( { millis : { $gt:1000 } } ).sort( { ts : -1 } )

因为它的谷歌第一个答案… 对于版本3

$ mongo
MongoDB shell version: 3.0.2
connecting to: test
> use myDb
switched to db
> db.setLogLevel(1)

http://docs.mongodb.org/manual/reference/method/db.setLogLevel/

我建议你看看mongosniff。这个工具可以做任何你想做的事情,甚至更多。特别是它可以帮助诊断大规模mongo系统的问题,以及查询是如何路由的以及它们来自哪里,因为它是通过监听您的网络接口进行所有与mongo相关的通信来工作的。

http://docs.mongodb.org/v2.2/reference/mongosniff/