Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not authorized for query on Mongodb shard from Mongoose driver

I am using mongoDB 2.6.7 and mongoose 3.8.23 driver for node.js. Database is sharded in 2 shards (each is separate replica) and everything looks ok when I enter mongo shell on any of instance, replica, mongos, config servers etc.

After that I have added admin user and user (named normaluser) for specific database I am connecting. I have added them on mongos instance. Users are :

[
{
    "_id" : "admin.adminuser",
    "user" : "adminuser",
    "db" : "admin",
    "roles" : [
        {
            "role" : "dbAdminAnyDatabase",
            "db" : "admin"
        },
        {
            "role" : "readWriteAnyDatabase",
            "db" : "admin"
        },
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        },
        {
            "role" : "clusterAdmin",
            "db" : "admin"
        }
    ]
}
]

and

[
{
    "_id" : "mydb.normaluser",
    "user" : "normaluser",
    "db" : "mydb",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "mydb"
        }
    ]
}

]

From my node.js application I am connecting to mongos instances (I have 2 mongos instances) like this :

mongoose.createConnection('mongodb://mongos-ip-1:27018,mongos-ip-2:27018/mydb', {user : 'normaluser', password : 'some-password', mongos : true, server : {poolSize : 5, keepAlive : true}}

I am successfully connected to database but when I try to query mydb.some-collection from application I get this error :

 Possibly unhandled MongoError: not authorized for query on mydb.some-collection

When I go to mongo shell on some of mongos instance and login as normaluser I can query anything and it is ok.

Does anyone knows why I cannot make any query although I have readWrite role to user and specify user in connection url of mongoose? Maybe I am missing something with mongoose?

I have even tried to put dbOwner role to normaluser but nothing changed

Thanks, Ivan

like image 350
Ivan Longin Avatar asked Feb 16 '15 12:02

Ivan Longin


People also ask

Does mongoose use MongoDB driver?

Mongoose relies on the MongoDB Node. js Driver to talk to MongoDB. You can refer to this table for up-to-date information as to which version of the MongoDB driver supports which version of MongoDB.

Should I use MongoDB driver or mongoose?

Mongoose is built untop of mongodb driver, the mongodb driver is more low level. Mongoose provides that easy abstraction to easily define a schema and query. But on the perfomance side Mongdb Driver is best. Save this answer.

Can I use MongoDB and mongoose at the same time?

Yes, we're actually using multiple drivers, in a production application. We need connections to multiple databases, and mongoose is only able to connect to one DB. So we use MongoDB for the connections to the secondary databases. It should be the same using MongoJS instead.

Do you have to use mongoose with MongoDB?

Mongoose is an object document modeling (ODM) layer that sits on top of Node's MongoDB driver. If your coming from SQL, it's similar to an ORM for a relational database. While it's not required to use Mongoose with the Mongo, here are four reasons why using Mongoose with MongoDB is generally a good idea.


1 Answers

Ivan, try this syntax:

mongoose.createConnection('mongodb://user:pass@localhost:port/db, ...', opts);
like image 123
Mi Ke Bu Avatar answered Nov 14 '22 23:11

Mi Ke Bu