How to Install MongoDB on Your Local System

How to Install MongoDB on Your Local System

MongoDB is a popular NoSQL database known for its flexibility and scalability. Installing MongoDB on your local system allows you to develop, test, and explore its features in a controlled environment. Here’s a step-by-step guide to install MongoDB on your local machine.

Step 1: Check Prerequisites

  • Supported Operating Systems
    • MongoDB is available for Windows, macOS, and Linux. Ensure your OS is supported by the version you’re installing.
  • System Requirements
    • Minimum 2GB of RAM (4GB or more recommended).
    • Sufficient disk space for data storage.
  • Administrator Privileges
    • You’ll need administrative rights on your computer to install MongoDB.

Step 2: Download MongoDB

  • Go to the MongoDB Download Center.
  • Select your operating system.
  • Choose the latest MongoDB Community Server version.
  • Download the appropriate installer or package for your system.

Step 3.1: Install MongoDB in Windows

  • Run the Installer
    • Double-click the .msi file you downloaded to start the installation.
  • Follow the Setup Wizard
    • Accept the license agreement.
    • Select Complete installation.
  • Choose Configuration Options
    • Keep the default settings for the Service Configuration.
    • Ensure the Install MongoDB Compass option is selected (optional graphical interface).
  • Complete Installation
    • Click Install and wait for the process to finish.
  • Add MongoDB to Path
    • To use MongoDB commands globally, add the MongoDB binary directory to your system’s PATH environment variable.
C:\Program Files\MongoDB\Server\<version>\bin

Step 3.2: For macOS

  • Install via Homebrew
    • Open the Terminal and run:
brew tap mongodb/brew brew install mongodb-community@<version>
  • Replace <version> with the desired version (e.g., 6.0).

  • Verify Installation
    • Check the version by running:
mongod --version
  • Start MongoDB Service
    • Start MongoDB using Homebrew services:
brew services start mongodb/brew/mongodb-community

Step 3.3: For Linux

  • Install via Package Manager
    • Import MongoDB’s public key:
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
  • Create a list file for MongoDB in /etc/apt/sources.list.d
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
  • Update package listings:
sudo apt-get update
  • Install MongoDB:
sudo apt-get install -y mongodb-org
  • Start MongoDB
    • Run the MongoDB service:
sudo systemctl start mongod
  • Enable MongoDB to start on boot:
sudo systemctl enable mongod
  • Verify Installation
    • Open a terminal or command prompt.
    • Run: mongod --version This command displays the installed MongoDB version.
    • To connect to the database, start the MongoDB shell by typing: mongosh

Step 4: MongoDB Compass (Optional)

  • MongoDB Compass is a GUI tool for managing your databases visually.
  • If you selected the Compass installation option during setup, launch it after installation.
  • Connect to your local MongoDB instance using the default URI:
mongodb://localhost:27017

Step 5: Start Using MongoDB

  • Create a database:
use myDatabase
  • Insert a document:
db.myCollection.insertOne({ name: "coderanil", age: 30 })
  • Query data:
db.myCollection.find()

Step 6: Common Troubleshooting

  • Service Not Starting
    • Ensure the MongoDB service is running:
sudo systemctl status mongod
  • Check the log file (/var/log/mongodb/mongod.log) for errors.
  • Port Conflicts
    • By default, MongoDB uses port 27017. Ensure it’s not blocked or used by another application.
  • Permission Issues
    • Run installation and service commands with administrator or sudo privileges.

With MongoDB installed locally, you’re now ready to dive into database development.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *