Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

MongoDB "command find requires authentication" and "not authorized on db to execute command" (Unauthorized, code 13): not signed in, or signed in without the role

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

MongoDB returns error code 13, Unauthorized, in two situations that need different fixes. command find requires authentication means access control is on and the connection has not signed in. not authorized on appdb to execute command { find: ... } usually means the connection did sign in, but the user's roles do not allow that action on that database. With no users yet and a local connection, it can also come from the localhost exception. Read which of the two you have, check what the connection is signed in as, then fix the connection string or grant the missing role.

What the error means

MongoDB's access control page says: "Enabling access control on a MongoDB deployment enforces authentication. With access control enabled, users must authenticate and can only perform actions permitted by their assigned roles." The SCRAM tutorial turns it on with mongod --auth or the security.authorization setting set to enabled.

Our own reading of src/mongo/db/commands.cpp separates the two messages. The check that builds requires authentication runs first and fails when the command needs authentication and the session is not authenticated. The check that builds not authorized on <db> to execute command <command> wraps the later authorization check and fails when that check refuses the command. A session that is using the localhost exception is not decided by the first check (the source returns early with the comment "Still can't decide on auth because of the localhost bypass."), so it can also receive not authorized. Both messages use error code Unauthorized, which error_codes.yml at r8.0.4 lists as code 13.

The first message changed its capitalisation. At tags r5.0.28 and r6.0.19 the source writes command <name> requires authentication in lower case. At r7.0.14 and r8.0.4 it writes Command <name> requires authentication with a capital C. The search text you meet depends on the server version.

MongoDB's localhost exception page adds one special case: "the localhost exception only applies when there are no users or roles created in the MongoDB instance." On an instance with access control and no users, only a local connection can create the first user.

Likely causes

Cause What you see How to confirm
The client sent no credentials requires authentication connectionStatus lists no authenticated users
The connection string has no username (our own reading: or the variable that fills it is empty) Same message; the authSource option has no effect The page says MongoDB ignores authSource if no username is given
Access control was switched on and the application was never given a login The application worked before the change and fails after it The configuration shows authorization: enabled or the process has --auth
The user signed in but holds no role on that database not authorized on appdb to execute command, or (our own reading) the same message from an unauthenticated local connection while no users exist getUser shows roles only on other databases; for the local case, connectionStatus lists no users and the instance has no users
The user holds a role, but not one that allows the action Reads work and writes fail, or the reverse The roles from getUser against the built-in role descriptions

Prove the cause before changing anything

All of these only read. Run them from a mongosh session that uses the same login as the failing client.

  1. Note which message you have and, for not authorized, the database and the command named in it.

  2. Ask the server who this connection is:

db.runCommand({ connectionStatus: 1, showPrivileges: true })

MongoDB's connectionStatus page says showPrivileges returns "the full set of privileges that currently-authenticated users possess", and the result's authInfo.authenticatedUsers lists the users. An empty list means the connection never signed in. A listed user with no privileges on the database named in the error means a missing role. An empty list together with a not authorized message points, in our own reading, at the localhost exception: the instance has no users yet.

  1. Read the user's roles. The getUser page says: "Run this method on the user's database."
use appdb
db.getUser("appuser")

Compare the roles array, each entry with a role and a db, with the database in the error message. A user created in admin can hold roles on other databases. The SCRAM tutorial says the database where you create a user "is the user's authentication database", and that "The user's authentication database doesn't limit the user's privileges."

  1. Check whether access control is on. Look for security.authorization set to enabled in the configuration file the server starts with, or --auth on its command line.

Fix

The client sent no credentials

MongoDB's connection string page says authSource is the "Database name associated with the user's credentials. Defaults to defaultauthdb if specified, otherwise defaults to admin." The same page says characters such as $ : / ? # [ ] @ in a username or password must be percent-encoded.

To test the login from a shell first (this only reads), the MongoDB Shell connect page shows this form:

mongosh "mongodb://db01.example.com:27017" --username appuser --authenticationDatabase appdb

mongosh asks for the password; type it when prompted.

First save the current state:

cp -p app.env app.env.2026-12-03

Disruptive step: the next edit changes the database connection string in app.env, and the application restart that follows interrupts it. A wrong value leaves the application unable to sign in.

Edit app.env so the line carries the username, the password and the authSource the application uses, then restart the application so it reads the file:

MONGODB_URI=mongodb://appuser:PASSWORD@db01.example.com:27017/appdb?authSource=appdb

Only if you need to back out, copy the saved file back, which overwrites any later edit to app.env, restart the application, and check that it starts.

Disruptive step: the next command overwrites app.env with the copy saved above, so any other edit made to the file since that copy is lost, and the application restart that follows interrupts it.

cp -p app.env.2026-12-03 app.env

The user lacks a role on the database

The built-in roles page says readWrite "Provides all privileges of the read role plus the ability to modify data on all non-system collections and the system.js collection." If the application only reads, read is the narrower role. The grantRolesToUser page says: "You must have the grantRole action on a database to grant a role on that database." Run the grant while signed in as a user who holds that action on appdb. The grantRolesToUser and revokeRolesFromUser pages both say: "This command is not supported in MongoDB Atlas clusters." On Atlas, neither command below applies.

First save the current state, the user's record as it is now:

mongosh --quiet --eval 'use appdb' --eval 'db.getUser("appuser")' "mongodb://db01.example.com:27017" --username admin1 --authenticationDatabase admin > appuser-before.txt

Risk: the next command gives the user appuser the readWrite role on appdb. From then on that user can change every non-system collection in that database.

Run it in the database where the user was created:

use appdb
db.grantRolesToUser("appuser", [ { role: "readWrite", db: "appdb" } ])

Only if you need to back out: revoke the same role, which removes readWrite on appdb from appuser. The revokeRolesFromUser page describes removing roles from a user on the current database.

Risk: the next command removes the readWrite role on appdb from appuser. If appuser-before.txt shows the user already held that role before the grant, do not run it.

use appdb
db.revokeRolesFromUser("appuser", [ { role: "readWrite", db: "appdb" } ])

Then run db.getUser("appuser") and compare the roles array with appuser-before.txt.

Example (illustrative): On db01, started with mongod --auth, the application on app01 fails with Command find requires authentication (8.0 wording). connectionStatus from the application's login shows an empty authenticatedUsers list, and the app.env line has no username. The administrator puts appuser and authSource=appdb into the string and restarts the application. The next error is not authorized on appdb to execute command { find: "orders" }: getUser("appuser") shows one role, readWrite on reports. The application also writes orders, so after saving appuser-before.txt the administrator grants readWrite on appdb, and the query succeeds.

Confirm the fix

  1. db.runCommand({ connectionStatus: 1, showPrivileges: true }) from the application's login lists appuser and privileges on the database named in the error.
  2. The command that failed now completes.
  3. Check a command that should still fail, for example a write by a read-only user. It must return not authorized, which shows that you granted no more than intended.

What to watch afterwards

  • Keep one login per application and grant roles on that application's database only.
  • When you switch on access control, create the first user while the localhost exception still applies, then give each application its own login before restarting it.
  • Record which database holds each user. getUser finds nothing when run on the wrong one: the page says it returns null if the user does not exist in that database.

Risks and limits

  • These commands have not been run or lab-tested.
  • The two-message reading rests on the server source code (commands.cpp), not on a documentation page.
  • The SCRAM tutorial recommends passwordPrompt() so the password is not visible on screen or kept in shell history. Our own advice: a password in app.env is readable by anyone who can read that file, so restrict it.
  • Granting readWrite is broader than the application may need. Grant read where reading is enough.
  • Replica sets and sharded clusters run access control with keyfile or other internal authentication as well. Those pages are not covered here.

Sources