Skip to content

Instantly share code, notes, and snippets.

@waiphyo285
Last active October 20, 2024 08:44
Show Gist options
  • Save waiphyo285/e997b7291fb6c77847e1502007d0814d to your computer and use it in GitHub Desktop.
Save waiphyo285/e997b7291fb6c77847e1502007d0814d to your computer and use it in GitHub Desktop.

Setting Up MongoDB Authentication

Step 1: Update MongoDB Config File

Open the mongod.conf file in a text editor with superuser privileges. You can use nano, vim, or any other text editor of your choice. Here’s how to do it with nano:

sudo nano /etc/mongod.conf

Add or update the following lines to enable authorization:

security:
    authorization: enabled

Save the file and exit the text editor. For nano, you can save and exit by pressing CTRL + X, then Y, and then ENTER.

Step 2: Restarting the MongoDB Service

After updating the configuration file, restart the MongoDB service to apply the changes:

sudo systemctl restart mongod

Step 3: Connecting to MongoDB without Authentication

This command opens the MongoDB shell (mongosh) and connects to MongoDB running on port 27017. Since no authentication credentials are provided, it assumes that access control is not yet enabled.

mongosh --port 27017

Step 4: Switching to the admin Database

This command switches the current database context to the admin database. The admin database is special in MongoDB and is used for administrative tasks, including user management.

use admin

Step 5: Creating an Administrative User

db.createUser(
  {
    user: "superAdmin",
    pwd: passwordPrompt(),  // This will prompt the user to enter a password securely
    roles: [
      { role: "userAdminAnyDatabase", db: "admin" },
      { role: "readWriteAnyDatabase", db: "admin" }
    ]
  }
)

This command creates a new user named superAdmin with administrative privileges. The passwordPrompt() function securely prompts the user to enter a password. The roles assigned to this user are:

  • userAdminAnyDatabase: Allows the user to manage users across all databases.
  • readWriteAnyDatabase: Allows the user to read and write data in any database.

Step 6: Exiting the MongoDB Shell

exit

Step 7: Connecting to MongoDB with Authentication

This command opens the MongoDB shell again, but this time it connects using the superAdmin user created earlier. The --authenticationDatabase "admin" option specifies that the user's credentials should be authenticated against the admin database. The -u and -p options provide the username and prompt for the password, respectively.

mongosh --port 27017 --authenticationDatabase "admin" -u "superAdmin" -p

Step 8: Switching to the Target Database

This command switches the current database context to your_db_name, which is the database for which you want to create a new user.

user your_db_name

Step 9: Creating a Regular User for the Target Database

db.createUser(
  {
    user: "yourUsername",
    pwd: passwordPrompt(),  // This will prompt the user to enter a password securely
    roles: [ { role: "readWrite", db: "your_db_name" } ]
  }
)

This command creates a new user named yourUsername with read and write privileges on your_db_name. The passwordPrompt() function securely prompts the user to enter a password.

Step 10: Granting Additional Roles to the User

This command grants the readWrite role on your_db_name to yourUsername. This role allows the user to read and write data in your_db_name.

db.grantRolesToUser("yourUsername", [ { role: "readWrite", db: "your_db_name" }] );

Step 11: Exiting MongoDB Shell

exit

Step 12: Checking MongoDB Service Status

This command checks the status of the MongoDB service. It uses systemctl, which is a system and service manager for Linux operating systems. The mongod service is the primary daemon process for MongoDB.

sudo systemctl status mongod

Step 13: Restarting MongoDB Service

This command restarts the MongoDB service. Restarting the service is often necessary to apply configuration changes, such as enabling authentication.

sudo systemctl restart mongod

By following these commands, you can set up and manage users in MongoDB with appropriate roles and privileges. This ensures secure access to your MongoDB databases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment