MongoDB: Not authorized on admin to execute command
14,792
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
- Stop the mongod instance
- Start the instance with
$ mongod --auth
- Now start mongo shell and create user
use admin db.createUser( { user: "admin", pwd: "password", roles: [ { role: "root", db: "admin" } ] } ); exit;
Now give permission
db.auth("admin", "password") db.grantRolesToUser("password", [ { role: "readWrite", db: "admin" } ])
Now check
show users
Related videos on Youtube
Author by
DragonBorn
Updated on June 04, 2022Comments
-
DragonBorn almost 2 years
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
-
charan tej over 6 years@SLASH what error you got exactly after executing with root
-
DragonBorn over 6 yearsI got this error: couldn't add user: not authorized on admin to execute command
-
DragonBorn over 6 yearsHere is the whole thing Error: couldn't add user: not authorized on admin to execute command { createUser: "admin", pwd: "xxx", roles: [ { role: "root", db: "admin" } ], digestPassword: false, writeConcern: { w: "majority", wtimeout: 600000.0 } } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype.createUser@src/mongo/shell/db.js:1292:15 @(shell):1:1
-
DragonBorn over 6 yearsSame error: Error: not authorized on admin to execute command { grantRolesToUser: "admin", roles: [ "readWrite" ], writeConcern: { w: "majority", wtimeout: 600000.0 } } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype.grantRolesToUser@src/mongo/shell/db.js:1493:19 @(shell):1:1
-
charan tej over 6 years
mongo --port 27017 -u "admin" -p "password" --authenticationDatabase "admin"
use command and run -
DragonBorn over 6 yearsIt says Authentication failed
-
DragonBorn over 6 yearsNope didn't work. I started it with
mongod --auth
. Still gave me the same errors. I had to remove everything and reinstall everything and it works now