Very new to MongoDB and Node.js. I'm trying to connect to my mongoDB server via the connection string given to me by mongo:
"mongodb+srv://david:password@cluster0-re3gq.mongodb.net/test?retryWrites=true"
In my code I am calling the connection through mongoose like this (obviously putting in my password):
const mongoose = require('mongoose');
const db = 'mongodb+srv://david:<password>@cluster0-re3gq.mongodb.net/test?retryWrites=true'
mongoose
.connect(db, {
useNewUrlParser: true,
useCreateIndex: true
})
.then(() => console.log('MongoDB connected...'))
.catch(err => console.log(err));
When I run the code I am getting the following error
"MongoError: bad auth Authentication failed."
Any ideas of what that could mean? Thanks!
Checklist to follow:
1) Make sure you're using the correct password (the DB user password and not the Mongo account).
2) When entering your password, make sure all special characters are URL encoded (for example:
p@ssword
should bep%40ssword
).3) If you don't remember your password of your DB user - go to Database Access (if you're using Mongo Atlas) -> select your DB user -> edit -> create a new password -> don't forget update to click on 'Update User'.
(!) Security warning: Do not write the password in plain text inside your code - Follow the suggestions given here.
Works great!!
In my case, my password was wrong, to diagnostic the error, I have been follow the next steps:
I have to try connection by command line:
Whit this command: mongo "mongodb+srv://cluster0-j8ods.mongodb.net/test" --username :
The response was again: 2020-04-26T11:48:27.641-0500 E QUERY [js] Error: bad auth Authentication failed. :
then I'm change the password for my user, in my case, root user. and thats it, I'm authorized
Are you writing your password in the place of
<password>
? If your aren't, a good practice is to create a environment variable on your operating system and call it using process.env.[your variable]. Ex:Better yet, you can also put your whole url connection string inside a env variable:
This Happens because entered password is wrong.Because you are entering login password not cluster one.
Solution ===> step 1. click Database Access From left Side Navigation of mongodb atlas page.
step 2. select your username and and click to edit from right side.
step 3. click to change password.
step 4. click update user.
During change password keep password only alphabetical because special characters need encoding.
that's all now you can connect.
Happy Coding.
The same problem i faced with mongoDB password authentication failed.
"Error: bad auth Authentication failed."
As per Pawan's suggestion given above i replaced my login password in MONGO_URI link with database password and it works. be sure to check that one also.
If you not generated the generate new one or if created earlier then replace with new one.