Skip to content

This guide will help you install and configure MongoDB with authentication and how to set up a user with a password.

Notifications You must be signed in to change notification settings

Khairul25556/mongodb-install-auth-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

MongoDB Setup and Configuration Guide

This guide will help you install and configure MongoDB with authentication and how to set up a user with a password.

Prerequisites

  • Windows OS
  • Administrative privileges on your machine

Step 1: Download MongoDB and MongoDB Shell

  1. Download MongoDB Community Edition:
    MongoDB Community Download

  2. Download MongoDB Shell:
    MongoDB Shell Download

Step 2: Set Up Environment Variables

  1. Open Environment Variables:

    • Search for Environment Variables in the Windows search bar and select Edit the system environment variables.
  2. 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.

Step 3: Start MongoDB Shell

  1. Open Git Bash or Command Prompt:

    • You can use either Git Bash or Command Prompt for MongoDB interaction.
  2. Run MongoDB Shell:

    • In the terminal, type mongosh and press Enter.
    • This will take you to the MongoDB shell.

Step 4: Set Up Authentication

  1. 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.

Step 5: Enable Authentication in MongoDB Configuration

  1. 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.
  2. Enable Authentication:

    • Locate the security section and uncomment (remove the # symbol) and enable authorization:
      security:
        authorization: "enabled"
  3. Save and Close the configuration file.

Notes:

  • "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."

Step 6: Restart MongoDB Service

  1. Open Command Prompt as Administrator:

    • Press Windows + S, type Command Prompt, right-click, and select Run as Administrator.
  2. Stop MongoDB:

    • In the Administrator Command Prompt, run:
      net stop MongoDB
  3. Start MongoDB:

    • Once MongoDB is stopped, run:
      net start MongoDB

Step 7: Log In with the Created User

  1. 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.
  2. Enjoy MongoDB with Authentication Enabled!

---bash

Notes:

  • 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 in mongosh -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!

About

This guide will help you install and configure MongoDB with authentication and how to set up a user with a password.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published