MongoDB – How to Create Database Users

0

First and foremost, one would be required to enable authentication with MongoDB database. The details can be found on this page

MongoDB Database – Create User Command

Following steps can be used to create a database user:

  • Switch to the desired database. This is same as creating a database.
use databaseName
  • Create user using following command:
db.createUser({
    "user": "userName",
    "pwd": "somePassword",
    "roles": ["dbOwner"],
});

Above command would create user with with role such as dbOwner. The details on creating users can be found on this page covering the description on creating users – db.createUser() The user with role such as dbOwner can perform any administrative action on the database. The role combines the privileges granted by the readWrite, dbAdmin and userAdmin roles.

MongoDB – Built-in User Roles

Following are some of the roles which can be associated with the user:

  • Database user roles: Every database includes following client roles:
    • read
    • readWrite
  • Database administration roles: Every database includes following administration roles:
    • dbAdmin
    • dbOwner
    • userAdmin
  • Cluster administration roles: The admin database includes following roles for managing database cluster rather than the single database.
    • clusterAdmin
    • clusterManager
    • clusterMonitor
    • hostManager
  • Backup and restoration roles: The admin database includes following roles for backing up and restoring the data.
    • backup
    • restore
  • All-Database roles: Following roles are provided by admin database which applies to all the database except local and config databases.
    • readAnyDatabase
    • readWriteAnyDatabase
    • userAdminAnyDatabase
    • dbAdminAnyDatabase
  • Superuser roles: Provide full privileges on all resources.
    • root

The details on above roles can be found on this page, MongoDB Built-in User Roles.

Ajitesh Kumar

Ajitesh Kumar

Ajitesh is passionate about various different technologies including programming languages such as Java/JEE, Javascript, PHP, C/C++, mobile programming languages etc, and, computing fundamentals related with cloud-native technologies, application security, cloud computing platforms, mobile apps, big data etc.

He has also authored the book, Building Web Apps with Spring 5 and Angular.
Ajitesh Kumar

Leave A Reply

Time limit is exhausted. Please reload the CAPTCHA.