Reputation: 3353
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
Upvotes: 8
Views: 1463
Reputation: 224
Ivan, try this syntax:
mongoose.createConnection('mongodb://user:pass@localhost:port/db, ...', opts);
Upvotes: 2