MongoDB 记录所有查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15204341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB logging all queries
提问by Jo?o Rocha da Silva
The question is as basic as it is simple... How do you log all queries in a "tail"able log file in mongodb?
问题既简单又基本......你如何在mongodb的“尾部”日志文件中记录所有查询?
I have tried:
我试过了:
- setting the profiling level
- setting the slow ms parameter starting
- mongod with the -vv option
- 设置分析级别
- 设置slow ms参数开始
- 带有 -vv 选项的 mongod
The /var/log/mongodb/mongodb.log keeps showing just the current number of active connections...
/var/log/mongodb/mongodb.log 一直显示当前的活动连接数...
采纳答案by Jo?o Rocha da Silva
I ended up solving this by starting mongod like this (hammered and ugly, yeah... but works for development environment):
我最终通过像这样启动 mongod 来解决这个问题(锤击和丑陋,是的......但适用于开发环境):
mongod --profile=1 --slowms=1 &
This enables profiling and sets the threshold for "slow queries" as 1ms, causing all queries to be logged as "slow queries" to the file:
这将启用分析并将“慢查询”的阈值设置为 1 毫秒,从而导致所有查询都作为“慢查询”记录到文件中:
/var/log/mongodb/mongodb.log
Now I get continuous log outputs using the command:
现在我使用以下命令获得连续的日志输出:
tail -f /var/log/mongodb/mongodb.log
An example 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
回答by Kristóf Dombi
You can log all queries:
您可以记录所有查询:
$ 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()
Source: http://docs.mongodb.org/manual/reference/method/db.setProfilingLevel/
来源:http: //docs.mongodb.org/manual/reference/method/db.setProfilingLevel/
db.setProfilingLevel(2)
means "log all operations".
db.setProfilingLevel(2)
表示“记录所有操作”。
回答by barak
Because its google first answer ...
For version 3
因为它的谷歌第一个答案......
对于版本 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/
http://docs.mongodb.org/manual/reference/method/db.setLogLevel/
回答by xameeramir
MongoDB
has a sophisticated feature of profiling. The logging happens in system.profile
collection. The logs can be seen from:
MongoDB
具有复杂的分析功能。日志记录发生在system.profile
集合中。日志可以从以下位置看到:
db.system.profile.find()
There are 3 logging levels (source):
有 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.
- 级别 0- 分析器关闭,不收集任何数据。mongod 总是将比 slowOpThresholdMs 阈值更长的操作写入其日志。这是默认的分析器级别。
- 级别 1- 仅为慢速操作收集分析数据。默认情况下,慢速操作是那些慢于 100 毫秒的操作。您可以使用 slowOpThresholdMs 运行时选项或 setParameter 命令修改“慢”操作的阈值。有关详细信息,请参阅指定慢速操作的阈值部分。
- 级别 2- 收集所有数据库操作的分析数据。
To see what profiling level the database is running in, use
要查看数据库运行的分析级别,请使用
db.getProfilingLevel()
and to see the status
并查看状态
db.getProfilingStatus()
To change the profiling status, use the command
要更改分析状态,请使用命令
db.setProfilingLevel(level, milliseconds)
Where level
refers to the profiling level and milliseconds
is the ms of which duration the queries needs to be logged. To turn off the logging, use
哪里level
是指分析级别,milliseconds
是查询需要记录的持续时间的毫秒。要关闭日志记录,请使用
db.setProfilingLevel(0)
The query to look in the system profile collection for all queries that took longer than one second, ordered by timestamp descending will be
在系统配置文件集合中查找耗时超过一秒的所有查询的查询,按时间戳降序排列将是
db.system.profile.find( { millis : { $gt:1000 } } ).sort( { ts : -1 } )
回答by Mariano Ruiz
I made a command line tool to activate the profiler activity and see the logs in a "tail"ableway: "mongotail".
我制作了一个命令行工具来激活分析器活动并以“尾部”方式查看日志:“mongotail”。
But the more interesting feature (also like tail
) is to see the changes in "real time"with the -f
option, and occasionally filter the result with grep
to find a particular operation.
但更有趣的功能(也像tail
)是使用选项查看“实时”的变化-f
,并偶尔过滤结果grep
以查找特定操作。
See documentation and installation instructions in: https://github.com/mrsarm/mongotail
查看文档和安装说明:https: //github.com/mrsarm/mongotail
回答by Faiz Mohamed Haneef
Once profiling level is set using db.setProfilingLevel(2)
.
一旦使用db.setProfilingLevel(2)
.
The below command will print the last executed query.
You may change the limit(5) as well to see less/more queries.
$nin - will filter out profile and indexes queries
Also, use the query projection {'query':1} for only viewing query field
下面的命令将打印上次执行的查询。
您也可以更改 limit(5) 以查看更少/更多查询。
$nin - 将过滤掉配置文件和索引查询
此外,使用查询投影 {'query':1} 仅查看查询字段
db.system.profile.find(
{
ns: {
$nin : ['meteor.system.profile','meteor.system.indexes']
}
}
).limit(5).sort( { ts : -1 } ).pretty()
Logs with only query projection
仅包含查询投影的日志
db.system.profile.find(
{
ns: {
$nin : ['meteor.system.profile','meteor.system.indexes']
}
},
{'query':1}
).limit(5).sort( { ts : -1 } ).pretty()
回答by DariusNica
if you want the queries to be logged to mongodb log file, you have to set both the log level and the profiling, like for example:
如果您希望将查询记录到 mongodb 日志文件中,则必须同时设置日志级别和分析,例如:
db.setLogLevel(1)
db.setProfilingLevel(2)
(see https://docs.mongodb.com/manual/reference/method/db.setLogLevel)
(参见https://docs.mongodb.com/manual/reference/method/db.setLogLevel)
Setting only the profiling would not have the queries logged to file, so you can only get it from
仅设置分析不会将查询记录到文件中,因此您只能从
db.system.profile.find().pretty()
回答by Hans N. Hjort
The profiler data is written to a collection in your DB, not to file. See http://docs.mongodb.org/manual/tutorial/manage-the-database-profiler/
探查器数据写入数据库中的集合,而不是文件。请参阅http://docs.mongodb.org/manual/tutorial/manage-the-database-profiler/
I would recommend using 10gen's MMSservice, and feed development profiler data there, where you can filter and sort it in the UI.
我建议使用 10gen 的MMS服务,并在那里提供开发分析器数据,您可以在其中在 UI 中对其进行过滤和排序。
回答by ppeterka
I think that while not elegant, the oplogcouldbe partially used for this purpose: it logs all the writes - but not the reads...
我认为虽然不优雅,但oplog可以部分用于此目的:它记录所有写入 - 但不记录读取......
You have to enable replicatoon, if I'm right. The information is from this answerfrom this question: How to listen for changes to a MongoDB collection?
如果我是对的,你必须启用复制。信息来自以下问题的答案:How to listen for changes to a MongoDB collection?
回答by Shnkc
Setting profilinglevel to 2 is another option to log all queries.
将 profilinglevel 设置为 2 是记录所有查询的另一个选项。