Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How To Install MongoDB on CentOS 7

Table Of Contents


    ## Table of Contents

    – [Introduction](#introduction
    – [Prerequisites](#prerequisites
    – [Adding the MongoDB Repository](#adding-the-mongodb-repository
    – [Installing MongoDB](#installing-mongodb
    – [Verifying Startup](#verifying-startup
    – [Conclusion](#conclusion

    ## Introduction

    In this guide, we will explain to you how to install MongoDB on CentOS 7.

    [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 CentOS 7 server and created a non-root user account with sudo privileges by following our guide, How to Create a Sudo user on CentOS.

    ## Adding the MongoDB Repository

    By default, Mongodb-org package does not exist in the default repositories for CentOS. It will maintain a separate repository dedicated to it only. You have to add it to your server using below command.

    In the vi editor, create a .repo file for yum, the package management utility for CentOS.

    “`
    $ sudo vi /etc/yum.repos.d/mongodb-org.repo
    “`

    Then you can check the Install on Red Hat section of MongoDB’s documentation and ass the repository information for the latest stable release to the file.

    “`
    [mongodb-org-3.4]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
    “`

    Now save and close the file.

    Now you can verify that the MongoDB repository exists within the yum utility. You can see the list of enabled repositories use the below command.

    “`
    $ yum repolist
    “`

    ![yumreposit](https://grid.media/assets/images/yum-reposit-02132019.png

    Now you can proceed with the installation.

    ## Installing MongoDB

    Using yum utility which is a third-party repository, We can install the mongodb-org package using below command.

    “`
    $ sudo yum install mongodb-org
    “`

    Now you will get two Is this ik [y/n] prompts. The first prompt permits the installation of the MongoDB package and the second one imports a GPG key.

    MongoDB publisher signs their software and yum uses a key to confirm the integrity of the downloaded packages. Type y and then press ENTER key.

    Now start the MongoDB service with the systemctl utility.

    “`
    $ sudo systemctl start mongod
    “`

    You can change the state of the MongoDB service with the reload and stop commands.

    The below command requests that the mongod process reads the configuration file, /etc/mongod.conf, and applies any changes without requiring a restart.

    “`
    $ sudo systemctl reload mongod
    “`

    The stop command will stop the running mongod processes.

    “`
    $ sudo systemctl stop mongod
    “`

    Now, If you execute the start command, systemctl utility did not provide a result. But you can check the service started by viewing the end of the mongodb.log file with tail command.

    “`
    $ sudo tail /var/log/mongodb/mongod.log
    “`

    ![connectionsonport](https://grid.media/assets/images/connections-on-port-02132019.png

    The output for waiting for connection confirms that MongoDB has started successfully. Now you can access the database server with the MongoDB Shell.

    “`
    $ mongo
    “`

    Now you may get some warnings. To resolve warnings change the processes soft limit value for mongod by editing the 20-nproc.conf file as below.

    “`
    $ sudo vi /etc/security/limits.d/20-nproc.conf
    “`

    Add these lines to the end of the file.

    “`
    mongod soft nproc 32000
    “`

    Now you can restart using systemctl utility as below.

    “`
    $ sudo systemctl restart mongod
    “`

    Now there are no warnings in your output.

    To know how to interact with MongoDB from the shell, you can use the below command for help.

    “`
    $ db.help(
    “`

    You can quit the shell using the exit command.

    “`
    $ exit
    “`

    ## Verifying Startup

    We have to verify that the database-driven application cannot function without a database, So we have to make sure that the MongoDB thread, mongod will start the system.

    Use the systemctl utility to check its startup status.

    “`
    $ systemctl is-enabled mongod; echo $?
    “`

    If you get output zero, confirms an enabled daemon. If you get one, as output then it is disabled.

    ![verifyingstartup](https://grid.media/assets/images/verifying-startup-02132019.png

    If you get disabled, then use the systemctl utility to enable it.

    “`
    $ sudo systemctl enable mongod
    “`

    If you want to exit from the shell, then you can simply use the exit command.

    “`
    $ exit
    “`

    ![mongodbexit](https://grid.media/assets/images/exit-02132019.png

    ## Conclusion

    In this guide, we have described How to install MongoDB on CentOS 7.