Databases

How To Install MongoDB on CentOS 8

In this article, we will install MongoDB on CentOS 8.

This article installs MongoDB 4.4 Community Edition. To install a different version of MongoDB Community, use the version drop-down menu in the upper-left corner of this page to select the documentation for that version.

MongoDB, also known as Mongo, is an open-source document database used in many modern web applications. It’s classified as a NoSQL database because it doesn’t rely on a traditional table-based relational database structure.

Prerequisites

  • Dedicated server or VPS running CentOS 8.
  • Root access or normal user with administrative privileges.
  • Firewall configured with firewalld

Let’s get started with the installation.

Step 1 – Keep the server up to date

# dnf update -y

Step 2 – Install MongoDB 4.4

There’s no official MongoDB package available in the standard CentOS repositories.  We need to create one repo file in order to install MongoDB.

To create repo file you can use vi — a widely-used text editor that’s installed on CentOS systems by default

Create a /etc/yum.repos.d/mongodb-org-4.4.repo file so that you can install MongoDB directly using yum:

# vi /etc/yum.repos.d/mongodb-org-4.4.repo

[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 

Save and exit.

Here’s what each of these directives do:

  • [mongodb-org-4.4]: the first line of a .repo file is a single string of characters, wrapped in brackets, that serves as an identifier for the repository
  • name: this directive defines a human-readable name to describe the repository. You could enter whatever name you’d like here, but for clarity you can enter MongoDB Repository
  • baseurl: this points to the URL of a directory where the repository’s repodata directory, which contains the repository’s metadata, can be found
  • gpgcheck: setting this directive to 1 tells the DNF package manager to enable GPG signature-checking on this repository, requiring it to verify whether any packages you want to install from it have been corrupted or tampered with
  • enabled: setting this directive to 1 will tell DNF to include this repository as a package source; setting it to 0 would disable this behavior
  • gpgkey: this directive specifies the URL of the GPG key that should be imported to verify the signatures of packages from this repository

To install the latest stable version of MongoDB, issue the following command:

# dnf install -y mongodb-org

Step 3 – Start and enable the MongoDB service

# systemctl start mongod

# systemctl enable mongod

Step 4 – Verify that the database is operational

You can further verify that the database is operational by connecting to the database server and executing a diagnostic command.

# mongo –eval ‘db.runCommand({ connectionStatus: 1 })’

Output:

MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { “id” : UUID(“e89ae2f4-29d5-4736-b4aa-3ad0ac5933bc”) }
MongoDB server version: 4.4.1
{
“authInfo” : {
“authenticatedUsers” : [ ],
“authenticatedUserRoles” : [ ] },
“ok” : 1
}

Step 5 – Begin using MongoDB

Start a mongo shell on the same host machine as the mongod. You can run the mongo shell without any command-line options to connect to a mongod that is running on your localhost with default port 27017:

# mongo

To uninstall MongoDB, run following commands:

Step 1 – Stop MongoDB

# systemctl stop mongod

Step 2 – Remove Packages

# dnf erase $(rpm -qa | grep mongodb-org)

Step 3 – Remove Data Directories

Remove MongoDB databases and log files.

# sudo rm -r /var/log/mongodb
# sudo rm -r /var/lib/mongo

That’s it.

We have successfully install MongoDB on CentOS 8.

Related Articles