0
votes

I need to create user in MongoDB with "root" role but only to few selected databases (database_one and database_two). An my questions are:

  1. In which database should I create user?
  2. Do I need to create custom roles? With what settings?
  3. Which roles should I grant to user?

My plan was that I create user in DB admin, set roles dbAdmin, userAdmin, readWrite with both databases (database_one and database_two). But in this case I'm not able to connect as this user. Can you help me how to setup user and his roles?

1

1 Answers

0
votes

Assuming you have chosen a current database (by using this command use <databaseName>) that you are already working with, then you can try below code to add a user and specify his/her credentials and roles.

db.addUser({user: "root", pwd: "123456", roles: [ "userAdminAnyDatabase", "readWriteAnyDatabase", "dbAdminAnyDatabase", "clusterAdmin" ]}));

OR to create a user admin for a single database:

use <databaseName>
db.createUser(
  {
    user: "recordsUserAdmin",
    pwd: "password",
    roles: [ { role: "userAdmin", db: "records" } ]
  }
)

More from here