Published on October 18, 2024By DeveloperBreeze

Tutorial: How to Install MongoDB on Ubuntu

MongoDB is a popular NoSQL database widely used for modern web applications due to its flexibility and scalability. In this guide, we'll walk through the process of installing MongoDB on an Ubuntu machine. We'll be using MongoDB 6.0 for this installation.

Step 1: Update Your Package List

Before installing MongoDB, make sure your system's package list is up to date. Open a terminal and run the following command:


sudo apt-get update

Step 2: Add the MongoDB GPG Key

To verify the integrity of the packages you download, MongoDB provides a GPG key. Add this key to your system by running the following command:


curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-server-6.0.gpg

This downloads the MongoDB GPG key and stores it in your system's trusted keyring.

Step 3: Add the MongoDB Repository

Now that the GPG key is added, we can configure Ubuntu to pull MongoDB packages from the official MongoDB repository.

Run the following command to add the MongoDB 6.0 repository for Ubuntu 22.04 (Jammy):


echo "deb [ arch=amd64,arm64 signed-by=/etc/apt/trusted.gpg.d/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

If you're using a different version of Ubuntu, replace jammy with the codename of your Ubuntu version (e.g., focal for 20.04, bionic for 18.04).

Step 4: Update Your Package List Again

Since we've added a new repository, we need to update the package list again to include MongoDB packages:


sudo apt-get update

Step 5: Install MongoDB

Now we can install MongoDB and its related components by running the following command:


sudo apt-get install -y mongodb-org

This will install the following MongoDB components:

  • mongodb-org-server: The MongoDB server daemon.
  • mongodb-org-mongos: A routing service for MongoDB sharding.
  • mongodb-org-shell: The MongoDB command-line shell.
  • mongodb-org-tools: Utilities for importing, exporting, and other database management tasks.

Step 6: Start and Enable MongoDB

Once the installation is complete, you can start the MongoDB service:


sudo systemctl start mongod

To ensure that MongoDB starts automatically on system boot, enable the MongoDB service:


sudo systemctl enable mongod

Step 7: Verify MongoDB Installation

You can check if MongoDB is running properly by checking the status of the mongod service:


sudo systemctl status mongod

You should see an output indicating that MongoDB is active and running. You can also connect to the MongoDB shell to ensure it's working:


mongosh

If MongoDB is running correctly, you'll be dropped into the MongoDB shell.

Step 8: Allow Remote Connections (Optional)

By default, MongoDB only listens to localhost. If you need MongoDB to be accessible from another machine, you'll need to modify the MongoDB configuration file:

  1. Open the MongoDB configuration file:

   sudo nano /etc/mongod.conf
  1. Look for the bindIp line under the net section. By default, it looks like this:

   bindIp: 127.0.0.1

Change it to:


   bindIp: 0.0.0.0

This allows MongoDB to listen on all IP addresses.

  1. Save and exit the file, then restart MongoDB:

   sudo systemctl restart mongod

Note: Opening MongoDB to external connections can pose security risks. Be sure to configure appropriate firewall rules and authentication.

Step 9: Basic MongoDB Usage

Here are a few common MongoDB commands to get you started:

  • View Databases:

  show dbs
  • Create/Use a Database:

  use mydatabase
  • Insert a Document:

  db.mycollection.insertOne({ name: "John Doe", age: 30 })
  • View Documents in a Collection:

  db.mycollection.find()

Conclusion

That's it! You have successfully installed MongoDB on your Ubuntu machine. MongoDB is now up and running, ready to store and manage your data. You can now explore more advanced features like replication, sharding, or connecting MongoDB to your web applications.

Comments

Please log in to leave a comment.

Continue Reading:

How to Install MongoDB Shell (mongosh) on Ubuntu

Published on October 18, 2024

bash