· Tutorial ·

How to install MongoDB on your Linux server

In this manual we will show you how to install MongoDB on your Linux server so you can start using this NoSQL database. However, first we will review some of the concepts that define this database management system.

What is MongoDB?

MongoDB is the name given to one of the most popular database management systems in recent years. It is also a fundamental piece of the well-known "MEAN Stack": a set of technologies formed by MongoDB, Nginx, Angular and NodeJS. And increasingly, companies are adopting it as their preferred choice for web application development.

One of the most important aspects to keep in mind about MongoDB is that it is a NoSQL database management system, so its operation is drastically different from MySQL, MariaDB or PostgreSQL.

What does NoSQL mean?

The term "No SQL" or "Non-relational" means that the database does not use tables to store entries or relate the various stored entries to each other. Instead, the entries are stored as separate objects, often in JSON format.

NoSQL databases greatly facilitate horizontal scalability, allowing the database to be distributed across multiple servers. Unlike a relational database, server synchronization latency is not a problem: collections (similar to tables) may be out of sync for brief moments, but the integrity of the stored objects is maintained at all times.

The flexibility of NoSQL databases makes them ideal for applications with large volumes of data or real-time web applications.

How to install MongoDB on Linux

info In this manual we provide the installation steps for the Ubuntu 18.04 distribution. For other distributions, especially RedHat, CentOS or Suse, the steps to follow may be different. Superuser permissions are required to follow the steps in this manual.

1. Import the public key from the MongoDB repository

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

This key is required to verify the authorship of installed packages. It is a standard and very common procedure when adding a repository.

2. We add the MongoDB repository

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

The above command adds the repository to a .list file. This is the procedure recommended by the MongoDB developers.

3. Update the repository list

apt-get update

The above command will update the list of repositories and available packages, including the new MongoDB repository we added in the previous step.

4. Install MongoDB using the package manager

apt-get install mongodb-org

info The package name is "mongodb-org" and not "mongodb". This package comes from the official repositories we have added which offers the latest stable version of MongoDB. The package offered by the Ubuntu repositories might be outdated.

5. Disable automatic updates to avoid compatibility issues (optional)

A recommended practice is to disable automatic updates that could lead to compatibility problems. It is better to install updates manually.

To disable updates, enter each of the following commands separately:

echo "mongodb-org hold" | dpkg --set-selections
echo "mongodb-org-server hold" | dpkg --set-selections
echo "mongodb-org-shell hold" | dpkg --set-selections
echo "mongodb-org-mongos hold" | dpkg --set-selections
echo "mongodb-org-tools hold" | dpkg --set-selections

In the future, to re-enable automatic updates:

echo "mongodb-org install" | dpkg --set-selections
echo "mongodb-org-server install" | dpkg --set-selections
echo "mongodb-org-shell install" | dpkg --set-selections
echo "mongodb-org-mongos install" | dpkg --set-selections
echo "mongodb-org-tools install" | dpkg --set-selections

success You are done! If you have followed the instructions correctly you will have successfully installed the latest official version of MongoDB.

i