MongoDB – How to Create Database Users

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

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.
Posted in MongoDB, NoSQL. Tagged with , .