DEV Community

Cover image for How to fix the error 'MongoServerError: bad auth : Authentication failed'?
Shafia Chowdhury
Shafia Chowdhury

Posted on • Updated on

How to fix the error 'MongoServerError: bad auth : Authentication failed'?

Oftentimes, you might come across the error "bad auth: authentication failed." What does this error mean?

"Bad auth" means the authentication method is not proper due to providing wrong username or password; hence, it failed.

This blog will discuss the possible mistakes we make for which this error appears and the solutions we can try to fix the error.

1) using whitespaces

When you put your username and password, do not add any whitespace (extra space) before and after your username and password in the MongoDB string connection.


Image description

Image description

Correct way-1

Image description

const uri = mongodb+srv://;

Correct way-2
Image description

const uri = mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS};

2) username and password did not match

Check if the username and password in your MongoDB connection string matches with the username and password in your mongodb database access

Correct: Username and password matches

MongoDB connection string
Image description

Database Access
Image description

3) Forgot to save username and password

Make sure you have clicked the 'Add User' button after creating the username and password in your database access.

Image description

Image description

4) Using angle brackets

Do not put your username and password inside the angle brackets like the picture below:


Image description


Image description

5) Environment variables did not match

If you are using environment variables, make sure you have written them correctly in your MongoDB connection string

Did not match

Image description

Image description


Image description

Image description

6) Do not add any space when assigning your secret information to the environment variables


Image description


Image description

7) Don't forget to do the following before using the environment variables

Always check if you have installed dotenv in your package.json. If you haven't, then run this command to install dotenv

npm i dotenv

After installation, make sure you have imported dotenv


Take time and go through your code to see if you have made the mistakes discussed above when your authentication fails. I am sure one of these solutions will help you connect your MongoDB successfully. Happy coding!!!

Top comments (6)

dapone profile image
Afisunlu Dapo

Great! the > had me for hours.

blaze_26 profile image

Thanks soo much this was very helpful am so glad i saw this post thanks bro

masrafi404 profile image
Ali Hasan Masrafi

very helpful, thakn you!

mernsanowar profile image
Md Sanowar Hossain

very helpful

strubloid profile image

Why you never had the things to copy and paste? if we are having some issues with the string, could be nice to have in your article what is right by copying it, so we could compare what is working for you with our own configuration.

majorroots profile image

My issue was that my nodemon needed a hard restart after editing the env variables.