How To Install MongoDB on Red Hat or CentOS
Introduction
MongoDB is a document-oriented database that is free and open-source. It is classified as a NoSQL database because it does not rely on a traditional table-based relational database structure. Instead, it uses JSON-like documents with dynamic schemas. Unlike relational databases, MongoDB does not require a predefined schema before you add data to a database. You can alter the schema at any time and as often as is necessary without having to setup a new database with an updated schema.
This tutorial guides you through installing MongoDB Community Edition on a CentOS 7 server.
Step 1 – Adding the MongoDB Repository
The mongodb-org
package does not exist within the default repositories for CentOS. However, MongoDB maintains a dedicated repository. Let’s add it to our server.
With the vi
editor, create a .repo
file for yum
, the package management utility for CentOS:
sudo vi /etc/yum.repos.d/mongodb-org.repo
Copy
Then, visit the Install on Red Hat section of MongoDB’s documentation and add the repository information for the latest stable release to the file:/etc/yum.repos.d/mongodb-org.repo
[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc
Copy
Save and close the file.
Before we move on, we should verify that the MongoDB repository exists within the yum
utility. The repolist
command displays a list of enabled repositories:
yum repolist
With the MongoDB Repository
in place, let’s proceed with the installation.
Step 2 – Installing MongoDB
We can install the mongodb-org
package from the third-party repository using the yum
utility.
sudo yum install mongodb-org
Copy
There are two Is this ok [y/N]:
prompts. The first one permits the installation of the MongoDB packages and the second one imports a GPG key. The publisher of MongoDB signs their software and yum
uses a key to confirm the integrity of the downloaded packages. At each prompt, type Y
and then press the ENTER
key.
Next, start the MongoDB service with the systemctl
utility:
sudo systemctl start mongod
Copy
Although we will not use them in this tutorial, you can also change the state of the MongoDB service with the reload
and stop
commands.
The reload
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
Copy
The stop
command halts all running mongod
processes.
sudo systemctl stop mongod
Copy
The systemctl
utility did not provide a result after executing the start
command, but we can check that the service started by viewing the end of the mongod.log
file with the tail
command:
sudo tail /var/log/mongodb/mongod.log
Copy
Output. . .
[initandlisten] waiting for connections on port 27017
An output of waiting for a connection confirms that MongoDB has started successfully and we can access the database server with the MongoDB Shell:
mongo