In this manual, we will show you how to install MongoDB on your Linux server so you can start using this NoSQL database. Before, we will review some of the concepts that define this database management system.
MongoDB is the name given to one of the most popular database management systems in recent years. In addition, it is a fundamental piece of the well-known "MEAN Stack": a set of technologies formed by MongoDB, Nginx, Angular and NodeJS and that more and more companies are adopting as their preferred option for the development of web applications.
One of the most important aspects to take into account of MongoDB is that it is a management system of NoSQL Databases, so its operation is drastically different from MySQL, MariaDB or PostgreSQL.
The term "No SQL" or "Non-relational" means that the database does not use tables to store entries or relate the different 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 among multiple servers. Unlike a relational database, the latency in the synchronization of the servers is not a problem: the collections (simile tables) can be out of sync for a few moments but at all times the integrity of the stored objects is maintained.
The flexibility of NoSQL databases makes them ideal for applications with large volumes of data or web applications in real time.
💡 Attention: In this manual we offer the installation steps for the Ubuntu 18.04 distribution. For other distributions, especially RedHat, CentOS or Suse, the steps to follow may be different.
It is necessary to have superuser's permissions to follow the steps of this manual.
1. Imports the public key of the MongoDB repository
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
This key is necessary to verify the authorship of the installed packages. It is a standard procedure and very common 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 developers of MongoDB.
3. Update the list of repositories
The previous command will update the list of repositories and available packages, including the new MongoDB repository that we added in the previous step.
4. Install MongoDB using the package manager
apt-get install mongodb-org
💡 Attention: The name of the package is "mongodb-org" and not "mongodb". This package comes from the official repositories that we have added and that offers the latest stable version of MongoDB. The package offered by the Ubuntu repositories may be outdated.
5. Disable automatic updates to avoid compatibility problems (optional)
A recommended practice is to disable automatic updates that could cause compatibility problems. It is better to install the 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 reactivate 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
If you have followed the instructions correctly, you will have installed the latest official version of MongoDB.