这个问题既简单又基本。如何将所有查询记录在mongodb中的“尾部”日志文件中?
我试过:
设置概要级别 设置启动慢ms参数 使用-vv选项Mongod
/var/log/mongodb/mongodb.log一直显示当前活动连接的数量…
这个问题既简单又基本。如何将所有查询记录在mongodb中的“尾部”日志文件中?
我试过:
设置概要级别 设置启动慢ms参数 使用-vv选项Mongod
/var/log/mongodb/mongodb.log一直显示当前活动连接的数量…
当前回答
我认为oplog虽然不够优雅,但可以部分地用于这个目的:它记录所有的写操作——但不记录读操作……
如果我没猜错的话,你必须马上启用复制。信息来自这个问题的答案:如何监听MongoDB集合的更改?
其他回答
我建议你看看mongosniff。这个工具可以做任何你想做的事情,甚至更多。特别是它可以帮助诊断大规模mongo系统的问题,以及查询是如何路由的以及它们来自哪里,因为它是通过监听您的网络接口进行所有与mongo相关的通信来工作的。
http://docs.mongodb.org/v2.2/reference/mongosniff/
您可以记录所有查询:
$ 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)表示“记录所有操作”。
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/
很久以前有人问过这个问题,但这仍然可以帮助到一些人:
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之后添加的另一个功能。