Lecture Note Of Day 2 - Installing MongoDB

Rashmi Mishra
0

 PREVIOUS                                                                              NEXT

Lecture Note Of Day 2 

 Installing MongoDB


Objective:

By the end of this class, students will be able to install MongoDB on different platforms such as Windows, macOS, and Linux. Additionally, they will learn how to configure MongoDB and ensure it's running properly.


Introduction to MongoDB Installation

MongoDB is a popular, open-source NoSQL database that provides high performance, high availability, and easy scalability. Installing MongoDB is the first step to begin using it for application development. We will cover how to install MongoDB on various platforms and ensure that it's correctly set up.


Prerequisites

  • An internet connection to download the MongoDB installer
  • Basic understanding of using the command line (Terminal or Command Prompt)
  • Administrative privileges on the system

Installation Steps for MongoDB

1. Installing MongoDB on Windows

Step 1: Download MongoDB

  • Visit the MongoDB download page: MongoDB Download Center
  • Choose the Windows version of MongoDB and click Download.
  • The installer will be in the form of an .msi file.

Step 2: Run the Installer

  • After downloading, double-click the .msi file to begin the installation.
  • Follow the installation wizard:
    • Select Complete for the full installation.
    • Choose Install MongoDB as a Service to ensure MongoDB runs automatically when the computer starts.
    • Choose Install MongoDB Compass (optional, for GUI-based interaction with MongoDB).

Step 3: Add MongoDB to System PATH

  • Open the System Properties window by typing sysdm.cpl in the Run dialog (Win + R).
  • In the Advanced tab, click on Environment Variables.
  • Under System Variables, find the Path variable and click Edit.
  • Add the path to MongoDB’s bin directory (e.g., C:\Program Files\MongoDB\Server\5.0\bin) to the system PATH.
  • Click OK to save the changes.

Step 4: Start MongoDB

  • Open the Command Prompt and type mongod to start the MongoDB server.
  • In a new Command Prompt window, type mongo to start the MongoDB shell and connect to the server.

2. Installing MongoDB on macOS

Step 1: Install Homebrew (if not already installed)

  • Open the Terminal and run the following command to install Homebrew (a package manager for macOS):

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Step 2: Install MongoDB using Homebrew

  • Run the following command to install MongoDB:

brew tap mongodb/brew

brew install mongodb-community@5.0

Step 3: Start MongoDB

  • To start the MongoDB service, run the following command:

brew services start mongodb/brew/mongodb-community

Step 4: Verify Installation

  • Open the terminal and run:

mongo

  • This should connect you to the MongoDB shell, indicating that MongoDB is running correctly.

Step 5: Stop MongoDB

  • If you want to stop MongoDB, run:

brew services stop mongodb/brew/mongodb-community


3. Installing MongoDB on Linux

Step 1: Install MongoDB using APT (Debian/Ubuntu-based Linux distributions)

  • Open the Terminal and update your system's package list:

sudo apt update

  • Install the necessary dependencies:

sudo apt install -y gnupg

  • Add MongoDB's official repository:

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

  • Add the MongoDB repository to your package manager's list:

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

  • Update the package list:

sudo apt update

Step 2: Install MongoDB

  • Now install MongoDB:

sudo apt install -y mongodb-org

Step 3: Start MongoDB

  • To start the MongoDB service, run:

sudo systemctl start mongod

  • To enable MongoDB to start automatically on boot, run:

sudo systemctl enable mongod

Step 4: Verify Installation

  • Run the following command to connect to the MongoDB shell:

mongo

  • If you see the MongoDB shell, the installation was successful.

Step 5: Stop MongoDB

  • To stop MongoDB, run:

sudo systemctl stop mongod


Configuration Tips

1.  MongoDB Configuration File: MongoDB's configuration file is usually located at /etc/mongod.conf on Linux, /usr/local/etc/mongod.conf on macOS, and C:\Program Files\MongoDB\Server\<version>\bin\mongod.cfg on Windows. This file can be used to adjust various settings, such as port, bind IP address, and log file location.

2.  Check MongoDB Logs: MongoDB logs can be useful for troubleshooting installation issues. The default log location is /var/log/mongodb/mongod.log on Linux and macOS, and C:\Program Files\MongoDB\Server\<version>\log\mongod.log on Windows.

3.  Security Configuration: By default, MongoDB does not have authentication enabled. For a production environment, it's important to enable authentication by modifying the MongoDB configuration file (mongod.conf), setting security.authorization to enabled, and creating user roles for access control.

4.  Run MongoDB as a Service: It’s advisable to run MongoDB as a background service to ensure it is running even after system reboots. On Linux, this is done with systemctl. On Windows, the MongoDB installer provides an option to install MongoDB as a service.


Conclusion

In this lesson, we've learned how to install MongoDB on different platforms, including Windows, macOS, and Linux. We also explored the basics of MongoDB's configuration and startup processes. This is the first step in working with MongoDB, and with it, you can begin to interact with MongoDB using the shell and start building applications that leverage the power of this NoSQL database.

Assignments for Practice

1.  Install MongoDB on your system (Windows/macOS/Linux).

2.  Start the MongoDB server and access the MongoDB shell.

3.  Create a configuration file for MongoDB and modify its settings.

4.  Verify the MongoDB installation by inserting a sample document into a new collection and retrieving it.

5.  Explore the MongoDB logs and explain what information can be found in the logs.




Tags

Post a Comment

0Comments

Post a Comment (0)

About Me