codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

3 / 65 topics
1Introduction to MongoDB2MongoDB Architecture3Installation and Setup4Connecting to MongoDB5MongoDB Shell Basics
Tutorials/MongoDB/Installation and Setup
🍃MongoDB

Installation and Setup

Updated 2026-04-20
3 min read

Installation and Setup

MongoDB is a popular NoSQL database that stores data in flexible, JSON-like documents. This tutorial will walk you through the installation and setup process for MongoDB on various operating systems, including Windows, macOS, and Linux. We'll also cover some best practices to ensure your MongoDB instance is secure and optimized.

Prerequisites

Before installing MongoDB, ensure your system meets the following requirements:

  • Operating System: MongoDB supports Windows, macOS, and Linux.
  • Hardware Requirements: At least 2GB of RAM for a development environment.
  • Software Requirements: Ensure you have administrative privileges on your machine.

Installation Steps

On Windows

  1. Download MongoDB:

    • Visit the MongoDB Download Center and select the appropriate version for Windows.
    • Choose the installer package (.msi) for a standard installation or the zip file for a custom installation.
  2. Install MongoDB:

    • Run the downloaded .msi file and follow the installation wizard.
    • During installation, you can choose to install MongoDB Compass, a GUI tool for managing your databases.
  3. Configure Environment Variables:

    • Add C:\Program Files\MongoDB\Server\<version>\bin to your system's PATH environment variable. This allows you to run MongoDB commands from any command prompt.
  4. Create Data Directory:

    • Create a directory for the database files, e.g., C:\data\db.
  5. Start MongoDB Server:

    • Open Command Prompt and run:
      mongod --dbpath "C:\data\db"
      
    • This command starts the MongoDB server with the specified data directory.
  6. Connect to MongoDB:

    • Open another Command Prompt window and run:
      mongo
      
    • This connects you to the running MongoDB instance.

On macOS

  1. Install Homebrew (if not already installed):

    • Homebrew is a package manager for macOS.
    • Run in Terminal:
      /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
      
  2. Install MongoDB:

    • Use Homebrew to install MongoDB:
      brew tap mongodb/brew
      brew install mongodb-community@6.0
      
  3. Start MongoDB Server:

    • Start the MongoDB service:
      brew services start mongodb-community@6.0
      
    • Alternatively, you can start it manually with:
      mongod --config /usr/local/etc/mongod.conf
      
  4. Connect to MongoDB:

    • Open another Terminal window and run:
      mongo
      

On Linux

  1. Import the Public Key:

    • Import the MongoDB public GPG key:
      wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
      
  2. Create a List File for MongoDB:

    • Create a list file for MongoDB:
      echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
      
  3. Reload the Package Database:

    • Reload the package database with the new MongoDB repository:
      sudo apt-get update
      
  4. Install MongoDB:

    • Install the latest version of MongoDB:
      sudo apt-get install -y mongodb-org
      
  5. Start MongoDB Server:

    • Start the MongoDB service:
      sudo systemctl start mongod
      
    • Enable MongoDB to start at boot:
      sudo systemctl enable mongod
      
  6. Connect to MongoDB:

    • Open another Terminal window and run:
      mongo
      

Best Practices

Security Configuration

  1. Enable Authentication:

    • Edit the mongod.conf file to enable authentication:
      security:
        authorization: "enabled"
      
    • Restart MongoDB to apply changes.
  2. Create Admin User:

    • Connect to MongoDB and create an admin user:
      use admin
      db.createUser({
        user: "admin",
        pwd: "password",
        roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
      })
      
  3. Secure Bind IP:

    • Bind MongoDB to a specific IP address to restrict access:
      net:
        bindIp: 127.0.0.1
      

Performance Optimization

  1. Adjust WiredTiger Cache Size:

    • Set the WiredTiger cache size in mongod.conf:
      storage:
        wiredTiger:
          engineConfig:
            cacheSizeGB: 2
      
  2. Enable Journaling:

    • Ensure journaling is enabled for data durability:
      storage:
        journal:
          enabled: true
      
  3. Monitor and Tune Performance:

    • Use MongoDB's built-in monitoring tools to track performance metrics and make necessary adjustments.

Conclusion

MongoDB provides a flexible and powerful platform for storing and managing unstructured data. By following the installation steps outlined in this tutorial, you can set up a MongoDB instance on your preferred operating system. Remember to apply best practices for security and performance optimization to ensure your MongoDB deployment is robust and efficient.

This completes the "Installation and Setup" section of our MongoDB course. Next, we will explore the basics of working with data in MongoDB.


PreviousMongoDB ArchitectureNext Connecting to MongoDB

Recommended Gear

MongoDB ArchitectureConnecting to MongoDB