MongoDB: MongoServerError: bad auth : Authentication failed (code 8000 / 18)
MongoDB (often Atlas) rejected the username/password in the connection string.
Seen on:
MongoDB
Meaning
Wrong password, a database user created in another project/cluster, special characters in the password not URL-encoded, or the wrong authSource cause it.
Common causes
- Wrong password or username
- Special characters (@ : / ?) in the password not URL-encoded
- User doesn’t exist on this cluster/project
- authSource wrong (user defined in admin)
⚡ Quick fix
- Reset the DB user password in Atlas
- URL-encode the password (encodeURIComponent)
- Add ?authSource=admin when needed
Detailed fix by platform
JavaScript
const uri =mongodb+srv://${encodeURIComponent(user)}:${encodeURIComponent(pass)}@cluster0.abcde.mongodb.net/app?retryWrites=true&w=majority;
How to diagnose
- Credentials — Test with mongosh
- Encoding — Special characters?
- authSource — Where the user lives
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Most viewed in MongoDB
Other ways to find it
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026