Home > database >  grantRolesToUser fails with error "Could not find role: root"
grantRolesToUser fails with error "Could not find role: root"

Time:10-31

Using mongosh in mongo compass, I've created a user for my database:

use test_db
db.createUser({user:'some_user', pwd: 'some_pass', roles:[]})

Now, when I try to assign root role to it, I get could not find role: root@test_db

db.grantRolesToUser(    "some_user",
    [
      { role: "root", db: "test_db" }
    ])

Isn't root role a built-in role? Why cannot I assign it? Currently, when I try to run an aggregate for instance, I get MongoServerError: not authorized on test to execute command { aggregate: .... That's why I want to assign the root role to get rid of this.

CodePudding user response:

Role root is defined in admin database. In database test_db this role does not exist - unless you created it manually.

Note:

Except for roles created in the admin database, a role can only include privileges that apply to its database and can only inherit from other roles in its database.

A role created in the admin database can include privileges that apply to the admin database, other databases or to the cluster resource, and can inherit from roles in other databases as well as the admin database.

Actually, I don't know any reason to create user in other database than admin.

  • Related