Install MongoDB on CentOS

Updated on November 21, 2023
Install MongoDB on CentOS header image

MongoDB is a leading NoSQL database commonly used in modern web applications. This tutorial 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.

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

Next Steps

MongoDB is not secure by default. Use our guide to configure MongoDB security and authentication.

Note: Any start-up warnings related to Readahead can be safely ignored. To prevent these warnings, verify that the sectors are "as stated" in the warning by running blockdev –getra /dev/vda1. If the output is greater than 512, run the following commands:

blockdev --setra 512 /dev/vda1
systemctl restart mongod

The warning will now be resolved.