Error
Error Code:
11
MongoDB Error 11: User Not Found Authentication Failure
Description
This error indicates that MongoDB could not find a user account corresponding to the credentials provided during an authentication attempt. It typically occurs when a client tries to connect to the database or perform operations using a username that does not exist in the configured authentication database.
Error Message
User Not Found
Known Causes
4 known causesMismatched Username
The username supplied during the connection or authentication process does not exactly match an existing user account in MongoDB.
User Not Yet Created
The user account you are attempting to authenticate with has not been properly defined or created within the MongoDB instance.
Incorrect Authentication Database
The authentication request is directed to a database where the specified user is not defined (e.g., authenticating against 'test' when the user is defined in 'admin').
Typographical Error
A simple spelling mistake or incorrect casing in the username prevents MongoDB from locating the intended user account.
Solutions
4 solutions available1. Verify Username and Authentication Database easy
Ensure the username and the database used for authentication are correctly specified.
1
When connecting to MongoDB, the username and the authentication database are crucial. The authentication database is where the user's credentials are stored. Common authentication databases are 'admin' or the specific database the user was intended to be created in.
mongosh --username <your_username> --password --authenticationDatabase <auth_db_name> <host>:<port>
2
Double-check the spelling of your username and the `--authenticationDatabase` parameter. A common mistake is to use the wrong database, especially if you have multiple databases with similar names or if the user was created in the 'admin' database.
mongosh --username myUser --password --authenticationDatabase admin myhost.com:27017
2. Create or Recreate the User medium
If the user does not exist or has incorrect credentials, create or recreate them.
1
Connect to your MongoDB instance with administrator privileges. This usually means connecting as the initial root user or another user with sufficient permissions.
mongosh --username rootUser --password
2
Switch to the database where the user should be authenticated (often 'admin' or the specific application database).
use admin
3
Create the user with the desired username, password, and roles. Replace `<new_username>`, `<new_password>`, and the roles as needed. If the user already exists, `db.createUser()` will throw an error, indicating you might need to use `db.updateUser()` or drop and recreate.
db.createUser({ user: "<new_username>", pwd: "<new_password>", roles: [ { role: "readWrite", db: "<database_name>" }, { role: "dbAdmin", db: "<database_name>" } ] })
4
If the user already exists and you suspect an issue with their credentials or roles, you can update them. Replace `<existing_username>` and the roles as needed.
db.updateUser("<existing_username>", { roles: [ { role: "read", db: "<database_name>" } ] })
5
Alternatively, if you want to ensure a clean slate, you can drop the existing user and then recreate them.
db.dropUser("<username_to_drop>")
3. Check MongoDB Logs for Detailed Errors medium
Inspect MongoDB server logs for more granular information about the authentication failure.
1
Locate your MongoDB server log files. The location varies depending on your operating system and installation method. Common locations include `/var/log/mongodb/mongod.log` (Linux) or within the MongoDB data directory.
tail -f /var/log/mongodb/mongod.log
2
Look for log entries around the time of the authentication failure. Error messages related to authentication might provide more specific details, such as 'authentication failed for user <username>' or 'user not found in database <db_name>'.
grep -i 'authentication failed' /var/log/mongodb/mongod.log
3
Analyze the log messages. They can often pinpoint whether the issue is with the username, password, the authentication database, or if the user simply doesn't exist in the specified authentication database.
text: Examine log messages for specific details like 'user not found in database admin' or 'authentication failed for user testUser'.
4. Verify MongoDB Service Configuration for Authentication advanced
Ensure that MongoDB is configured to use authentication and that the security settings are correct.
1
Check the MongoDB configuration file (e.g., `mongod.conf`). Ensure that the `security.authorization` setting is enabled. If it's not set, authentication might not be enforced.
security:
authorization: enabled
2
If you've recently enabled authentication, you'll need to restart the MongoDB service for the changes to take effect.
sudo systemctl restart mongod
3
Review any custom security configurations or network access control lists that might be preventing the client from reaching the authentication database or the user from being recognized.
text: Check firewall rules and network configurations if connecting remotely.