Rabikant

Posted on November 15th

How to install MongoDB on Ubuntu

"Let's learn How to install MongoDB on Ubuntu"

Introduction to MongoDB

Platforms that anyone can easily identify include the MongoDB, which happens to be a NoSQL database management system, which is not only well-known for flexibility, scalability, and performance. This is in contrast with classic RDBMS that uses tables and rows to implement relations between the data MongoDB stores data as flexible JSON-like documents. It makes mongoDB a perfect contender for applications that need to store a large amount of unstructured or semi-structured data, availability, and horizontal scalability.

In this article we will explain how to install MongoDB on an Ubuntu system, what procedures are necessary for this task. During the installation process one needs to set up the right repository, import correct public keys, and execute the installation commands. Also, we will so how to start MongoDB, check for the installation and auto run of MongoDB on boot.

Import the MongoDB GPG Public Key

The first step in installing MongoDB is to import the GPG public key used by MongoDB to sign its packages. GPG (GNU Privacy Guard) keys are essential for ensuring that the software packages you install come from a trusted source and have not been tampered with.

To import the MongoDB GPG key, open your terminal and execute the following command:

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | 
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg 
--dearmor

Create the MongoDB Source List

After adding the GPG key, the next step is to create a source list file that contains the repository information for MongoDB. This source list directs your package manager to the correct location where the MongoDB packages are hosted.

To create the source list file, run the following command in your terminal:

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

Update the Packages List

With the MongoDB repository added to your system, the next step is to update the package list. Updating the package list ensures that your package manager is aware of the new software available from MongoDB.

To update the package list, run the following command:

sudo apt-get update

Breaking Down the Command:

  • sudo apt-get update: This command instructs your package manager to fetch the latest package lists from all configured repositories, including the newly added MongoDB repository. The sudo command is used to grant the necessary privileges to update the package list.

During this step, the package manager compares the list of available packages to the ones currently installed on your system. If new versions are available, they will be noted. However, no software is installed or upgraded during this step; it merely updates the package index.

Install MongoDB

Now that your system is configured to find the MongoDB packages, you can proceed with the installation of MongoDB. The following command will install MongoDB on your system:

sudo apt-get install -y mongodb-org

Breaking Down the Command:

sudo apt-get install: This command tells your package manager to install the package(s) of your choice. To do the installation, the sudo command provides the required permission level.

-y: This option is for accepting all the options that may appear during the installation process of the software through pressing a ‘yes’ key. This is especially helpful in order to be able to guarantee an installation that does not require manual intervention, which is very useful in script environments and useful when you are sure that is the option that you want for your installation.

mongodb-org: This is basically the meta-package for MongoDB. A ‘meta-package’, on the other hand does not contain software for installation, but points to a set of related packages that should be installed. In this case, installing mongodb-org installs the following packages:In this case, installing mongodb-org installs the following packages:

mongodb-org-server: The most important server of the basic data storage.

mongodb-org-mongos: The MongoDB routing service deployed in MongoDB sharded cluster.

mongodb-org-shell: MongoDB shell, this is actually a command prompt where you will interact with your MongoDB database.

mongodb-org-tools: Suite of programs to deal with MongoDB databases and data, including to load into MongoDB, export from MongoDB and backup tools.

By the end of this step, MongoDB will be installed on your system, but it will not be running yet.

Start MongoDB

Once MongoDB is installed, the next step is to start the MongoDB service. Starting the service allows MongoDB to begin accepting connections and performing database operations.

To start the MongoDB service, execute the following command:

sudo systemctl start mongod

Breaking Down the Command:

  • sudo systemctl start mongod: The systemctl command is used to interact with systemd, the system and service manager for Linux. In this case, it starts the MongoDB daemon (mongod). The sudo command is used to grant the necessary privileges to start the service.

When you start mongod, the MongoDB server is launched, allowing it to begin processing database operations and accepting client connections.

Verify the Installation

To ensure that MongoDB is running correctly, it’s important to check the status of the MongoDB service. This can be done with the following command:

sudo systemctl status mongod

Breaking Down the Command:

  • sudo systemctl status mongod: This command checks the current status of the MongoDB service. It provides information about whether the service is active (running) and displays recent log messages from the service. The sudo command is used to grant the necessary privileges to check the service status.

When you run this command, look for the following key indicators:

  • active (running): This indicates that MongoDB is up and running without any issues.
  • Log messages: The output also includes the last few lines of the MongoDB service log, which can be helpful for diagnosing any problems if MongoDB isn't starting correctly.

If MongoDB is running as expected, you can proceed to the next step.

Enable MongoDB to Start on Boot

To ensure that MongoDB automatically starts whenever your system boots, you can configure MongoDB to start on boot. This is particularly useful for production environments, where you want to minimize downtime and ensure that your database is always available.

To enable MongoDB to start on boot, execute the following command:

sudo systemctl enable mongod

Breaking Down the Command:

  • sudo systemctl enable mongod: This command tells systemd to automatically start the MongoDB service whenever the system boots. It does this by creating a symbolic link in the appropriate directory that includes the MongoDB service in the list of services to be started during the boot process. The sudo command is used to grant the necessary privileges to modify the system's startup configuration.

Enabling MongoDB to start on boot ensures that your database will be running automatically every time your server is restarted, minimizing potential downtime and ensuring that your applications that rely on MongoDB are always connected to the database.

Access the MongoDB Shell

Once MongoDB is installed and running, you can interact with your MongoDB instance using the MongoDB shell (mongo). The shell provides a command-line interface to interact with your database, allowing you to perform various database operations such as querying data, updating documents, and managing collections.

To access the MongoDB shell, run the following command:

mongo

Breaking Down the Command:

  • mongo: This command launches the MongoDB shell, connecting to the MongoDB server running on your local machine by default. If your MongoDB server is running on a different machine or a non-standard port, you can specify the connection details with the mongo command (e.g., mongo --host <hostname> --port <port>).

Once inside the MongoDB shell, you can start executing commands to manage your MongoDB databases. For example, you can create a new database, insert documents intxo a collection, query for data, and more.

Basic MongoDB Shell Commands:

Here are a few basic commands you can run in the MongoDB shell to get started:

  • Show Databases:

    show dbs
    

    This command lists all the databases on your MongoDB server.

  • Switch to a Database:

    use mydatabase
    

    This command switches to the specified database (mydatabase). If the database does not exist, MongoDB will create it when you insert data.

  • Insert a Document:

    db.mycollection.insert({ name: "John Doe", age: 30 })
    

    This command inserts a document into the specified collection (mycollection). If the collection does not exist, MongoDB will create it.

  • Exit the Shell:

    exit
    

    This command exits the MongoDB shell.

Uninstall MongoDB (Optional)

If, for any reason, you need to uninstall MongoDB from your system, you can do so by following these steps. First, stop the MongoDB service:

sudo systemctl stop mongod

Next, remove the installed MongoDB packages:

sudo apt-get purge mongodb-org*

Finally, remove any MongoDB-related directories:

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

Breaking Down the Commands:

  • sudo apt-get purge mongodb-org*: This command removes all MongoDB packages from your system. The `` wildcard ensures that all packages associated with MongoDB are removed.
  • sudo rm -r /var/log/mongodb and sudo rm -r /var/lib/mongodb: These commands delete the MongoDB log and data directories, respectively. The r option ensures that the directories are removed recursively.

Uninstalling MongoDB will completely remove all MongoDB software and data from your system. Be sure to back up any important data before proceeding with the uninstallation.

Conclusion

MongoDB is a powerful and flexible NoSQL database management system that can handle a wide range of data storage needs. By following the steps outlined in this guide, you can install MongoDB on an Ubuntu system, start the MongoDB service, and begin interacting with your MongoDB databases using the MongoDB shell.

We've covered how to:

  1. Import the MongoDB GPG public key.
  2. Create the MongoDB source list.
  3. Update the package list.
  4. Install MongoDB.
  5. Start the MongoDB service.
  6. Verify the installation.
  7. Enable MongoDB to start on boot.
  8. Access the MongoDB shell.

By ensuring that MongoDB starts automatically on boot and understanding the basic commands in the MongoDB shell, you can maintain a reliable and accessible database environment for your applications. Whether you're developing a small-scale application or managing a large-scale distributed system, MongoDB offers the scalability and flexibility needed to meet your data management needs.

Remember that MongoDB’s rich ecosystem of tools and community support makes it a great choice for modern, data-intensive applications. As you continue to work with MongoDB, you’ll discover more advanced features and configurations that can further optimize your database performance and management.

Comments

Leave a comment.

Share your thoughts or ask a question to be added in the loop.