Sling Academy
Home/MongoDB/MongoDB PrivilegeError: not authorized on admin to execute command

MongoDB PrivilegeError: not authorized on admin to execute command

Last updated: February 02, 2024

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.

Next Article: MongoDB ConflictingUpdateOperators Error: Common Causes and Solutions

Previous Article: MongoDB Error – AlreadyInitialized: dbclient is already initialized

Series: Fixing Common Issues in MongoDB

MongoDB

You May Also Like

  • MongoDB: How to combine data from 2 collections into one
  • Hashed Indexes in MongoDB: A Practical Guide
  • Partitioning and Sharding in MongoDB: A Practical Guide (with Examples)
  • Geospatial Indexes in MongoDB: How to Speed Up Geospatial Queries
  • Understanding Partial Indexes in MongoDB
  • Exploring Sparse Indexes in MongoDB (with Examples)
  • Using Wildcard Indexes in MongoDB: An In-Depth Guide
  • Matching binary values in MongoDB: A practical guide (with examples)
  • Understanding $slice operator in MongoDB (with examples)
  • Caching in MongoDB: A practical guide (with examples)
  • CannotReuseObject Error: Attempted illegal reuse of a Mongo object in the same process space
  • How to perform cascade deletion in MongoDB (with examples)
  • MongoDB: Using $not and $nor operators to negate a query
  • MongoDB: Find SUM/MIN/MAX/AVG of each group in a collection
  • References (Manual Linking) in MongoDB: A Developer’s Guide (with Examples)
  • MongoDB: How to see all fields in a collection (with examples)
  • Type checking in MongoDB: A practical guide (with examples)
  • How to query an array of subdocuments in MongoDB (with examples)
  • MongoDB: How to compare 2 documents (with examples)