mongoose: MongoError: command find|insert|update requires authentication

Do you want to request a feature or report a bug? bug

What is the current behavior? Under high load, lots of errors appear in the console like this: MongoError: command find requires authentication with error code 13. I believe it is when a connection gets closed/times out and then reconnects we start seeing errors like this. It only ever happens under very high load with >200 requests/second executing ~4 queries per request.

MongoError: command find requires authentication
  File "/worker/node_modules/mongodb-core/lib/cursor.js", line 247, col 25, in queryCallback
    return callback(new MongoError(result.documents[0]), null);
  File "/worker/node_modules/mongodb-core/lib/connection/pool.js", line 531, col 18, in null.<anonymous>
    return cb(err, result);
  File "internal/process/next_tick.js", line 176, col 11, in process._tickCallback

MongoDB server is v4.0 (seen also on v3.6) running as a Docker container. The logs show no unauthorised errors related to these queries, only these unrelated ones:

root@ubuntu-s-8vcpu-32gb-lon1-01:~# docker logs mongodb | grep authorized
2018-08-10T10:13:42.389+0000 I ACCESS   [conn188] Unauthorized: not authorized on admin to execute command { getLog: "startupWarnings", $db: "admin" }
2018-08-10T10:13:42.677+0000 I ACCESS   [conn188] Unauthorized: not authorized on admin to execute command { replSetGetStatus: 1.0, forShell: 1.0, $db: "admin" }
2018-08-10T10:13:43.623+0000 I ACCESS   [conn188] Unauthorized: not authorized on nanowire to execute command { find: "system.users", filter: {}, $db: "nanowire" }

If the current behavior is a bug, please provide the steps to reproduce.

Connection is initialised like this:

import mongoose from 'mongoose';

mongoose.Promise = global.Promise;

const connectToMongoDB = () => {
  const opts = {
    keepAlive: true,
    keepAliveInitialDelay: 300000,
    socketTimeoutMS: 30000,
    poolSize: 50,
    reconnectTries: Number.MAX_VALUE,
    reconnectInterval: 500,
    autoReconnect: true,
  };

  return mongoose.connect('mongodb://user:password@mongodb:27017/nanowire', opts);
};

export default connectToMongoDB;

After that mongoose is just used as normal in an express app, executing 3 finds, 1 insert and 2 updates on every request.

What is the expected behavior?

No authentication errors should occur as only one database is used with the same credentials, there is definitely no mixing of credentials to different databases. This also only occurs at high load.

Please mention your node.js, mongoose and MongoDB version.

Node 9.11.2 Mongoose 5.2.7 MongoDB 4.0.0

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 17

Most upvoted comments

I’m also getting following error when connecting to MongoDb Atlas from Firebase functions. Only happens when the servers been idle for sometime. Then doesn’t work till I deploy functions again.

{ MongoError: command update requires authentication
    at /user_code/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:581:63
    at authenticateStragglers (/user_code/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:504:16)
    at Connection.messageHandler (/user_code/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:540:5)
    at emitMessageHandler (/user_code/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:310:10)
    at TLSSocket.<anonymous> (/user_code/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:453:17)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at TLSSocket.Readable.push (_stream_readable.js:134:10)
    at TLSWrap.onread (net.js:559:20)
  operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1540468510 },
  ok: 0,
  errmsg: 'command update requires authentication',
  code: 13,
  codeName: 'Unauthorized',
  '$clusterTime': 
   { clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1540468510 },
     signature: { hash: [Object], keyId: [Object] } },
  name: 'MongoError' }

Interesting. I have never tried mongoose with firebase functions, will check it out