Fix "bad auth : authentication failed" in MongoDB Atlas
The driver reached your Atlas cluster, but the database user and password in the connection string were rejected. Check, in order: that you used a database user (not your Atlas login), that the password is correct and its special characters are percent-encoded, that the <db_password> placeholder and its angle brackets are gone, that authSource is admin, and that the user hasn't expired or been limited to other clusters.
What the error tells you
In Node.js the message reads:
MongoServerError: bad auth : authentication failedOther drivers phrase the same failure as a SCRAM error, for example unable to authenticate using mechanism "SCRAM-SHA-256": (AuthenticationFailed) Authentication failed.
The good news is in what it rules out. To get this far your client resolved the host, got through the IP access list and spoke to the cluster. Network fixes (adding IPs, opening ports) won't help. If instead you see a timeout, that is MongoServerSelectionError, a network problem.
The usual causes, in order
- Atlas login instead of a database user. The email and password you use to sign in to cloud.mongodb.com are an Atlas user. Your app needs a database user, created under Security, Database & Network Access, Database Users. They are separate accounts.
- The placeholder is still there. Atlas gives you
mongodb+srv://app_user:<db_password>@…. Replace<db_password>including the angle brackets. - Special characters in the password. In a connection string, characters such as
: / ? # [ ] @(Atlas's list also includes! $ & ' ( ) * , ; = %and the space) must be percent-encoded, or the driver splits the string in the wrong place.p@ssw0rd'9'!becomesp%40ssw0rd%279%27%21. Don't encode it when you type it into mongosh's password prompt. - Wrong password. If in doubt, set a new one: in Database Users, click Edit on the user and change its password. Update every app that uses the user.
- Wrong authSource. For password (SCRAM) users, Atlas's authentication database is
admin, which is also the drivers' default. If your string setsauthSourceto another database, change it toadmin. Also watch the path: when a string has a database such as/mydband noauthSourceoption, MongoDB's connection string rules use that database for authentication, so addingauthSource=adminis the safe choice. - The user expired. Temporary users are deleted when their time is up (6 hours, 1 day or 1 week in the UI). The app that worked this morning now gets bad auth.
- The user can't use this cluster. A user created with Restrict Access to Specific Clusters only works on the clusters you ticked.
Encode the password in code, not by hand
Build the string from parts so the encoding is always right. In Node.js:
const user = encodeURIComponent(process.env.DB_USER);
const pass = encodeURIComponent(process.env.DB_PASSWORD);
const uri = `mongodb+srv://${user}:${pass}@cluster0.abcd1.mongodb.net/?retryWrites=true&w=majority`;In Python, urllib.parse.quote_plus() does the same job. Most drivers also accept the username and password as separate client options, which avoids URL encoding altogether.
When you change a password in Atlas and Atlas warns This password contains special characters which will be URL-encoded., that is a reminder for your connection strings, not an error.
Test the user on its own
Take the app out of the picture and log in with mongosh, which prompts for the password so no encoding is involved:
mongosh "mongodb+srv://cluster0.abcd1.mongodb.net/" --username app_user- mongosh works, the app fails: the app's string or environment variable is wrong (often encoding, or a stale secret in the deploy).
- mongosh fails too: the user, password, expiry or cluster restriction is wrong. Reset the password or create a fresh user.
For quick checks from a laptop, a temporary database user is safer than reusing the app's credentials: give it the role it needs and let Atlas delete it after a few hours.
Questions
Is bad auth an IP access list problem?
No. An IP that isn't on the access list never reaches the authentication step; it times out with MongoServerSelectionError. bad auth means the network is fine and the credentials were rejected.
Can I log in to my cluster with my Atlas email and password?
Not with SCRAM password authentication. Clusters use database users, which you create separately in Atlas. Atlas users and database users are different accounts.
Which characters must be encoded in a MongoDB password?
In a connection string, at least : / ? # [ ] @, and Atlas also lists ! $ & ' ( ) * , ; = %, plus the space. Use your language's URL-encoding function rather than doing it by hand.
Sources
- Connection Troubleshooting (Node.js driver)www.mongodb.com
- Troubleshoot Connection Issues (Atlas)www.mongodb.com
- Configure Database Userswww.mongodb.com
- Connection String Formatswww.mongodb.com
Shardherd brings your Atlas clusters to iPhone.