Mongoose connection authentication failed

mongoose connection timeout
mongoose close connection
mongoose multiple connections
mongoose connect to mongodb atlas
mongoerror: authentication failed
mongoose.connect callback
mongoose reconnect
mongoose.connect is not a function

With this help, I created a super user in the mongo shell: Create Superuser in mongo

user: "try1"
passw: "hello"

In mongo cmd, I have 3 databases: 'admin', 'myDatabase' and 'local'.

Now I try to use this authorized connection to the database called 'myDatabase'.


But this is the error I get:

name: 'MongoError', message: 'Authentication failed.', ok: 0, errmsg: 'Authentication failed.', code: 18, codeName: 'AuthenticationFailed' } Mongoose disconnected Mongoose disconnected through ${msg}

I had the same problem many hours ago, and after all I solve it. My code is:

    "auth": {
      "authSource": "admin"
    "user": "admin",
    "pass": "password"

Authentication Failed � Issue #5235 � Automattic/mongoose � GitHub, If I use mongoose and connect by mongoose.connect('mongodb://${user}:${pass} @${uri}/${db}') I have error MongoError: Authentication failed� Each connection, whether created with mongoose.connect or mongoose.createConnection are all backed by an internal configurable connection pool defaulting to a maximum size of 5. Adjust the pool size using your connection options:

Further to @kartGIS, I've added one more option to make the connection code perfect as possible.

mongoose.connect("mongodb://localhost:27017/databaseName", {
    "auth": { "authSource": "admin" },
    "user": "username",
    "pass": "password",
    "useMongoClient": true

Authentication failed � Issue #4587 � Automattic/mongoose, mongoose.connection.on("error", function(err) { console.log("Could not at TCP. onread (net.js:581:20) ok: 0, errmsg: 'Authentication failed. Mongoose 5.0.1: Authentication failed with authMechanism=SCRAM-SHA-1 in MongoDB docker container #6052 Closed BorntraegerMarc opened this issue Jan 26, 2018 · 10 comments

I have the same problem, and it solved by removing the 'authSource' param

/* Not working */
mongoose.connect("mongodb://localhost:27017/test", {
    "auth": { "authSource": "admin" },
    "user": "admin",
    "pass": "admin123",
    "useMongoClient": true

/* Working */
mongoose.connect("mongodb://localhost:27017/test", {
    "user": "admin",
    "pass": "admin123",
    "useMongoClient": true

Tested on Mongoose-v5.0.0.

Mongoose is not connected in MongoDB and Mongoose, ok: 0, errmsg: 'Authentication failed.', code: 18, codeName: 'AuthenticationFailed' } Mongoose disconnected Mongoose disconnected through ${msg}� A heartbeat is subject to serverSelectionTimeoutMS, so the MongoDB driver will retry failed heartbeats for up to 30 seconds by default. Mongoose only emits a 'disconnected' event after a heartbeat has failed, so you may want to decrease this setting to reduce the time between when your server goes down and when Mongoose emits 'disconnected'.

Working fine for me on Mongodb 4.2 and Mongoose 5.7.13


const Connect = async () => {

    let url = "mongodb://localhost:27017/test_db";

    try {

        let client = await Mongoose.connect( url, {
            poolSize: 10,
            authSource: "admin",
            user: "root",
            pass: "root123", 
            useCreateIndex: true,
            useNewUrlParser: true,
            useUnifiedTopology: true
        } );

        log( "Database is connected!" );
    } catch ( error ) {
        log( error.stack );
        process.exit( 1 );



  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

  dbPath: /var/lib/mongo
    enabled: true

  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

  port: 27017

   enableLocalhostAuthBypass: false

  authorization: enabled

Database User

use admin;
    user: "root",
    pwd: "root123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]

show users;
   _id": "admin.root",
   "userId": UUID( "5db3aafd-b1fd-4bea-925e-8a4bfb709f22" ),
   "user": "root",
   "db": "admin",
   "roles": [ {
        "role": "userAdminAnyDatabase",
        "db": "admin"
        "role": "readWriteAnyDatabase",
        "db": "admin"
   "mechanisms": [

Mongoose v5.9.27: Connecting to MongoDB, I'm getting this when trying to connect my Node app to Mongodb: MongoError: Authentication failed. I can access my databases in the Mongo shell with this� Authentication to the cluster failed¶ To connect to Atlas, you must authenticate with a MongoDB database user. To create a database user for your cluster, see Configure Database Users .

Mongoose connection authentication failed - node.js - html, mongoose connect to mongodb atlas mongoerror: bad auth authentication failed mongoose.connect not working mongonetworkerror: failed to connect to server� This post is Part 1 (of 2) on implementing secure username/password authentication for your Mongoose User models, originally posted on Jeremy Martin’s DevSmash Blog.In this first installment, we will discuss how to implement one-way encryption of user passwords with bcrypt, and how to subsequently use the encrypted password for login verification.

MongoError: Authentication failed with Mongoose.. Any suggestions , The "authentication failed" error message is often the result of one of five conditions. Password missing after copying connection string from the Atlas web page. I am trying to do a mongoose connection from Mongo Atlas. I have a cluster ayman and I am trying to connect it from Terminal(Ubuntu 18). It says that I have successfully connected to the shard, but it says authentication failed. I am giving credentials defined as SCRAM on Mongo Atlas. Mongo Security Page

MongoError: auth failed mongoose connection sting, I am passing username/password/authDB/etc., but it is not allowing me to connect and giving the following error: MongoError: Authentication� In MongoDB 3.0, it now supports multiple authentication mechanisms. MongoDB Challenge and Response (SCRAM-SHA-1) - default in 3.0; MongoDB Challenge and Response (MONGODB-CR) - previous default (< 3.0) If you started with a new 3.0 database with new users created, they would have been created using SCRAM-SHA-1.

  • Why is mongoose documentation so difficult!? I had tried many variations of these but just did not try this one. Finally you saved my day. Thanks @kartGIS
  • what mongoose version are you using?
  • can you explain what is this option "auth": { "authSource": "admin" },?
  • "auth": { "authSource": "admin" } means set authenticationDatabase to admin.
  • For me, just add user, pass and authSource: "admin" and works like a charm. Thanks a lot.