Centos

How to Install MongoDB on CentOS 8, 7, 6

Share on:

Table of Contents

MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License.

This guide will walk you through setting up MongoDB on Red Hat Enterprise Linux (RHEL) and CentOS Linux:

  • RHEL / CentOS 8
  • RHEL / CentOS 7
  • RHEL / CentOS 6

Update your system before beginning installation.

# sudo yum update
# sudo reboot

 

Install MongoDB Community Edition

Install MongoDB with the yum package manager.

  1. Create the /etc/yum.repos.d/mongodb-org-4.4.repo file for MongoDB.

    # nano /etc/yum.repos.d/mongodb-org-4.4.repo
    
  2. Paste the following into the editor:

    [mongodb-org-4.4]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
    
  3. Save and exit the file.

  4. To install the latest stable version of MongoDB:

    # sudo yum install -y mongodb-org
    

If you need to install a specific version or prevent automatic upgrades, see the MongoDB documentation.

Setup MongoDB

  1. Start MongoDB.

    # systemctl start mongod.service
    # systemctl enable mongod.service
    
  2. Connect to MongoDB.

    # mongo

Securing MongoDB

MongoDB is not secure by default. If you are installing MongoDB and launching it without configuring it for authentication, you are going to have a bad time. People can read, write, destroy, or alter data on your server without ever needing to login or authenticate in anyway. Securing the database is not hard to do and can be done in a few steps.

Secure MongoDB

First, start up your Mongo client. On Linux it is the command mongo. Enter this block of text in, of course changing the placeholder parts to your own information.

db.createUser({
  user: "USERNAME", 
  pwd: "PASSWORD", 
  roles: [
    {
      role: "readWrite",
      db: "YOUR_DATABASE"
    }
  ]
});

After you’re done, quit the mongo client and edit your MongoDB configuration file. Depending on your OS and distro, you will find it in one of these places.

/etc/mongodb.conf
/etc/mongod.conf

Change the following line, #security: to the following.

security:
  authorization: enabled

You should consider changing the bind port to localhost (127.0.0.1) or bind it to a private IP that does not get exposed to the internet. Exposing your database to the internet is just a bad idea in general. This is what you should change.

# network interfaces
net:
  port: 27017
  bindIp: 634.234.102.6

Mind your spaces! Always in twos, never tabs. Afterwards restart your MongoDB database. On Linux it will be one of the following commands based on your distro of choice.

systemctl restart mongod
systemctl restart mongodb

Hope this guide will help you.