How to Fix MongoDB Error 18 (AuthenticationFailed)
You get mongodb error 18 when the server rejects a client login attempt. You fix it by matching the username, password, and auth database across your setup. The official error name for code 18 is AuthenticationFailed. It blocks the client before any database query can run. Teams see this error after a password rotation or a deployment change. It also appears when an app points to the wrong database for login checks. What causes mongodb error 18 in your cluster MongoDB stores user records ins
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You get mongodb error 18 when the server rejects a client login attempt. You fix it by matching the username, password, and auth database across your setup.
The official error name for code 18 is AuthenticationFailed. It blocks the client before any database query can run. Teams see this error after a password rotation or a deployment change. It also appears when an app points to the wrong database for login checks.
What causes mongodb error 18 in your cluster
MongoDB stores user records inside a specific database. That database acts as the user auth source. Every user account belongs to one auth database. If a client connects to the wrong auth source, the login fails with code 18. The password might be right, but the server looks in the wrong place.
Typos in secret stores also trigger this error code. Special characters in a password can break a connection string if they lack encoding. When a parser misreads the string, it sends a bad password to the server. The server then returns AuthenticationFailed right away.
Another common cause is a mismatch in the login method. A client and a server must agree on how to check the key. If the client uses an old method that the cluster rejects, login fails. You will also see this error if the user account does not exist yet.
How to diagnose and fix the login failure safely
Start by checking the server log files for the failed login event. The log entry shows the user name and the target database. Compare those two values with the secret your app uses. Make sure the client points to the exact database where you created the user.
Next, check the password for special characters like colons, slashes, or at signs. Encode those characters before placing the password into a connection string. Better yet, pass the username and password as separate fields in your driver setup. This step stops string parsing bugs from breaking your login.
MongoDB Authentication & Authorization + Service-to-Service Authentication: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
When you recreate or fix a user account, keep role privileges tight. MongoDB grants access through roles on defined resources down to the collection level. Do not grant cluster admin rights just to make a login error go away. Admin roles do not fix code 18, because code 18 happens before role checks.
Assign only the role the app needs on its target database. The built-in read role grants read access to all non-system collections in that database. It allows actions like find, listCollections, and listIndexes, while blocking insert, update, and remove. If the app must change data, use the readWrite role on that database instead. Note that Atlas users and self-hosted users have different built-in role options.
How to prevent credential drift across teams
Many login failures happen when teams share static passwords across many apps. When one person rotates a shared password, other apps break with code 18. You can stop this cycle by removing shared static keys from developer laptops.
Route human and tool logins through a central access layer instead. Centralizing login rules lets you manage database accounts in one place. You can pair role limits with data masking to protect sensitive fields. Masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in results. This setup keeps production data safe while preventing manual password mistakes.
Add command guardrails and field masking beside MongoDB
Collection roles and IP rules control who connects to MongoDB. Yet an active client or agent session can still run a pipeline that writes data or reads raw personal fields. In practice, hoop.dev sits inline in the wire protocol beside your database as a sidecar for your MongoDB. For every command an agent or human sends in production, hoop.dev decides to pass, mask, block, or escalate it for review.
Using hoop.dev MongoDB controls, guardrails refuse banned commands before they run or hold risky calls for approval, and data masking rewrites matched fields in returned documents in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and generates evidence for GDPR, HIPAA, and PCI DSS programs. View the project on GitHub.
Frequently asked questions
Does mongodb error 18 mean my user lacks read or write permissions?
No, code 18 means the login step itself failed. Role permission issues happen after login and return a different authorization error code.
Why do I get AuthenticationFailed when my password is correct?
Your client is likely checking the wrong auth database for that user account. Point your client login setup to the database where the user was created.
Can special characters in a password trigger mongodb error 18?
Yes, unescaped characters in a URI string corrupt the password value sent to the server. Encode special characters in the URI or pass the password as a separate field.
Related guides
In this post