Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.92k stars 3.84k forks source link

MongoError: Authentication failed #4587

Closed yankouskia closed 8 years ago

yankouskia commented 8 years ago

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

yankouskia commented 8 years ago

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

vkarpov15 commented 8 years ago

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

vkarpov15 commented 8 years ago

Also, what version of mongoose and mongodb?

yankouskia commented 8 years ago

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

vkarpov15 commented 8 years ago

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:

yankouskia commented 8 years ago

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

Oleksii-Romanchenko commented 7 years ago

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.

xiaoxiekeke commented 6 years ago

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)

alikarimii commented 6 years ago

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

phamthephuc commented 5 years ago

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