MongoDB PrivilegeError: not authorized on admin to execute command

Updated: February 2, 2024 By: Guest Contributor Post a comment

The Problem

The error message PrivilegeError: not authorized on admin to execute command is a common issue that MongoDB users might encounter. This error indicates that the current user does not have the necessary permissions to perform the intended action in the MongoDB admin database. This guide will delve into the underlying reasons for this error and present several effective solutions to resolve it.

Solution 1: Grant Appropriate Roles

Issue qualitative permissions by assigning suitable roles to the user trying to execute the command.

  1. Log into MongoDB with a user that has the userAdmin or userAdminAnyDatabase role.
  2. Use the db.grantRolesToUser function to grant necessary permissions.
  3. Confirm the roles have been assigned correctly.

Example:

// Switch to the 'admin' database
use admin

// Grant specific roles to a user
// Replace 'yourUserName' with the actual username
// Replace 'role1' and 'role2' with the actual role names to be granted
db.grantRolesToUser(
    'yourUserName', ['role1', 'role2']
)

Note: Assigning privileges should be done cautiously to maintain database security. An incorrect role can either limit required actions or expose your database to security risks.

Solution 2: Create a New User with Proper Roles

If the existing user does not have the required roles, creating a new user with correct permissions may be advisable.

  1. Connect to the MongoDB shell as a user with userAdmin privileges.
  2. Create a new user with administrative roles using db.createUser.
  3. Log out and re-login using the new user credentials.

Example:

// Switch to the 'admin' database
use admin

// Create a new user in the MongoDB database
db.createUser({
    user: 'newAdmin',    // Set the username for the new user
    pwd: 'password',     // Set the password for the new user
    roles: ['root']      // Assign roles to the new user. Here, 'root' role is assigned for full access
})

Note: Using the ‘root’ role gives extensive capabilities. Create user-specific roles to adhere to the principle of least privilege.

Solution 3: Update User Roles

Modify the existing user’s role if it doesn’t align with current access necessities.

  1. Access MongoDB as a user with userAdmin rights.
  2. Check the current user’s roles and update them using db.updateUser.
  3. Exit the shell and re-login with updated permissions.

Example:

// Switch to the 'admin' database
use admin

// update roles
db.updateUser('existingUser', {
    roles: ['newRole1', 'newRole2']
})

Note: Regularly review user permissions to ensure they’re in line with the responsibilities and changes in your data access patterns.

Solution 4: Authentication Database

If you’re connecting to a MongoDB instance, ensure you’re authenticating against the right database.

  • Be certain you are connecting with the correct credentials and the proper authentication database.
  • Use the --authenticationDatabase admin option with the mongo client.

Example:

mongo -u yourUserName -p yourPassword --authenticationDatabase admin

Note: Users must authenticate against the database where their credentials and roles are stored, usually the admin database.

Conclusion

Each solution presented caters to a similar outcome—ensuring the user has appropriate access levels to perform database commands. It’s essential to understand user roles and privileges in MongoDB to prevent encountering PrivilegeErrors and to maintain a secure database environment.