Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How to Install MongoDB on Debian 9

Table Of Contents


    ## Table of Contents

    – [Introduction](#introduction
    – [Prerequisites](#prerequisites
    – [Installing MongoDB](#installing-mongodb
    – [Checking the Service and Database](#checking-the-service-and-database
    – [Managing the MongoDB Service](#managing-the-mongodb-service
    – [Adjusting the Firewall](#adjusting-the-firewall
    – [Conclusion](#conclusion

    ## Introduction

    In this guide, we will explain to you how to install MongoDB on Debian 9.

    [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 an 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 a Debian 9 server and created a non-root user account with sudo privileges by following our guide, [Initial server setup on Debian 9](https://systemongrid.com/support/guides/initial-server-setup-with-debian-9 including a sudo-enabled non-root user and a firewall.

    ## Installing MongoDB

    We have to install the official MongoDB repository. First of all, we have to add MongoDB signing key with apt-key add. Now we have to make sure that curl command to be installed before.

    “`
    $ sudo apt install curl
    “`

    Now we have to download the key and pass it to apt-key add. To do that follow the below command.

    “`
    $ curl https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add –
    “`

    Now you have to create a source list for the MongoDB repo, For that apt knows where to download from. First, open the source list file in a text editor using below command.

    “`
    $ sudo vi /etc/apt/sources.list.d/mongodb-org-4.0.list
    “`

    Now you will get an empty file, copy the below text and paste it.

    “`
    deb http://repo.mongodb.org/apt/debian stretch/mongodb-org/4.0 main
    “`

    Now save and close the file and update your package cache by using below command.

    “`
    $ sudo apt update
    “`

    Now install the mongodb-org package to install the server and supporting tools by using below command.

    “`
    $ sudo apt-get install mongodb-org
    “`

    Now you have to enable and start the mongod service to get your MongoDB database running.

    “`
    $ sudo systemctl enable mongod
    $ sudo systemctl start mongod
    “`

    Now installed and started the latest version of MongoDB. Let’s verify that the server is running and works correctly.

    ## Checking the Service and Database

    Now, it’s 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 mongod
    “`

    ![mongodbdebianstatus](https://grid.media/assets/images/mongodb-debian-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 }’
    “`

    ![mongodbconnectionstatus](https://grid.media/assets/images/mongodb-debian-connection-status-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.

    ## Managing the MongoDB Service

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

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

    “`
    $ sudo systemctl status mongod
    “`

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

    “`
    $ sudo systemctl stop mongod
    “`

    Use the start command to start the systemd service.

    “`
    $ sudo systemctl start mongod
    “`

    Use the restart command to restart the systemd service.

    “`
    $ sudo systemctl restart mongod
    “`

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

    “`
    $ sudo systemctl disable mongod
    “`

    Use the enable command to enable the automatic startup.

    “`
    $ sudo systemctl enable mongod
    “`

    ## Adjusting the Firewall

    In the prerequisites, we recommended you to follow our guide, Initial Server Setup with Debian 9. 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 the 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 you can verify the change in firewall settings with ufw:

    “`
    $ sudo ufw status
    “`

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

    ![mongodbdebianufwstatus](https://grid.media/assets/images/mongodb-debian-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.235.

    “`
    . . .
    # network interfaces
    net:
    port: 27017
    bindIp: 127.0.0.1,your_server_ip
    . . .
    “`

    Then, save and exit the file.

    Restart MongoDB to make the changes effective.

    “`
    $ sudo systemctl restart mongod
    “`

    ## Conclusion

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