1

I am querying mongo oplog in MongoLabs mongoDb. As Mongolabs doesn't allow one to create user for local, I created an oplog-reader in admin but it throws error showing that it is unauthorised:

Code snippet:

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);
});

I get :

{ [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 }

When I use 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 }

When I use allReaderURL: []

But I can log into mongo shell and query the oplog using oplog-reader user and switching the db to local. I can also tail oplog by mongo-oplog by using the oplog-user on admin.

How should I create the mongo URL/Query to get data from oplog.rs??

Shipra
  • 1,259
  • 2
  • 14
  • 26
  • I really can't understand why people are querying oplog directly. You are not supposed to manipulate oplog by any mean. It's for MongoDB server so please don't mess with it. – Saleem Mar 22 '16 at 00:17
  • 1
    @Saleem "manipulate?" There is nothing in here to attempt to "alter" the oplog contents at all. "Querying" the oplog is a **very** useful thing, as it allows you to trigger actions on updates/inserts etc. Frameworks like meteor use it for it's "reactivity" features in exactly such a way. As for the error in the question, it's a pretty straightforward case that the account in question lacks the permissions to query. Therefore it's simply a matter of setting a user with permissions. You can ask this on [dba.stackexchange.com](http://dba.stackexchange.com) since it's about setting user permission – Blakes Seven Mar 22 '16 at 00:39
  • @Saleem The question did not ask that. Therefore your comment is just chatter. – Blakes Seven Mar 22 '16 at 01:30
  • @Saleem I innocently need to find out the timestamp of last operation on mongo. – Shipra Mar 22 '16 at 01:32
  • Please don't crosspost: http://dba.stackexchange.com/questions/132933/unable-to-query-oplog-in-mongoose – Blakes Seven Mar 22 '16 at 02:35

1 Answers1

2

It was mentioned in the mlab docs, the connection string to be used should be:

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

Shipra
  • 1,259
  • 2
  • 14
  • 26