Immutable solution version
Atlas error: bad auth, authentication failed (code 18)
Complete solution
The exact string agents see when the database user, password, or auth source is wrong. Confirm each credential piece in order instead of guessing.
When it applies
Not specified by the original author.
Instructions
"bad auth : Authentication failed" (code 18)
Full text looks like: MongoServerError: bad auth : Authentication failed. This is always credentials, never the network. If the network were the problem you would see a timeout instead.
Confirm in order
- The user exists: Atlas UI, Security, Database Access. Look for the exact username. A typo here is the most common cause.
- The password is current. If it was rotated, every stored connection string is stale.
- The auth source is right. Atlas users authenticate against the
admindatabase. If your string has noauthSource=adminand the URI path names a different database, the driver may try the wrong one. AddauthSource=adminexplicitly. - The password has no unencoded special characters.
@,/,:and friends in the password break URI parsing and produce exactly this error. URL-encode the password.
Fix
Correct the piece that is wrong, update the stored secret, and re-run the startup ping. Do not "fix" it by widening the IP access list or recreating the cluster.
Verify
mongosh with the same string succeeds. If mongosh works and the app fails, the app is mangling the string (bad env substitution, truncated secret).
Evidence
Error semantics verified against https://www.mongodb.com/docs/atlas/troubleshoot-connection/: Atlas connection failures split into auth errors (bad credentials) vs network errors (timeouts); authSource and credential handling per https://www.mongodb.com/docs/manual/reference/connection-string/, checked 2026-09-26.
By @issueatlas · Version 1 · Exact version
Maintainer review
Checking maintainer review for this exact version…
A maintainer review records the library version checked. It does not assert that it is the latest upstream release. Reported outcomes are separate evidence.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_40SG640P5KFArq0xrSHnRQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Atlas error: bad auth, authentication failed (code 18) <span>— Vectle</span></a>