This guide will help you install and configure MongoDB with authentication and how to set up a user with a password.
- Windows OS
- Administrative privileges on your machine
-
Download MongoDB Community Edition:
MongoDB Community Download -
Download MongoDB Shell:
MongoDB Shell Download
-
Open Environment Variables:
- Search for Environment Variables in the Windows search bar and select Edit the system environment variables.
-
Modify the System Path:
- In the System Properties window, click Environment Variables.
- Under System Variables, scroll down and select Path, then click Edit.
- In the Edit Environment Variable window, click New and paste the MongoDB installation path(yours):
it's mine: C:\Program Files\MongoDB\Server\8.0\bin
- Click OK to save the changes.
-
Open Git Bash or Command Prompt:
- You can use either Git Bash or Command Prompt for MongoDB interaction.
-
Run MongoDB Shell:
- In the terminal, type
mongosh
and press Enter. - This will take you to the MongoDB shell.
- In the terminal, type
- Create a Root User:
- In the MongoDB shell, switch to the admin database:
use admin
- Create a root user with a password:
db.createUser({ user: "root", pwd: "yourpassword", // Replace with your desired password roles: [{ role: "root", db: "admin" }] })
- This will create a user with full admin privileges.
- In the MongoDB shell, switch to the admin database:
-
Edit MongoDB Configuration File:
- Go to the
bin
folder where MongoDB is installed (e.g.,C:\Program Files\MongoDB\Server\8.0\bin
). - Open the mongod.conf file using Notepad.
- Go to the
-
Enable Authentication:
- Locate the security section and uncomment (remove the
#
symbol) and enable authorization:security: authorization: "enabled"
- Locate the security section and uncomment (remove the
-
Save and Close the configuration file.
- "If your Notepad doesn't allow you to save the mongod.conf file in the same directory after modification, open Notepad using Windows Search, run it as Administrator, then navigate to the mongod.conf file, modify it again, and save."
-
Open Command Prompt as Administrator:
- Press
Windows + S
, type Command Prompt, right-click, and select Run as Administrator.
- Press
-
Stop MongoDB:
- In the Administrator Command Prompt, run:
net stop MongoDB
- In the Administrator Command Prompt, run:
-
Start MongoDB:
- Once MongoDB is stopped, run:
net start MongoDB
- Once MongoDB is stopped, run:
-
Log In to MongoDB:
- Go back to Git Bash or Command Prompt and type:
mongosh -u "userName(that you gave)" -p
- Enter the password you created for the user when prompted.
- Go back to Git Bash or Command Prompt and type:
-
Enjoy MongoDB with Authentication Enabled!
---bash
- Always run MongoDB as Administrator when stopping or starting services.
- You can change the
root
username and password to anything you like, but the username must match the one used inmongosh -u "YourUsername" -p
. - for read and write always update for each db:
- for example:
db.updateUser("Khairul", { roles: [ { role: "userAdminAnyDatabase", db: "admin" }, { role: "readWrite", db: "college" }, { role: "readWrite", db: "yourNewDB" } // add new DB here ]
Feel free to customize it further based on your setup and needs. This README will help others follow the process smoothly!
Let me know if you need any changes or further improvements!