Skip to content
Latchkey

MongoDB "bad auth : Authentication failed" on Atlas in CI

Atlas returned "bad auth : Authentication failed", meaning the cluster was reachable but the database user or password in the URI is wrong. This is an Atlas database user, distinct from your Atlas login and from API keys.

What this error means

A mongodb+srv:// connection to Atlas fails with "bad auth : Authentication failed" after the SRV lookup and TLS handshake succeed.

node
MongoServerError: bad auth : Authentication failed.
    at Connection.onMessage (.../mongodb/lib/cmap/connection.js:229:30) {
  code: 8000, codeName: 'AtlasError'
}

Common causes

Wrong Atlas database user or password

The Database Access user in Atlas does not match the credentials in MONGODB_URI, so Atlas rejects the login with code 8000.

Special characters not URL-encoded

A password with @, :, /, or # breaks the URI parse, so the driver sends the wrong password.

How to fix it

Match a real Atlas Database Access user

  1. In Atlas, open Database Access and confirm the SCRAM username.
  2. Reset the password there and store it in a CI secret.
  3. URL-encode the password when composing the URI.
Terminal
# URL-encode a password before putting it in the URI
node -e "console.log(encodeURIComponent(process.env.MONGO_PASS))"

Test the credentials with mongosh

Verify the Atlas user authenticates before wiring it into the suite.

Terminal
mongosh "mongodb+srv://ciuser:PASS@cluster0.abcde.mongodb.net/appdb" --eval 'db.adminCommand({ping:1})'

How to prevent it

  • Keep Atlas Database Access users in sync with CI secrets.
  • URL-encode passwords with reserved characters.
  • Do not confuse the database user with the Atlas account or API keys.

Frequently asked questions

What causes ""bad auth : Authentication failed""?
The Database Access user in Atlas does not match the credentials in MONGODB_URI, so Atlas rejects the login with code 8000.
How do I fix "bad auth : Authentication failed"?
Match a real Atlas Database Access user

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card