MongoDB is a free and open-source NoSQL Database used commonly in today’s web applications.
In this tutorial, we will show you how to install and setup MongoDB on your Ubuntu 18.04 machine in a few simple steps!
Step 1: Install MongoDB on Ubuntu Using Apt
Since the MongoDB package is available in Ubuntu’s official repositories, we can simply use apt to install MongoDB on our system.
But before that, update all system critical packages first.
1 |
sudo apt update |
Now, install the MongoDB package using:
1 |
sudo apt install mongodb |
This will install the latest version of MongoDB on our system.
As for any Database System, there is a server and a service associated with the task of Database Management. The MongoDB server is automatically started after the installation.
To ensure that our installation went smoothly, we need to ensure that the MongoDB service and the server are working correctly.
Step 2: Check the MongoDB Service
To check the status of the MongoDB service, use systemctl
:
1 |
sudo systemctl status mongodb |
You’ll get an output similar to the below screenshot.
So now that we know that our service is running, let’s verify if our MongoDB Server is responding to our requests or not.
Step 3: Test the MongoDB Server
By default, the MongoDB Server is located at the localhost and uses port 27017. So the address of the server is:
1 |
127.0.0.1:27017 |
Let’s test our server by running this command to find the Connection Status using mongo
command:
1 |
mongo --eval 'db.runCommand({ connectionStatus: 1 })' |
You should get a response like the below screenshot, having the “ok” field as 1, ensuring that our Connection is valid.
Step 4: Allow remote access to the Database
By default, the MongoDB server is only allowed by the ufw
firewall to operate locally.
To allow access to MongoDB on it’s port 27017 from anywhere, we need to allow incoming connections on this port.
But this is very insecure, since any person can access the Database. To prevent this, only allow IP Addresses which correspond to trusted machines/servers.
1 |
sudo ufw allow from TRUSTED_IP/32 to any port 27017 |
Replace TRUSTED_IP
with any trusted IP address in the above command.
This allows connections from your trusted server to reach the MongoDB Database, since ufw
now allows these connections.
But we still need to ensure that the MongoDB server does not only listen locally. Therefore, we need to add our trusted machine’s IP to the mongodb.conf
configuration file.
Use your favorite text editor and edit /etc/mongodb.conf
.
1 |
sudo vi /etc/mongodb.conf |
Go to the bind_ip value.
Add your trusted machine’s public IP Address here, by appending to the field using a comma (,).
The modified file will look something like this:
Exit the editor, after saving changes
Step 5: Restart the MongoDB Service
Now, we are almost done! We simply need to restart the mongodb
service to apply our changes.
1 |
sudo systemctl restart mongodb |
We have now successfully configured our MongoDB Server to accept remote connections!
Conclusion
In this tutorial. we showed you how you can install and configure the latest version of MongoDB on your Ubuntu 18.04 system. We also made sure that we can accept specific remote connections to our database from another trusted server.
I hope this served you well and cleared any of your remaining doubts regarding MongoDB installation!