无法在mongoose中查询Oplog

时间:2016-03-21 22:27:07

标签: node.js mongodb mongoose mongodb-oplog

我在MongoLabs mongoDb中查询mongo oplog。由于Mongolabs不允许为本地创建用户,因此我在管理员中创建了一个oplog-reader但是它会抛出错误,表明它是未经授权的:

代码段:

var oplogURL = 'mongodb://<oplog-user>:<password>@node1,node2:port/admin?replicaSet=rs-<relpset>';
var allLocalURL = 'mongodb://<oplog-user>:<password>@node1,node2:port/local?replicaSet=rs-<relpset>'; //user on admin  with "readWriteAnyDatabase" permisson, trying to use local db
var allReaderURL='mongodb://<user>:<password>@node1,node2:port/admin?replicaSet=rs-<relpset>';   //user on admin  with "readWriteAnyDatabase" permisson

var db = mongoose.createConnection(oplogURL,{replset: { poolSize: 50 }}, function (err, res){
    if (err) {console.log("Oops!"); console.log(err);}

});

var opschema = new mongoose.Schema({v:"Number", op:"String", ns:"String"}, { strict: false});
var oplogModel = db.model('oplogModel', opschema, 'oplog.rs');

oplogModel.find().exec(function(err, res){
//oplogModel.find().sort('-ts').limit(1).select('ts').exec(function(err, res){
    if (err) {console.log("Oopps!"); console.log(err);}
    console.log (res);
});

我明白了:

{ [MongoError: not authorized for query on admin.oplog.rs]
    name: 'MongoError',
        message: 'not authorized for query on admin.oplog.rs',
    '$err': 'not authorized for query on admin.oplog.rs',
    code: 13 }

当我使用allLocalURL时:

{ [MongoError: not authorized for query on local.oplog.rs]
    name: 'MongoError',
        message: 'not authorized for query on local.oplog.rs',
    '$err': 'not authorized for query on local.oplog.rs',
    code: 13 }

当我使用allReaderURL时: []

但我可以登录mongo shell并使用oplog-reader用户查询oplog并将db切换到local。 我还可以通过在管理员上使用oplog-user来通过mongo-oplog来关闭oplog。

我应该如何创建mongo URL / Query以从oplog.rs获取数据?

1 个答案:

答案 0 :(得分:1)

mlab docs中提到过,要使用的连接字符串应为:

mongodb://<dbuser>:<dbpassword>@ds012345-a0.mlab.com:56789,ds012345-a1.mlab.com:56790/local?replicaSet=rs-ds012345&authSource=admin