Mongoose: MongoError: not authorized for query on ***

Created on 16 Aug 2016  路  10Comments  路  Source: Automattic/mongoose

Hi,

The last week we have been getting a lot of errors about not being able to execute commands on our production replica set.

The error occurs on several collections, but the users collection is the most common one. The error output is like this:

MongoError: not authorized for query on addb.users
    at Function.MongoError.create (/home/backend/main/node_modules/mongoose/node_modules/mongodb-core/lib/error.js:31:11)
    at queryCallback (/home/backend/main/node_modules/mongoose/node_modules/mongodb-core/lib/cursor.js:182:34)
    at Callbacks.emit (/home/backend/main/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:119:3)
    at null.messageHandler (/home/backend/main/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:358:23)
    at Socket.<anonymous> (/home/backend/main/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:292:22)
    at emitOne (events.js:90:13)
    at Socket.emit (events.js:182:7)
    at readableAddChunk (_stream_readable.js:153:18)
    at Socket.Readable.push (_stream_readable.js:111:10)
    at TCP.onread (net.js:534:20)

I believe this issue should have been solved as of 4.5.5, but it may have re-emerged. I'm not able to get rid of it, and it now happens for me with mongoose versions 4.4.14, 4.5.5, 4.5.8, and 4.5.9.

It could be noted that it usually only occur at one of the production servers at the time, and that the app are working, except for when these errors occur.

Are anyone else experiencing similar issues?

help

Most helpful comment

Hey,

We are still encountering this issue

MongoError :: not authorized on databaseName to execute command { find: "collection"...

Stack
        "node_modules/mongodb-core/lib/error.js:31:11",
        "node_modules/mongodb-core/lib/cursor.js:200:36",
        "node_modules/mongodb-core/lib/topologies/server.js:119:3",
        "node_modules/mongodb-core/lib/topologies/server.js:358:23",
        "node_modules/mongodb-core/lib/connection/connection.js:292:22",
        "events.js:96:13",
        "events.js:188:7",
        "_stream_readable.js:172:18",
        "_stream_readable.js:130:10",
        "net.js:542:20"

mongoose version: 4.5.10
mongodb version (default with mongoose): 2.1.18
mongodb-core version (default with mongodb): 1.3.18

I checked these are the only packages installed.

It seems to be fixed in 2.2.0
https://jira.mongodb.org/browse/NODE-722?jql=project%20%3D%20NODE%20AND%20text%20~%20%22not%20authorized%22
I guess I should wait for you guys to adopt the new mongodb version? Any plans or roadmap for that?

All 10 comments

@axelfran try doing a full reinstall of the mongodb driver, this issue was because mongoose updated to mongodb driver 2.1.21, which had this issue. You might still have an old version of the mongodb driver hanging around

@vkarpov15 We are using Docker containers, so everything should be reinstalled for every build. I tried to terminate and rebuild the whole ting from scratch now (with newest version of both Mongoose and MongoDB), but unfortunately the issue is still there.

Anything I can provide you with that could give you some clues?

We were just recently struggling with a similar issue trying to access mongodb with authorization. We've resolved this by upgrading the mongoose mongodb driver to version 2.2.4 (under the node_modules/mongoose directory, issue npm install [email protected] --save).
Kudos to the mlab.com support that resolved this issue!

@ohzeekay Have tested this solution for a bit now, and it's looking good so far! Hoping that this might do the trick! Btw, we also host our database with mLab.

@axelfran glad to hear that! :)
To be more specific about the issue - the older mongodb driver seems to try to access the database (issue a query) before the authorization process is complete. Once understood, this was very clear from the mongodb logs. The driver asks for authorization, then issues the query, before the authorization response is received. Definitely a driver issue that seems to have been resolved with version 2.2.4.

Highly recommend that mongoose will be upgraded to a newer mongodb driver as it makes no sense for me to hold an internal version of mongoose.js just because of that...

Hi,

An update from me on this issue. Our backend seems to be stable now, which is great. We are currently using regular Mongoose 4.5.9, and without changing the mongodb version from the standard. So it seems to be working correctly for us, even without @ohzeekay's tip.

I believe you were right when saying that we might have another version of the mongodb driver laying around, @vkarpov15. We had both mongodb itself (were targeting the newest version) and gridfs-stream in our project. Both of these were removed, in addition to moving from mongoose.createConnection() to mongoose.connect, as we were only using one database.

Many thanks for the help along the way to both of you!

Hey,

We are still encountering this issue

MongoError :: not authorized on databaseName to execute command { find: "collection"...

Stack
        "node_modules/mongodb-core/lib/error.js:31:11",
        "node_modules/mongodb-core/lib/cursor.js:200:36",
        "node_modules/mongodb-core/lib/topologies/server.js:119:3",
        "node_modules/mongodb-core/lib/topologies/server.js:358:23",
        "node_modules/mongodb-core/lib/connection/connection.js:292:22",
        "events.js:96:13",
        "events.js:188:7",
        "_stream_readable.js:172:18",
        "_stream_readable.js:130:10",
        "net.js:542:20"

mongoose version: 4.5.10
mongodb version (default with mongoose): 2.1.18
mongodb-core version (default with mongodb): 1.3.18

I checked these are the only packages installed.

It seems to be fixed in 2.2.0
https://jira.mongodb.org/browse/NODE-722?jql=project%20%3D%20NODE%20AND%20text%20~%20%22not%20authorized%22
I guess I should wait for you guys to adopt the new mongodb version? Any plans or roadmap for that?

4.6.0 was just released and uses mongodb driver 2.2.9, so give that a shot and let me know if that fixes it. Sorry for the delay, we found a few minor issues with the mongodb driver 2.2.4-2.2.7 that prevented us from upgrading effectively.

It seems to have disappeared after we stopped using a connection pool. I will update to 4.6.0 asap and let you know.

Thanks

@unDemian Thanks for the solution. We were having similar problems with 4.4.12 on our system. We have now moved to 4.5.10 and it all seems to work fine.

What's funny is that i restarted my pm2 application and things just fell apart. I didn't do a _npm install_ or anything but it still did fell apart. I was getting error on querying to the entire database.

Here is my updated stack -

mongoose - 4.5.10
mongodb-core - 1.3.21
mongodb - 2.1.21

thanks @vkarpov15 @axelfran

Was this page helpful?
0 / 5 - 0 ratings