这个问题既简单又基本。如何将所有查询记录在mongodb中的“尾部”日志文件中?
我试过:
设置概要级别 设置启动慢ms参数 使用-vv选项Mongod
/var/log/mongodb/mongodb.log一直显示当前活动连接的数量…
这个问题既简单又基本。如何将所有查询记录在mongodb中的“尾部”日志文件中?
我试过:
设置概要级别 设置启动慢ms参数 使用-vv选项Mongod
/var/log/mongodb/mongodb.log一直显示当前活动连接的数量…
当前回答
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 } )
其他回答
我写了一个脚本,将打印出系统。当查询进来时,配置文件实时登录。如其他回答中所述,您需要首先启用日志记录。我需要这个,因为我正在使用Linux的Windows子系统,对于它,tail仍然不起作用。
https://github.com/dtruel/mongo-live-logger
尝试这个包来跟踪所有查询(没有oplog操作):https://www.npmjs.com/package/mongo-tail-queries
(免责声明:我写这个包正是为了这个需要)
db.adminCommand( { getLog: "*" } )
Then
db.adminCommand( { getLog : "global" } )
分析器数据被写入DB中的集合,而不是文件。参见http://docs.mongodb.org/manual/tutorial/manage-the-database-profiler/
我建议使用10gen的MMS服务,并在那里提供开发分析器数据,在那里你可以在UI中过滤和排序。
db.setProfilingLevel(2,-1)
这个工作!它在mongod日志文件中记录了所有查询信息