MongoDB is one of the most popular document-oriented database systems. He moved in just a few steps under Ubuntu LTS version 20.04. Complete installation is quick and easy from terminal.
Install MongoDB on Ubuntu: What You Need
To install MongoDB on Ubuntu, the prerequisites are simple: you will need to know the basics of the main Linux commands and, of course, Ubuntu as an operating system. The current version of Ubuntu, namely version 22.04, does not yet offer the official support necessary for installing MongoDB (status: October 2022). It is therefore recommended to use Ubuntu 20.04 for installing the NoSQL database management system. This version of Ubuntu also gives you long-term support. Additionally, installing MongoDB only works on 64-bit operating systems. So be sure to check the version beforehand and install the required one if necessary.
Proceed to Install MongoDB on Ubuntu 20.04
Step 1: Import the MongoDB key
The first step is to import the MongoDB GPG public key. To do this, first open the terminal. Next, enter the following command to download the key for the current MongoDB version 6.0:
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
You will then be asked to enter your password. After confirming, the import process should normally go smoothly. However, it may be that the GNU Privacy Guard (GnuPG) is not yet installed on your system. In this case, an error message appears. To fix this, just install the program using the following terminal command:
sudo apt-get install gnupg
Then run the import command mentioned above again. The MongoDB GPG public key should now download successfully.
Step 2: Creating the list file
The next step is to create the list file corresponding to the Ubuntu version of your device. You can also use the terminal for this:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
In order for the changes to be applied, you will then need to restart your system. This will add the MongoDB repository to your system. This process may take a few minutes.
Step 3: Install MongoDB Packages
The next step is to install the necessary packages for the version of MongoDB you want to run. In most cases, it is best to choose the current version from MongoDB. The following command is then sufficient for the installation:
sudo apt-get install -y mongodb-org
Note
Be sure to choose the correct package when installing MongoDB and install « mongodb-org ». The ‘mongodb’ package provided by Ubuntu is unofficial and should not be used if you follow our instructions. If you have already installed this second package, you will then need to uninstall it for the procedure presented here to work without problems.
You wish install a specific version of MongoDB ? So be sure to manually determine the version number you want for each package. For example, if you want to install MongoDB version 6.0.1, use the following command:
sudo apt-get install -y mongodb-org=6.0.1 mongodb-org-database=6.0.1 mongodb-org-server=6.0.1 mongodb-mongosh=6.0.1 mongodb-org-mongos=6.0.1 mongodb-org-tools=6.0.1
After a few minutes, the installation process completes and MongoDB is officially installed.
Starting MongoDB
Once you have successfully installed the NoSQL database, the following command will start the system :
sudo systemctl start mongod
It may happen that an error occurs during the first start. In this case, it is necessary to reload all configuration files and reboot all units in your system :
sudo systemctl daemon-reload
MongDB should now start without issue. To ensure that the database started correctly, you can at any time consult theMongoDB status (MongoDB status) using the following terminal command:
sudo systemctl status mongod
Stop or restart MongoDB
A terminal command also allows thestop from MongoDB:
sudo systemctl stop mongod
THE reboot from the database works in a similar way:
sudo systemctl restart mongod
In both of these cases, the command to view the status of MongoDB presented above will allow you to check if your operations have been executed successfully.
Check Port
Before starting the Mongosh shell, you must first check if MongoDB is running on the correct port: this is the port 27017 by default. You can view open ports by entering the following command in the terminal:
Launch Mongosh
Entering this command will allow you to start Mongosh:
In the MongoDB Shell, you can, for example, add new users or assign new functions to your database. You can find helpful tips on this in our MongoDB tutorial.
Web hosting with personal advisor!
Powerful, flexible and high-performance web hosting with mailbox, personal advisor and domain included 1time year !
Uninstall MongoDB
There are many open source data centers out there, so you may end up preferring an alternative solution. You will then need to uninstall MongoDB: uninstalling is as quick and easy as installing. However, you should know that it generates the deletion of all databases and stored data.
Step 1: Stop MongoDB
Stop MongoDB with the following command:
Step 2: uninstall packages
You can uninstall any packages you previously installed by running a terminal command:
sudo apt-get purge mongodb-org*
Step 3: Delete databases and log files
Complete the uninstall process by removing all databases that you have created as well as all log files. The following command terminates the process:
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb