Error Message: MongoError: bad auth Authentication

2020-06-30 07:01发布

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!

9条回答
再贱就再见
2楼-- · 2020-06-30 07:14

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 be p%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.

查看更多
不美不萌又怎样
3楼-- · 2020-06-30 07:16

Works great!!

查看更多
beautiful°
4楼-- · 2020-06-30 07:19

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: enter image description here

Whit this command: mongo "mongodb+srv://cluster0-j8ods.mongodb.net/test" --username :

enter image description here

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

查看更多
老娘就宠你
5楼-- · 2020-06-30 07:23

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:

const password = process.env.YOURPASSWORDVARIABLE
const db = 'mongodb+srv://david:'+password+'@cluster0-re3gq.mongodb.net/test?retryWrites=true'

Better yet, you can also put your whole url connection string inside a env variable:

查看更多
倾城 Initia
6楼-- · 2020-06-30 07:27

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. See The Image For Your Better Understanding

that's all now you can connect.

Happy Coding.

查看更多
做个烂人
7楼-- · 2020-06-30 07:28

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.

查看更多
登录 后发表回答