这个问题既简单又基本。如何将所有查询记录在mongodb中的“尾部”日志文件中?
我试过:
设置概要级别 设置启动慢ms参数 使用-vv选项Mongod
/var/log/mongodb/mongodb.log一直显示当前活动连接的数量…
这个问题既简单又基本。如何将所有查询记录在mongodb中的“尾部”日志文件中?
我试过:
设置概要级别 设置启动慢ms参数 使用-vv选项Mongod
/var/log/mongodb/mongodb.log一直显示当前活动连接的数量…
当前回答
我最终通过像这样开始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.setLogLevel(1)
db.setProfilingLevel(2)
(参见https://docs.mongodb.com/manual/reference/method/db.setLogLevel)
只设置概要不会将查询记录到文件中,因此您只能从
db.system.profile.find().pretty()
分析器数据被写入DB中的集合,而不是文件。参见http://docs.mongodb.org/manual/tutorial/manage-the-database-profiler/
我建议使用10gen的MMS服务,并在那里提供开发分析器数据,在那里你可以在UI中过滤和排序。
很久以前有人问过这个问题,但这仍然可以帮助到一些人:
MongoDB profiler将所有查询记录在capped collection system.profile中。看这个:数据库分析器
Start mongod instance with --profile=2 option that enables logging all queries OR if mongod instances is already running, from mongoshell, run db.setProfilingLevel(2) after selecting database. (it can be verified by db.getProfilingLevel(), which should return 2) After this, I have created a script which utilises mongodb's tailable cursor to tail this system.profile collection and write the entries in a file. To view the logs I just need to tail it:tail -f ../logs/mongologs.txt. This script can be started in background and it will log all the operation on the db in the file.
我为系统编写的可尾游标代码。配置文件收集在nodejs中;它记录MyDb的每个集合中发生的所有操作和查询:
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const fs = require('fs');
const file = '../logs/mongologs'
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'MyDb';
//Mongodb connection
MongoClient.connect(url, function (err, client) {
assert.equal(null, err);
const db = client.db(dbName);
listen(db, {})
});
function listen(db, conditions) {
var filter = { ns: { $ne: 'MyDb.system.profile' } }; //filter for query
//e.g. if we need to log only insert queries, use {op:'insert'}
//e.g. if we need to log operation on only 'MyCollection' collection, use {ns: 'MyDb.MyCollection'}
//we can give a lot of filters, print and check the 'document' variable below
// set MongoDB cursor options
var cursorOptions = {
tailable: true,
awaitdata: true,
numberOfRetries: -1
};
// create stream and listen
var stream = db.collection('system.profile').find(filter, cursorOptions).stream();
// call the callback
stream.on('data', function (document) {
//this will run on every operation/query done on our database
//print 'document' to check the keys based on which we can filter
//delete data which we dont need in our log file
delete document.execStats;
delete document.keysExamined;
//-----
//-----
//append the log generated in our log file which can be tailed from command line
fs.appendFile(file, JSON.stringify(document) + '\n', function (err) {
if (err) (console.log('err'))
})
});
}
对于python中使用pymongo的可尾游标,请参考以下代码,该代码过滤MyCollection并且只进行插入操作:
import pymongo
import time
client = pymongo.MongoClient()
oplog = client.MyDb.system.profile
first = oplog.find().sort('$natural', pymongo.ASCENDING).limit(-1).next()
ts = first['ts']
while True:
cursor = oplog.find({'ts': {'$gt': ts}, 'ns': 'MyDb.MyCollection', 'op': 'insert'},
cursor_type=pymongo.CursorType.TAILABLE_AWAIT)
while cursor.alive:
for doc in cursor:
ts = doc['ts']
print(doc)
print('\n')
time.sleep(1)
注意:可尾随游标仅适用于有上限的集合。它不能直接用于记录集合上的操作,而是使用filter: 'ns': 'MyDb。MyCollection”
注意:我明白上面的nodejs和python代码可能对一些人没有太大的帮助。我只是提供了代码供参考
使用此链接查找语言/驱动程序选择Mongodb Drivers中的可尾标文档
我在这个logrotate之后添加的另一个功能。
我认为oplog虽然不够优雅,但可以部分地用于这个目的:它记录所有的写操作——但不记录读操作……
如果我没猜错的话,你必须马上启用复制。信息来自这个问题的答案:如何监听MongoDB集合的更改?
一旦使用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()