Skip to content

Instantly share code, notes, and snippets.

@waiphyo285
Created July 10, 2024 14:33
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: Updating the MongoDB Configuration 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

mongosh --port 27017

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.

Step 4: Switching to the admin Database

use admin

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.

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

This command exits the MongoDB shell.

Step 7: Connecting to MongoDB with Authentication

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

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.

Step 8: Switching to the Target Database

user your_db_name

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

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

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

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.

Step 11: Exiting the MongoDB Shell

exit

This command exits the MongoDB shell.

Step 12: Checking MongoDB Service Status

sudo systemctl status mongod

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.

Step 13: Restarting the MongoDB Service

sudo systemctl restart mongod

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

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