Failed: error getting database names: not authorized on admin to execute command { listDatabases: 1 }
4.9/5 - (28 votes)

Last Updated on April 13, 2023 by Vikash Ekka

The error message “Failed: error getting database names: not authorized on admin to execute command { listDatabases: 1 }” typically occurs when trying to list the databases on a MongoDB instance, but the user does not have the necessary authorization to execute the “listDatabases” command on the “admin” database.

This error can happen in MongoDB when the user attempting to execute the “listDatabases” command does not have the appropriate privileges to access the “admin” database. In MongoDB, users need specific permissions to perform various operations on databases and collections, including listing databases.

What is MongoDB

MongoDB is a popular NoSQL database that requires users to have appropriate permissions to perform operations on databases and collections.

Also Read

How To Install Studio 3T and Activate free license

Mongodb not starting | failed to start mongod.service

How to install mongodb extension for PHP

How to Fix not authorized on admin to execute command

To resolve this issue, you can take the following steps:

Step1. Check the user’s roles and permissions: Verify that the user attempting to execute the “listDatabases” command has the necessary roles and permissions to perform this operation. The user should have the “listDatabases” privilege on the “admin” database.

Step2. Grant appropriate permissions: If the user does not have the necessary permissions, you can grant them using the MongoDB shell or a MongoDB client with administrative privileges. For example, you can use the following command in the MongoDB shell to grant the “listDatabases” privilege to a user on the “admin” database:

use admin
db.grantRolesToUser("username", [{ role: "listDatabases", db: "admin" }])

Replace “username” with the actual username of the user that needs the privilege.

Step3. Restart MongoDB: After making changes to user roles and permissions, you may need to restart the MongoDB instance for the changes to take effect.

Remember to follow the principle of least privilege and only grant the necessary permissions needed for users to perform their intended tasks to ensure proper security practices.

By following these steps, you should be able to resolve the “Failed: error getting database names: not authorized on admin to execute command { listDatabases: 1 }” error in MongoDB.

By Vikash Ekka

Hi All, My name is Vikash Ekka from India. I’m the founder and tech editor of https://www.vetechno.in. I have completed my Graduation in BCA. I love to write technical articles like Windows, Linux & MAC Tutorials, Tips, Tricks, How To fix, Tutorials About Ethical Hacking & Cyber Security Guide, and Software Review. Currently, I have been working as an IT professional since 2018.

Leave a Reply

Your email address will not be published. Required fields are marked *