MongoDB is a free and opensource No SQL database written in C++ and used in modern web application development. This tutorial will give you a great understanding of how to install MongoDB on Ubuntu 18.04 LTE.
Before installing MongoDB, we need to ensure that the server is up-to-date. So, let’s first update and upgrade the server:
sudo apt-get update && apt-get upgrade –y
Steps to Install MongoDB on Ubuntu
Step 1 — Adding the MongoDB Repository
MongoDB is included in Ubuntu Package repository by default, but we will use official MongoDB repository since it provides the most up-to-date version of the MongoDB.
So, we will add the official MongoDB repository to our server.
Ubuntu verifies that software packages are signed with GPG keys to ensure the authenticity of the packages. So, we need to first import the key for the official MongoDB repository to ensure it’s authenticity. For that we need to run the following command:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
Once the keys are imported successfully, you get to see the output:
gpg: Total number processed: 1 gpg: imported: 1 (RSA: 1)
Now we need to add MongoDB repository details to let advance packaging tool (APT) know the location on the internet from where the
To add MongoDB repository, run the following command:
echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
Once done, update the package list by running the following command:
sudo apt-get update
Step 2 — Installing MongoDB
Since we are done with all the major configuration we can proceed further and install MongoDB on Ubuntu. To install MongoDB, run the following command:
sudo apt-get install -y mongodb-org
The command above will install all the latest stable packages required to run the MongoDB No SQL database on Ubuntu Server.
Now let’s start the MongoDB server using
sudo systemctl start mongod
Run the following command to check whether the MongoDB service started successfully or not:
sudo systemctl status mongod
If everything works properly, you get to see the following output:
● mongodb.service - High-performance, schema-free document-oriented database Loaded: loaded (/etc/systemd/system/mongodb.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2019-02-08 19:27:20 EDT; 1min 30s ago Main PID: 4093 (mongod) Tasks: 16 (limit: 512) Memory: 47.1M CPU: 1.224s CGroup: /system.slice/mongodb.service └─4093 /usr/bin/mongod --quiet --config /etc/mongod.conf
Now we need to enable the MongoDB that will allow the service to start automatically every time the server is restarted. It can be achieved by issuing the following command:
sudo systemctl enable mongod
Step 3 — Adjusting the Firewall
This is completely optional if you want to adjust your firewall. But if you run your web application and database on the same server, it is highly recommended that you adjust your firewall.
I assume you have completed your initial server setup and enabled the firewall on your server.
The first and the foremost thing is to allow access to the
MongoDB on its default port
To allow that run the following command:
sudo ufw allow 27017
To allow access to the MongoDB server from the trusted sources like another web application hosting server, run the following command:
sudo ufw allow from your_other_server_ip/32 to any port 27017
To verify the changes, run:
sudo ufw status
You get to see the following output:
Status: active To Action From -- ------ ---- 27017 ALLOW Anywhere OpenSSH ALLOW Anywhere 27017 (v6) ALLOW Anywhere (v6) OpenSSH (v6) ALLOW Anywhere (v6)
You can also read the official MongoDB documentation on how to install MongoDB on Ubuntu and other Linux variants.
You may also like other installation guides:
- How To Install Nginx On Ubuntu 18.04 LTE
- How To Install LAMP Stack – Ubuntu (Linux), Apache, MySQL, PHP
- How To Install LEMP Stack – Ubuntu (Linux), NGINX, MySQL, PHP
- How To Install WordPress On Ubuntu 18.04 LTE – LEMP Stack
- How To Install Let’s Encrypt SSL On Ubuntu 18.04 & 16.04, 14.04 LTS
I hope you would find this tutorial on how to install MongoDB on Ubuntu useful. If you have any question or concern, put them in the comment section below. You can also e-mail me on email@example.com