DEV Community

iffishells
iffishells

Posted on

Installation of MongoDb on Ubuntu 18.04

Introduction

ManngoDb is a free and open-source NOsql Document database used commonly in the modern web application on the internet.Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas.MongoDB is developed by MongoDB .

Pre-requisites

To follow this tutorial , you will need :

  • One Ubuntu 18.04
  • sudo non-root and firewall

step 1 :

Ubuntu official package repositories include an up to date version of mangodb ,which mean can install the necessary packages using

apt

first , update the packages list to have the most latest version of the repository listinggs :

sudo apt update -y

Now can install the Mangodb packages itself

sudo apt install -y mangodb

this command install several packages containing the latest stable version of MongoDb ,along with the helpfull tools.The Databse server is automatically started after installation

step 2 : checking service and DB

The installation process started MongoDb automatically,but lets verify the service is started and database is working or not.

first check the service status :

sudo systemctl status mongodb 

you will see th output like this :

According to systemd , the Mongodb server is up and running .

we can verify this further by actually connecting to the dataabase server and executing diagnostic command .

Execute this command

mongo --eval 'db.runCommand([connnectionStatus:1})' 

This will output the current database version , the server address and port , and the output of the status command :

A Value of 1 for the ok field in the response indicates that the server is working properly ,Next we 'll look how to manage the server is instance .

step 3 : Managin the MongoDb Service

MongoDb install as a systemd service , which means you that can manage it using standard systemd commands algonside all other system services in Ubuntu .

To verify status of the service type :

sudo systemctl status mongodb 

You can stop the server anytime by typing :

sudo systemctl stop mongodb 

To start the server when it is stopped , type :

sudo systemctl start mongodb 

you can also restart the server with single command :

sudo systemctl restart mognodb

By default Mongodb is configured to start automatically with the server but if you can to disable it .

sudo systemctl disable mongodb 

its easy to enable it again

sudo systemctl enable mongodb

Next , let's adjust the firewall setting for our Mongodb installation.

step 4 : adjusting the firewall (choice)

Assumming you have follow the above information for the installation for mogodb . if You intend to use the Mongodb server only locally with application running on the same server .this is the recommended and secure setting .However if you would like to be connect to your Mongodb server from the internet , you have to allow the incoming connection in ufw.

To Allow access to Mongodb on its default port 27017 from the everwhere , you could use .

sudo ufw allow 27017

However enabling internet access to MongoDb server on a default installation gives anyone unrestricted access to the database server and its data .

In Most cases Mongodb should be accessed only from the certain trusted locations , such as another server hosting an application . To achieve this task, you can allow access on MongoDb default port while specifying the IP address of another server that will be explicity allow to connect .

sudo ufw allow from other_server_IP/32 to any port 27017

you can verify the change in firewall setting with UFW :

sudo ufw status

if you have decided to allow only a certain IP address to connect to MongoDb server , the address of the allowed location will be instead of anywhere in the output.

you can find more advance firewall setting for restriction access to services in this community.

Even though the port is open MongoDb is currently only listening on the local address 127.0.0.1 . To allow remote connection , add your server publically-routable IP address to mongod.conf file.

Open the MongoDB Configuration file in your editor .

sudo vim /etc/mongodb.conf

Add your server IP address to the bindip value :

...
logappend=true

bind_ip = 127.0.0.1,your_server_ip
#port = 27017

...

Be sure to place a comma between the existing IP address and one you added . save te file . if you are using vim .

:wq!

Conclusion

This is all from my side if you are facing any problem you feel free to contact me.

Top comments (0)