Free Ebook cover Creation and maintenance of MongoDB database from basic to advanced

Creation and maintenance of MongoDB database from basic to advanced

5

(1)

88 pages

User and role management in MongoDB

Capítulo 71

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Managing users and roles is a crucial part of administering any database, and MongoDB is no exception. This chapter of our course will cover how to create, manage, and assign roles to users in MongoDB.

In MongoDB, authentication and authorization are the two main ways to manage security. Authentication verifies a user's identity, while authorization determines what actions a user can perform. In other words, authentication is the process of verifying who you are, while authorization is the process of verifying what you are allowed to do.

To manage users and roles in MongoDB, you need to understand how MongoDB handles security. MongoDB uses a role-based security model, which means you can assign specific roles to specific users. Each role has a specific set of privileges that determine what the user can do.

To create a new user in MongoDB, you can use the db.createUser() command. This command creates a new user and assigns him a role. For example, the following command creates a new user named "myUser" with the password "myPassword" and the role "readWrite":

db.createUser(
   {
     user: "myUser",
     pwd: "myPassword",
     roles: [ "readWrite" ]
   }
)

This command creates a new user who has permission to read and write data. However, this user does not have permission to perform administrative tasks such as creating new databases or managing users.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

If you want to change a user's role, you can use the db.updateUser() command. This command updates the role of an existing user. For example, the following command updates the user role "myUser" to "dbAdmin":

db.updateUser(
   "myUser",
   {
     roles : [ "dbAdmin" ]
   }
)

This command updates the role of user "myUser" to "dbAdmin", which means this user now has permission to perform administrative tasks.

In addition, MongoDB also allows you to create your own custom functions. To create a new role, you can use the db.createRole() command. For example, the following command creates a new function called "readWriteAndAdmin":

db.createRole(
   {
     role: "readWriteAndAdmin",
     privileges: [
       {
         resource: { db: "mydb", collection: "" },
         actions: [ "find", "update", "insert", "remove", "createCollection", "dropCollection", "createIndex", "dropIndex", "viewOn", "collStats", "dbStats", "dbHash" , "dbAdmin" ]
       }
     ],
     roles: []
   }
)

This command creates a new role that has permissions to read, write and administer the "mydb" database.

In summary, managing users and roles in MongoDB is an important task that helps ensure the security of your database. By understanding how to create, manage, and assign roles to users, you can ensure that each user has the appropriate privileges to perform their tasks.

In the next chapter, we will cover how to monitor the performance of your MongoDB database and how to optimize it for the best possible performance. Stay tuned!

Now answer the exercise about the content:

Which command is used to create a new user in MongoDB and how does it work?

You are right! Congratulations, now go to the next page

You missed! Try again.

The correct command to create a new user in MongoDB is db.createUser(). This command allows you to create a new user account and assign a specific role to it. Roles determine the specific permissions that the user will have, allowing for effective user and role management within MongoDB's role-based security model.

Next chapter

Security Implementation in MongoDB

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.