Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How to Install MongoDB on Ubuntu 18.04

Table Of Contents


    ## Table of Contents

    – [Introduction](#introduction
    – [Prerequisites](#prerequisites
    – [Install MongoDB on Ubuntu 18.04](#install-mongodb-on-ubuntu-18-04
    – [Install MongoDB](#install-mongodb
    – [Check MongoDB Service and Database](#check-mongodb-service-and-database
    – [Manage MongoDB Service](#manage-mongodb-service
    – [Adjust the Firewall – Optional](#adjust-the-firewall-optional
    – [Conclusion](#conclusion

    ## Introduction

    In this guide, we will explain to you how to install MongoDB on Ubuntu 18.04.

    [MongoDB](https://www.mongodb.com/ is an open-source NoSQL database used in web applications to store the data in the form of key-value pairs. It provides high scalability and flexibility including data management and data modeling. It also has the advanced feature of Auto-Scaling. Since MongoDB is a cross-platform database, you can install it in different operating systems like Linux, Windows etc.

    ## Prerequisites

    You should have access to an Ubuntu 18.04 server and created a non-root user account with sudo privileges by following our guide, [Initial server setup with Ubuntu 18.04](https://systemongrid.com/support/guides/how-to-do-initial-server-setup-with-ubuntu-18.04.

    ## Install MongoDB on Ubuntu 18.04

    ## Install MongoDB

    By default, Ubuntu includes the updated version of MongoDB in its package repositories.

    Update the packages using the “apt” command to get the latest version of the repository listings.

    “`
    $ sudo apt update
    “`

    Now, install the MongoDB package using the “apt” command.

    “`
    $ sudo apt install mongodb
    “`

    With this, you have installed the latest version of MongoDB with some essential management tools for the MongoDB server.

    ## Check MongoDB Service and Database

    Now, its time for you to verify if the MongoDB service is active and running.

    Use the below command to check the status of the MongoDB service.

    “`
    $ sudo systemctl status mongodb
    “`

    Output:

    ![mongodbstatus](https://grid.media/assets/images/mongodb-status-02132019.png

    The above output shows that the MongoDB service is active and running.

    You can verify the MongoDB database by connecting to the database server and executing the following diagnostic command.

    “`
    $ mongo –eval ‘db.runCommand({ connectionStatus: 1 }’
    “`

    Output:

    ![mongodbshellversion](https://grid.media/assets/images/mongodb-shell-version-02132019.png

    In the above output, you will get the current MongoDB shell version, the server address, and the status of the server. The value of “1” for the “ok” indicates – the MongoDB server is active and running properly.

    ## Manage MongoDB Service

    In Ubuntu, the MongoDB comes as a systemd service. So, you need to use systemctl to manage the systemd services.

    Use the status command to verify the status of the systemd service.

    “`
    $ sudo systemctl status mongodb
    “`

    Use the stop command to stop the currently running systemd service.

    “`
    $ sudo systemctl stop mongodb
    “`

    Use the start command to start the systemd service.

    “`
    $ sudo systemctl start mongodb
    “`

    Use the restart command to restart the systemd service.

    “`
    $ sudo systemctl restart mongodb
    “`

    In Ubuntu, MongoDB is configured to start automatically along with the server. Use the disable command to disable the automatic startup.

    “`
    $ sudo systemctl disable mongodb
    “`

    Use the enable command to enable the automatic startup.

    “`
    $ sudo systemctl enable mongodb
    “`

    ## Adjust the Firewall – Optional

    In the prerequisites, we recommended you to follow our guide, Initial Server Setup with Ubuntu 18.04. If you have followed the guide and enabled the firewall, you can’t access your MongoDB server from the internet. We recommended this assuming that you are intended to use the MongoDB server locally.

    If you want to access the MongoDB server from the internet, you will need to allow the incoming connections in ufw to connect to your MongoDB server from the internet. But, accessing MongoDB server on a default installation is not secure because spammers can get access to the database server and its data.

    So, to access your MongoDB server securely from the internet, you need to allow access to its default port, 27017 and mention the IP address of the server that you allowed to connect.

    “`
    $ sudo ufw allow from IP_address/32 to any port 27017
    “`

    Now, check the status of the ufw. You must see traffic to port 27017 is allowed along with the IP address of your server.

    Output:

    ![mongodbufwstatus](https://grid.media/assets/images/mongodb-ufw-status-02132019.png

    If you want to know the advanced firewall settings to restrict access, follow our guide, UFW Essentials: Common Firewall Rules and Commands.

    MongoDB listens on the local address, 127.0.0.1 even though the port is open. So, to allow remote connections, you need to add the IP address of your server to the MongoDB configuration file.

    “`
    $ sudo vi /etc/mongodb.conf
    “`

    Replace your IP address in the place of 216.200.116.91.

    “`

    logappend=true

    bind_ip = 127.0.0.1,216.200.116.91
    #port = 27017

    “`

    Then, save and exit the file.

    Restart MongoDB to make the changes effective.

    “`
    $ sudo systemctl restart mongodb
    “`

    ## Conclusion

    In this guide, you have learned how to install MongoDB on Ubuntu 18.04 and to allow the incoming connections in ufw to connect to your MongoDB server from the internet.