bad auth : Authentication failed 🍃 MongoDB

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

  1. Reset the DB user password in Atlas
  2. URL-encode the password (encodeURIComponent)
  3. Add ?authSource=admin when needed

Detailed fix by platform

JavaScript

  1. const uri = mongodb+srv://${encodeURIComponent(user)}:${encodeURIComponent(pass)}@cluster0.abcde.mongodb.net/app?retryWrites=true&w=majority;

How to diagnose

  1. Credentials — Test with mongosh
  2. Encoding — Special characters?
  3. 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:

🧠 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.