DragonBorn
DragonBorn

Reputation: 1809

MongoDB: Not authorized on admin to execute command

My mongo shell is giving me this error when I am using show dbs command.

not authorized on admin to execute command

I have tried to create a user using

db.createUser(
  {
    user: "siteUserAdmin",
    pwd: "password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

as on https://docs.mongodb.com/v2.6/tutorial/add-user-administrator/

But it still gives me the same error:

Could not add user: not authorized on admin to execute command.

I am using Ubuntu 16.04 and mongoDB version is 3.4.9

Upvotes: 5

Views: 19543

Answers (1)

charan tej
charan tej

Reputation: 1054

I think you can use the role as root for Ubuntu. Try the below query.

use admin
db.createUser(
  {
    user: "admin",
    pwd: "password",
    roles: [ { role: "root", db: "admin" } ]
  }
);
exit; 

You can give read write permission to user as well

use admin

 db.grantRolesToUser("admin",["readWrite"])

EDIT

Since the above didn't work try to start the instance with auth as below

  1. Stop the mongod instance
  2. Start the instance with $ mongod --auth
  3. Now start mongo shell and create user
   use admin
   db.createUser(
            {
             user: "admin",
             pwd: "password",
             roles: [ { role: "root", db: "admin" } ]
            }
                );
           exit;
  1. Now give permission

     db.auth("admin", "password")
     db.grantRolesToUser("password", [ { role: "readWrite", db: "admin" } ])
    
  2. Now check show users

Upvotes: 3

Related Questions