How to Install MongoDB on Ubuntu Server


MongoDB is a free to use and an open-source NoSQL database utilized generally in modern web applications. In this tutorial we will discuss how to install MongoDB on a Ubuntu Cloud Server.

1st Step - Adding the MongoDB Repository

MongoDB is as of now incorporated into Ubuntu package repositories, yet the authority MongoDB repository gives most recent version and is the prescribed method for installing the software. In this progression, we will add this official repository to our server.

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

After effectively importing the key, you will see result like this:

Result

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

Next, we need to include the MongoDB repository aspects so apt command will know where to download the packages from.

Use the given below command to list file for MongoDB.

$ 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 you done with adding the repository details, we have to update the package list.

$ sudo apt-get update

2nd Step - Installing & Verifying MongoDB

Presently we can install the MongoDB package itself.

$ sudo apt-get install -y mongodb-org

The above command will install various packages including most recent version of MongoDB alongside  supportive management tools needed for the MongoDB web server.

 

Next, begin MongoDB with systemctl.

$ sudo systemctl start mongod

You can likewise utilize systemctl to check that the administration has begun properly.

 

$ sudo systemctl status mongod

Result

? 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-04-16 18:25:15 GST; 15min 19s 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

 

The last step is to empower automatically beginning MongoDB when the system begins.

$ sudo systemctl enable mongod

The MongoDB server is currently designed and running, and you can deal with the MongoDB service utilizing the systemctl command.

3rd Step - Adjusting the Firewall

Supposing you have taken after the underlying server setup instructional exercise guidelines to empower the firewall on your server, MongoDB server will be inaccessible to reach from the web.

In the event that you expect to utilize the MongoDB server just locally with applications running on a similar server, it is a suggested and secure setting. Notwithstanding, in the event that you might want to have the capacity to connect with your MongoDB server from the web, we need to permit the incoming  associations in ufw.

To enable access to MongoDB on its default port 27017 from all over, you could utilize sudo ufw allow27017. Be that as it may, permitting web access to MongoDB server on a default installation gives unlimited access to the entire database server.

 

In most of the situations, MongoDB ought to be accessed to just from certain trusted locations, for example, another server hosting an application. To achieve this task, you can permit access on MongoDB's default port while determining the IP address of another server that will be unequivocally permitted to connect.

$ sudo ufw allow from another_server_ip_address/28 to any port 2082

You can check the change in firewall settings using ufw.

$ sudo ufw status

You should see activity to 27017 port permitted in the output. If you have chosen to permit just a specific IP address to allowed with MongoDB server, the IP address of the permitted area will be recorded rather than Anywhere in the output.

Output

Status: active

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