How to Create and Drop Users in MongoDB

How to Create and Drop Users in MongoDB

How to Create and Drop Users in MongoDB

MongoDB provides a user management system where you can create users with different roles and permissions for your databases. These roles help control the level of access to data and operations. Below is a step-by-step guide on how to create and drop users in MongoDB.

1. Connect to MongoDB

Before managing users, you need to authenticate to the MongoDB instance with a user who has sufficient privileges (e.g., an admin user).

mongosh --username admin --password --authenticationDatabase admin

Replace admin with your admin username and provide the correct password.

2. Create a User in MongoDB

MongoDB allows you to create users for specific databases with various roles that determine their permissions.

A. Syntax to Create a User

db.createUser({ user: "<username>", pwd: "<password>", // Password for the user roles: [ { role: "<role>", db: "<database>" } ] });
  • user: The username you are creating.
  • pwd: The password for the user.
  • roles: The roles assigned to the user (e.g., readWrite, dbAdmin, read, root).

B. Create a User with Specific Roles

Example 1: Create a User with readWrite Role

To create a user with readWrite access to a database (for example, blogDB), run:

use blogDB // Switch to the desired database db.createUser({ user: "appUser", pwd: "password123", // Replace with a secure password roles: [ { role: "readWrite", db: "blogDB" } ] });
  • readWrite: This role allows the user to read and write data in the blogDB database.

Example 2: Create a User with Admin Role

To create an admin user with full control over a specific database:

use blogDB db.createUser({ user: "adminUser", pwd: "adminPassword123", // Replace with a secure password roles: [ { role: "dbAdmin", db: "blogDB" } ] });
  • dbAdmin: This role grants the user administrative privileges over the blogDB database, allowing tasks like creating indexes or managing collections.

Example 3: Create a User with read Role

To create a user with read-only access to a database:

use blogDB db.createUser({ user: "readOnlyUser", pwd: "readOnlyPassword123", // Replace with a secure password roles: [ { role: "read", db: "blogDB" } ] });
  • read: This role grants the user read-only access to the database.

3. Drop (Delete) a User in MongoDB

You can also delete users that are no longer needed using the dropUser() command.

A. Syntax to Drop a User

db.dropUser("<username>");
  • <username>: The username of the user you want to delete.

B. Example of Dropping a User

To drop a user named appUser from the blogDB database:

use blogDB db.dropUser("appUser");

This will permanently remove the appUser from the blogDB database.

4. List All Users in MongoDB

You can list all users in the current database by using the show users command:

show users

This will display a list of all users and their roles for the current database.

5. Modify (Update) a User in MongoDB

To modify an existing user's roles or permissions, you can use the updateUser() method.

A. Syntax to Modify a User

db.updateUser("<username>", { roles: [ { role: "<new_role>", db: "<database>" } ] });

B. Example of Modifying a User's Roles

If you want to add a readWrite role to an existing user appUser for the blogDB database, run:

use blogDB db.updateUser("appUser", { roles: [ { role: "readWrite", db: "blogDB" } ] });

6. Example of Complete Workflow

1. Create an Admin User for the Admin Database

use admin db.createUser({ user: "adminUser", pwd: "adminPassword123", roles: [ { role: "root", db: "admin" } ] });

2. Create a User for Application Access

use blogDB db.createUser({ user: "appUser", pwd: "appUserPassword123", roles: [ { role: "readWrite", db: "blogDB" } ] });

3. Drop a User

use blogDB db.dropUser("appUser");

7. Summary

  • Creating Users: You create users with specific roles using the db.createUser() method. You assign roles like readWrite, dbAdmin, or root to grant appropriate permissions.
  • Dropping Users: To remove a user, use the db.dropUser() command.
  • Modifying Users: If you need to update a user's roles, use db.updateUser().
  • Listing Users: Use the show users command to list all users in the current database.

Would you like more examples or further details on managing MongoDB users and roles? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close