Couldn't create users with Custom Roles

74 views
Skip to first unread message

Harshaa Subramani

unread,
Sep 28, 2015, 2:45:23 AM9/28/15
to mongodb-user

Hi,
    I 'm running mongod with authentication enabled.I wanted to create a Custom Role and assign Users to it.I executed the following queries:

  > use admin
     switched to db admin
 >  db.createRole(
...    {
...      role: "Role1",
...      privileges: [
...        { resource: { cluster: true }, actions: [ "killop", "inprog" ] },
...        { resource: { db: "accounts", collection: "" }, actions: [ "killCursors" ] }
...      ],
...      roles: []
...    }
... )
     
   The above query successfully added the role named "Role1" to the collection system.roles under admin database.


Then I switched to the database accounts to create user

 > use accounts
 >> db.createUser(
...   {
...     user: "User1",
...     pwd: "user1",
...     roles: [ { role: "Role1", db: "accounts" } ]
...   }
... )

2015-09-28T12:10:33.652+0530 Error: couldn't add user: No role named Role1@accounts at src/mongo/shell/db.js:1081

How to resolve this error????????




 

Amit Sanjay Sadafule

unread,
Jun 7, 2019, 12:20:09 AM6/7/19
to mongodb-user
Did you solve the problem? If so, could you please suggest what did you do.

Thanks
Amit

Amit Sanjay Sadafule

unread,
Jun 7, 2019, 1:11:07 AM6/7/19
to mongodb-user
I figured it out. In your case, Role1 is not present in accounts db.

One way is to create new user in admin with Role1 attached.

e.g.
use admin
db.createRole({
    role: "Role1", 
    privileges: [
        { resource: { cluster: true }, actions: [ "killop", "inprog" ] },
        { resource: { db: "", collection: "" }, actions: [ "killCursors" ] }
    ],
    roles: []
})
db.createUser({user: 'killOpUser', pwd: '**********', roles:['Role1']})

Now this user can have access to currentOp and killOp both on any db.
Reply all
Reply to author
Forward
0 new messages