skip to content

A MongoDB user created under `use orders` logs in from mongosh, but the application's connection string has no `authSource` and gets `Authentication failed`; how do you diagnose and fix it?

level: seniorimportance: must knowfreq 44%

answer

  1. the shell and the app differ somehow
  2. the client's error hides the reason
  3. read the server's failed-login log line
  4. which database did the login reach

basics

~20 s

The shell logged in against orders, but with no authSource or path the application logs in against admin, where no such user exists. The server log shows that; fix it by adding authSource=orders to the application's connection string.

solid answer

~50 s

The user's authentication database is `orders`, and `db.auth` in the shell ran against `orders`, so it worked. The application's connection string has neither `authSource` nor a path, so its driver logs in against `admin`, where no such user exists. The client cannot tell this apart from a bad password, because the server turns the failure into a generic `Authentication failed.`. So I go to the server log and read the `Failed to authenticate` line: its `db` attribute shows where the login arrived and its `error` says `UserNotFound`. I confirm with `usersInfo` across all databases, which shows the user's `db` field. The least invasive fix is `authSource=orders` in the connection string. If the team's standard is users in `admin`, I create one there with the same roles and drop the old one, because no command moves a user.

code

plaintext · 3 lines
plaintext
"msg":"Failed to authenticate",
"attr":{"mechanism":"SCRAM-SHA-256","user":"svc","db":"admin",
        "error":"UserNotFound: Could not find user \"svc\" for db \"admin\""}

go deeper

for a junior

Remember that the same username and password can fail just because the login went to a different database. Check authSource and the connection string's path first.

for a middle

Work out the login database from the connection string by hand, and explain why db.auth in the shell can succeed while a driver with the same password fails.

for a senior

Go straight to the server log for the Failed to authenticate line, since the client's message is generic, and rule out encoding and shard-local users once the database is proven right.

for a principal

Turn the incident into a convention: one place users live, an explicit authSource in every templated connection string, and a deployment check that fails fast on a bad login.

## Why the shell works and the application does not A MongoDB user belongs to its **authentication database**: the database where it was created. A login only succeeds when the client sends its credentials to that database. So the scenario has two different logins hiding behind the same username and password. - **The shell.** After `use orders` and `db.createUser(...)`, calling `db.auth("svc", ...)` authenticates against the current database, which is `orders`. That is the right one, and it works. - **The application.** A connection string such as `mongodb://svc:pw@db1:27017/?replicaSet=rs0` has no `authSource` and no database in its path. The client then falls back to `admin`, and there is no `svc` user there. The reverse mistake is just as common. A user lives in `admin`, and the connection string is `mongodb://svc:pw@db1:27017/orders`, because someone thought the path only picks the working database. Without `authSource`, that path also decides where the login goes, so the login reaches `orders` and fails. ## Why the client's error tells you so little The `saslStart` and `saslContinue` commands catch almost every failure and replace it with one status: code `AuthenticationFailed` and the text `Authentication failed.`. A missing user and a wrong password therefore read identically on the client. A client cannot probe which usernames exist this way, and for the same reason the application's own log cannot settle this question. The driver adds its own noise. The driver authentication specification says that a failed handshake must mark the server Unknown and clear its connection pool, so an application can show this as a connectivity problem rather than an obvious login error. ## How to diagnose it 1. **Read the server log on the node the application reached.** Look for the message `Failed to authenticate`. It carries `mechanism`, `user`, `db`, `client` and `error` attributes. The `db` attribute is the database the login actually went to, and an `error` of `UserNotFound: Could not find user "svc" for db "admin"` settles it. These lines depend on `enableDetailedConnectionHealthMetricLogLines`, which the manual documents as `true` by default. 2. **Look for the negotiation line too.** When a driver negotiates a mechanism for a user the server cannot find, a 9.0 server logs `Supported SASL mechanisms requested for unknown user`, naming the user and database it was asked about. 3. **Find where the user really lives.** Run `usersInfo` with `{ forAllDBs: true }` and a filter on the name. The `db` field in each result is that user's authentication database. You need `viewUser` on the relevant databases to see other users. 4. **Read the connection string the application really uses.** Check the deployed value, not the one in the repository. With `mongodb+srv` addresses, also check the DNS TXT record, which may supply `authSource`; an option in the connection string overrides it. ## How to fix it | Situation | Fix | Cost | |---|---|---| | The user is where it should be | Add `authSource=orders` to the connection string | A config change and a restart of the application | | The team standard is users in `admin` | Create the user in `admin` with the same roles, deploy, then drop the old one | A new password value and a two-step rollout | | The string has a misleading path | Add `authSource=admin`, keeping the path if the application reads it | A config change | The user management commands have no move operation, so changing a user's database means creating a new user. Do not edit `system.users` directly; the manual forbids it. Granting more roles does nothing here, because authorization only starts after the login succeeds. ## Other failures with the same message Once the database checks out, the same generic text can still come from these: - **A wrong password**, or one that was not encoded. The characters `$ : / ? # [ ] @` must be percent-encoded in a connection string, so a password containing `@` often works in a shell prompt and fails in a URI. - **A shard-local user on a sharded cluster.** A user created by connecting directly to a shard lives in that shard's `admin` database and cannot be reached through `mongos`. - **The wrong deployment.** A connection string aimed at another environment, where this user was never created, produces exactly the same message. An explicit `authMechanism` naming a mechanism the user has no credential for is different. It returns a specific `MechanismUnavailable` message rather than the generic text, so if you see that, the database was right. ## Afterwards A team that meets this twice should fix the cause: template connection strings so that `authSource` is always explicit, whichever database is the convention. An explicit option removes the fallback chain from the picture, and a reader no longer has to work out what an omitted value means.

  • Why doesn't MongoDB just tell the client that the user does not exist?
    The SASL commands replace almost every failure with one generic `Authentication failed.`, and the effect is that a client cannot use error text to learn which usernames exist in which database. The detail stays on the server side, in the `Failed to authenticate` log line, so diagnosis needs someone who can read server logs.
  • A user was created through `mongos`, and the same name fails when connecting directly to a shard. Why?
    Users created through `mongos` are stored in the `admin` database on the config servers and are used through `mongos`. A direct connection to a shard checks that shard's own local users, which are independent. For direct maintenance you create a shard-local administrative user on that shard.
  • How do you stop a wrong-database login from recurring across many services?
    Make `authSource` explicit in every connection string, generated from one template, and pick one convention for where users live. Explicit values remove the fallback from the path or to `admin`, so the string says what it does. A startup check that logs in and fails the deployment catches the rest early.

  • The application's error says authentication failed, so the password must be wrong.
  • Granting the user readWrite on admin will fix the failed login.
  • If the login works in mongosh, the user and the application's settings must be correct.
  • The database in the connection string's path only selects where the application works.
  • You can fix it by updating the db field in system.users directly.