Home › Databases

MongoServerError: bad auth : authentication failed — how to fix

MongoServerError: bad auth : authentication failed
MongoServerError: Authentication failed.

Quick fix: URL-encode the password in the URI (p@ss becomes p%40ss), use the Atlas Database Access user rather than your Atlas account login, and set authSource to the database where the user was created (often ?authSource=admin). Test with mongosh "mongodb+srv://user:ENCODED@cluster.example.net/".

What it means

The client reached the server (so network and IP access are fine) but SCRAM authentication failed. bad auth : authentication failed is the wording MongoDB Atlas returns; self-hosted servers say Authentication failed. The server deliberately doesn't say which part was wrong, so check each of the causes below.

Cause 1: special characters in the password aren't encoded

The connection string is a URI. Characters such as @ : / ? # [ ] % in the username or password break parsing, so the driver sends a truncated or different password.

mongodb+srv://app:p@ss#1@cluster0.abcd.mongodb.net/shop     # wrong
mongodb+srv://app:p%40ss%231@cluster0.abcd.mongodb.net/shop # right

Encode it in code instead of by hand:

const uri = `mongodb+srv://${encodeURIComponent(user)}:${encodeURIComponent(pass)}@cluster0.abcd.mongodb.net/shop`;
from urllib.parse import quote_plus
uri = f"mongodb+srv://{quote_plus(user)}:{quote_plus(password)}@cluster0.abcd.mongodb.net/shop"

The URL encoder does the same for a one-off test. Also check for a stray < > left over from Atlas's template <db_password>.

Cause 2: wrong user (Atlas account vs database user)

In Atlas, the email/password you log into the website with is not a database user. Create one under Security > Database Access (Add New Database User), give it a role such as readWrite on your database, and use that username and password in the URI. Changes take a short while to deploy; wait until Atlas shows them as active.

If you reset the password, update every place it's stored: .env, CI secrets, hosting provider environment variables. A stale environment variable is a very common cause after a reset.

Cause 3: authSource points at the wrong database

Users belong to the database they were created in. Atlas users and most admin-created users live in admin. If your URI ends in /shop, many drivers use shop as the auth database unless told otherwise:

mongodb://app:secret@db.example.com:27017/shop?authSource=admin

For a self-hosted server, check where the user is defined:

// in mongosh, as an admin
use admin
db.getUsers()
use shop
db.getUsers()

mongodb+srv:// URIs for Atlas get authSource=admin from the DNS TXT record by default, so with Atlas this is rarely the issue unless you override it.

Cause 4: the .env value isn't what you think

Quotes, trailing spaces or newlines end up in the password:

MONGODB_URI="mongodb+srv://app:secret@cluster0.abcd.mongodb.net/shop"   # OK with dotenv
MONGODB_URI=mongodb+srv://app:secret @cluster0...                       # broken

Log the parsed username and password length (not the password itself) to confirm what the app actually uses. A $ in the password can also be expanded by the shell or by Docker Compose variable interpolation; escape it ($$ in Compose files) or encode it.

Cause 5: auth enabled, but the user was never created

On a fresh self-hosted MongoDB with security.authorization: enabled, or a Docker container started with MONGO_INITDB_ROOT_USERNAME/MONGO_INITDB_ROOT_PASSWORD, the user only exists if init ran. The official image runs init only when the data directory is empty, so changing these variables later on an existing volume does nothing. Either create the user manually or, if the data is disposable, remove the volume and start again.

Test outside your app

mongosh "mongodb+srv://cluster0.abcd.mongodb.net/" --username app

mongosh prompts for the password, avoiding all URI-encoding issues. If this works, the problem is how your app builds or loads the connection string.

Last updated 2026-12-03.