Mongoose: MongoError: Authentication failed

Created on 2 Oct 2016  ·  10Comments  ·  Source: Automattic/mongoose

I have mongodb in mongolab.
If I connect to it by command

mongo uri/$db -u user -p pass

it is ok.
If I use mongoose and connect by

mongoose.connect('mongodb://${user}:${pass}@${uri}/${db}')

I have error

MongoError: Authentication failed

I use mongoose 4.6.1

needs clarification

Most helpful comment

Hmm also what database is the user defined on? MongoDB users are scoped to dbs, even if they have cross-db privileges, so if the user you have isn't defined on db you'll need to put something like mongoose.connect('mongodb://${user}:${pass}@${uri}/${db}?authSource=admin') if the user is defined on the 'admin' db.

Also, if you're on mongodb 3.x, you might be using SCRAM-SHA-1 auth or not. Try both of the below:

  • mongoose.connect('mongodb://${user}:${pass}@${uri}/${db}?authMechanism=SCRAM-SHA-1')
  • mongoose.connect('mongodb://${user}:${pass}@${uri}/${db}?authMechanism=MONGODB-CR')

All 10 comments

Also, I have checked - bug is not reproduced in 4.4.20 version. It works correctly

Do your username and password include any non alphanumeric characters? Similar issues have surfaced in the past with handling characters like : in passwords

Also, what version of mongoose and mongodb?

Mongodb 'v3'
No, there is no non-alphanumeric symbols.

Hmm also what database is the user defined on? MongoDB users are scoped to dbs, even if they have cross-db privileges, so if the user you have isn't defined on db you'll need to put something like mongoose.connect('mongodb://${user}:${pass}@${uri}/${db}?authSource=admin') if the user is defined on the 'admin' db.

Also, if you're on mongodb 3.x, you might be using SCRAM-SHA-1 auth or not. Try both of the below:

  • mongoose.connect('mongodb://${user}:${pass}@${uri}/${db}?authMechanism=SCRAM-SHA-1')
  • mongoose.connect('mongodb://${user}:${pass}@${uri}/${db}?authMechanism=MONGODB-CR')

@vkarpov15, Yes, it helped! Thank you very much!

I have made connection to cloud.mongodb.com with node.js but I don't understand why and how I made 2 successful connections but other times connections were unsuccessfull .
image

In Console I see

the server/replset/mongos options are deprecated, all their options are supported at the top level of the options object [poolSize,ssl,sslValidate,sslCA,sslCert,sslKey,sslPass,sslCRL,autoReconnect,noDelay,keepAlive,connectTimeoutMS,fa
mily,socketTimeoutMS,reconnectTries,reconnectInterval,ha,haInterval,replicaSet,secondaryAcceptableLatencyMS,acceptableLatencyMS,connectWithNoPrimary,authSource,w,wtimeout,j,forceServerObjectId,serializeFunctions,ignoreUndefined,raw,bu
fferMaxEntries,readPreference,pkFactory,promiseLibrary,readConcern,maxStalenessSeconds,loggerLevel,logger,promoteValues,promoteBuffers,promoteLongs,domainsEnabled,keepAliveInitialDelay,checkServerIdentity,validateOptions,appname,auth]

(node:3268) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: authentication fail
(node:3268) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

i tryed it , but not work,

mongoose v4.5.3
mongodb v3.4.7

errMsg:
MongoError: Authentication failed. 14|gougouS | at Function.MongoError.create (/www/website/gougouServer/source/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11)

just add ?authSource=yourDB&w=1 to end of db url

mongoose.connect('mongodb://user:password@host/yourDB?authSource=yourDB&w=1')
this work for me . &w=1 is important

just add ?authSource=yourDB&w=1 to end of db url

mongoose.connect('mongodb://user:password@host/yourDB?authSource=yourDB&w=1')
this work for me . &w=1 is important

Thank you sir. :3 good answer

Was this page helpful?
0 / 5 - 0 ratings