Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoError: Authentication failed #4587

Closed
yankouskia opened this issue Oct 2, 2016 · 10 comments
Closed

MongoError: Authentication failed #4587

yankouskia opened this issue Oct 2, 2016 · 10 comments
Labels
needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity

Comments

@yankouskia
Copy link

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
Copy link
Author

yankouskia commented Oct 2, 2016

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

@vkarpov15
Copy link
Collaborator

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

@vkarpov15
Copy link
Collaborator

Also, what version of mongoose and mongodb?

@vkarpov15 vkarpov15 added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Oct 3, 2016
@yankouskia
Copy link
Author

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

@vkarpov15
Copy link
Collaborator

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')

@yankouskia
Copy link
Author

yankouskia commented Oct 7, 2016

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

@Oleksii-Romanchenko
Copy link

Oleksii-Romanchenko commented Oct 19, 2017

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
Copy link

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
Copy link

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
Copy link

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

@Automattic Automattic locked as resolved and limited conversation to collaborators Jan 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity
Projects
None yet
Development

No branches or pull requests

6 participants