Install MongoDB on Ubuntu 16.04 [Tutorial Under 2 Minutes]


If you want to install MySQL on Ubuntu 16.04 server then, this article is for you. Before diving into the installation part, let us see a short introduction about MongoDB.

What is MongoDB?

MongoDB is No SQL Database management system unlike other relational database management system, MongoDB uses the Document structure in Jason Format to store the data instead of using Tables.

MongoDB started back then 2008 and has grown to a large extent due to it’s No SQL functionality.


Install MongoDB on Ubuntu 16.04

We can install MongoDB from ubuntu’s default repository or you can install Mongo DB from its Official repository for up to date version.

To install the MongoDB, we need the GPG Keys to verify the authenticity of the software. We have to add the official Repository to the Ubuntu server.

Also, let us first import the GPG keys for the repository. For that, use the below command.

$ sudo apt-key adv --keyserver hkp:// --recv EA312927

You should get the following output, after importing the GPG keys.

gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

After that, we will create a source file to indicate the location for apt to download the MongoDB.

echo "deb xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

After that, update the package index.

$ sudo apt-get update

then, install the Mongo DB using the below command.

$ sudo apt-get install -y mongodb-org

This command will run for sometime and install all the required components of the Latest MongoDB.


Start the MongoDB using the below command.

$ sudo systemctl start mongod

Then, check the status of MongoDB

$ sudo systemctl status mongod

You should get 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 2017-02-12 11:07:10 EDT; 2min 48s ago
 Main PID: 4093 (mongod)
    Tasks: 16 (limit: 512)
   Memory: 53.1M
      CPU: 1.624s
   CGroup: /system.slice/mongodb.service
           └─4093 /usr/bin/mongod --quiet --config /etc/mongod.conf

Whenever, we restart or start the system, the MongoDB also has to be started. For that, use the below command.

$ sudo systemctl enable mongod

Now, it started running. If you want to stop mongoDB, please use the below command.

$ sudo systemctl stop mongod

And then, when you want to start MongoDB, just use the start command we have used earlier in this article.

Configuring the Firewall for MongoDB

If you have followed our ubuntu initial server setup tutorial, you might not access the MongoDB from any external source.

You can only access it, by logging into the remote server. It is for security for purpose only. For small applications running on single server, it is fine and recommended.


if you hosted any other application on MongoDB and need to access remotely, then you have to enable the default MongoDB port 27017.

Use this simple command to accomplish the task.

$ sudo ufw allow 27017

if you enable the port, then you will be allowed to access MongoDB from internet from anywhere.

However, if you are only focused on connecting other applications or server to access the database, then specify the address of the source in the firewall.

Then, UFW will only allow connection to the MongoDB from the specified on the port.

For that, use the below command.

$ sudo ufw allow from your_other_server_ip/32 to any port 27017

Now, Look at the UFW to find whether the changes have been reflected.

$ sudo ufw status

You will get the following output, if you have not specified any IP address during the process.

Status: active

To                         Action      From
--                         ------      ----
27017                      ALLOW       Anywhere
OpenSSH                    ALLOW       Anywhere
27017 (v6)                 ALLOW       Anywhere (v6)
OpenSSH (v6)               ALLOW       Anywhere (v6)

If you have specified any IP, then you should see the IP address instead of anywhere in the status.


Thats all. Now, you have installed Mongo DB on ubuntu 16.04 server. Your next step would be start coding and accessing the database.

Make sure, you subscribe for our upcoming free tutorial on cloud.


Share on facebook
Share on twitter
Share on linkedin

Leave a Comment