8

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

Ivan Longin
  • 3,207
  • 4
  • 33
  • 42
  • The code bits you've included look fine to me. Can you show us more code, maybe the code for the finds? Even better, can you write a minimal script that demonstrates connecting with the right credentials and then failing to auth for a find? – wdberkeley Feb 20 '15 at 08:08
  • I had the same problem ( and came here looking for answers.) We eventually discovered there was a piece of code that was trying to update the connection timeout, which requires admin access, yet it failed and gave "not authorized for query on mydb.mycollection." Not sure if you are in the same boat, but I would look for code that's trying to do administrative tasks. – David Hall Feb 25 '15 at 15:53
  • Did you try manually giving that collection permissions with `db.auth` ? If not open up mongo shell and try [this](http://docs.mongodb.org/manual/reference/method/db.auth/#db.auth) – Plentybinary May 24 '15 at 23:26
  • Any solutions for this problem ? I'm facing the same one. – Luc Jul 28 '15 at 12:54
  • I also had the same problem. My friend asked me to upgrade mongoose to version 3.8.27. After upgrading it worked for me. – Sarath Nair Aug 10 '15 at 12:39
  • could you show the node code, when trying to connect to MongoDB? – Gujarat Santana Aug 17 '15 at 14:03
  • HI ivan_zd, if your normaluser is created in the mydb database (rather than the default admin database) then does your connection need to specify the authentication database in your connection options? e.g. `auth: {authdb: 'mydb'}` – Vince Bowdren Dec 23 '15 at 14:55

1 Answers1

2

Ivan, try this syntax:

mongoose.createConnection('mongodb://user:pass@localhost:port/db, ...', opts);
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
Mi Ke Bu
  • 224
  • 2
  • 13