question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MongoError: Authentication failed

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10

github_iconTop GitHub Comments

83reactions
vkarpov15commented, Oct 7, 2016

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')
12reactions
alikarimiicommented, Apr 21, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

MongoError: Authentication failed - MongoDB
Authentication failed means wrong user name, wrong password or wrong authentication database. system (system) closed September 14, 2022, ...
Read more >
Unable to connect to mongolab, Getting MongoError: auth failed
When I am trying to connect to the database using the below statement. var mongoose = require('mongoose'); mongoose.connect('mongodb://mk:12345 ...
Read more >
MongoError: Authentication failed - Hackolade
MongoError : Authentication failed · 1) Open Compass, select the connection, and click on the Edit button: Compass connection copy 1 ​ ·...
Read more >
MongoDB Error Auth failed - Method to set it up correctly
Securing data is always a primary concern in database management. Usually, MongoDB error auth failed, occurs when using an improper ...
Read more >
MongoError: Authentication Failed - The freeCodeCamp Forum
What's happening: I have this error: MongoError: Authentication failed. at MessageStream.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found