这个问题既简单又基本。如何将所有查询记录在mongodb中的“尾部”日志文件中?
我试过:
设置概要级别 设置启动慢ms参数 使用-vv选项Mongod
/var/log/mongodb/mongodb.log一直显示当前活动连接的数量…
这个问题既简单又基本。如何将所有查询记录在mongodb中的“尾部”日志文件中?
我试过:
设置概要级别 设置启动慢ms参数 使用-vv选项Mongod
/var/log/mongodb/mongodb.log一直显示当前活动连接的数量…
当前回答
因为它的谷歌第一个答案… 对于版本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/
其他回答
您可以记录所有查询:
$ 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)表示“记录所有操作”。
我最终通过像这样开始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 } )
尝试这个包来跟踪所有查询(没有oplog操作):https://www.npmjs.com/package/mongo-tail-queries
(免责声明:我写这个包正是为了这个需要)
我写了一个脚本,将打印出系统。当查询进来时,配置文件实时登录。如其他回答中所述,您需要首先启用日志记录。我需要这个,因为我正在使用Linux的Windows子系统,对于它,tail仍然不起作用。
https://github.com/dtruel/mongo-live-logger