Skip to main content

Setting up MongoDB environment

MongoDB is an open-source NoSQL database that provides high performance, high availability, and easy scalability. It works on the concept of collections and documents, making it a popular choice for many developers. Before we can dive into the specifics of MongoDB, we first need to set up the MongoDB environment on our systems. This tutorial guides you through the process of setting up the MongoDB environment on your system.

Prerequisites

Before you start the installation process, ensure you have an active internet connection and administrative access to your system. MongoDB supports both Windows and Linux operating systems, so you can choose the one that suits your needs.

Downloading MongoDB

The first step in setting up MongoDB is to download the MongoDB server. Visit the MongoDB official website and navigate to the Downloads section. There, you can download MongoDB by following these steps:

  1. Select the version you want to download. It's usually best to stick with the recommended version.
  2. Choose your operating system.
  3. Choose the package you want to download. For beginners, the 'Server' package is usually the best option.
  4. Click the 'Download' button.

Installing MongoDB

For Windows

Once the download is complete, locate the installer file in your system and double-click to start the installation process. Follow these steps:

  1. Click on the 'Next' button in the setup wizard.
  2. Accept the End-User License Agreement and proceed.
  3. Choose 'Complete' to install all of the components.
  4. On the next screen, choose 'Run service as Network Service user'.
  5. Check 'Install MongoDB Compass'. MongoDB Compass is a GUI for MongoDB.
  6. Click on the 'Next' button, then 'Install' to start the installation.

For Linux

For Linux systems, you can use the package management system that comes with your distribution. Here's how you can install MongoDB on Ubuntu:

  1. Open a terminal.
  2. Import the public key used by the package management system. The Ubuntu package management tools ensure the consistency and authenticity of the software package.
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
  1. Create a list file for MongoDB.
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
  1. Reload local package database.
sudo apt-get update
  1. Install the MongoDB packages.
sudo apt-get install -y mongodb-org

Running MongoDB

For Windows

After the installation, you can start MongoDB by following these steps:

  1. Open the Command Interpreter (cmd.exe).
  2. Run the following command:
"C:\Program Files\MongoDB\Server\4.4\bin\mongod.exe"

For Linux

For Linux systems, use the following commands to start, stop, and restart MongoDB:

sudo service mongod start
sudo service mongod stop
sudo service mongod restart

Conclusion

That's it! You have successfully installed and set up MongoDB on your system. You can now start building your MongoDB database. Remember, the best way to learn is by doing. So, start practicing what you've learned and explore the various features MongoDB offers. Happy coding!