Mongoose connection authentication failed Mongoose connection authentication failed mongodb mongodb

Mongoose connection authentication failed


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

mongoose.createConnection(  "mongodb://localhost:27017/dbName",  {    "auth": {      "authSource": "admin"    },    "user": "admin",    "pass": "password"  });


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});


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.