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.
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
- In Atlas, open Database Access and confirm the SCRAM username.
- Reset the password there and store it in a CI secret.
- URL-encode the password when composing the URI.
# 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.
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.