How to Install MongoDB on CentOS 8
Updated on
•6 min read

MongoDB is a free and open-source document database. It belongs to a family of databases called NoSQL, which is different from the traditional table-based SQL databases like MySQL and PostgreSQL.
In MongoDB, data is stored in flexible, JSON-like documents where fields can vary from document to document. It does not require a predefined schema, and data structure can be changed over time.
This tutorial explains how to install and configure MongoDB Community Edition on a CentOS 8 server.
Installing MongoDB
MongoDB is not available in CentOS 8 core repositories. We’ll enable the official MongoDB repository and install the packages.
At the time of writing this article, the latest version of MongoDB available from the official MongoDB repositories is version 4.2. Before starting with the installation, visit the Install on Red Hat section of MongoDB’s documentation and check if there is a new release available.
Perform the following steps as root or user with sudo privileges to install MongoDB on a CentOS 8 system:
Enable the MongoDB repository by creating a new repository file named
mongodb-org.repoinside the/etc/yum.repos.d/directory:sudo nano /etc/yum.repos.d/mongodb-org.repo/etc/yum.repos.d/mongodb-org.repo[mongodb-org-4.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.2.ascIf you want to install an older version of MongoDB, replace each instance of
4.2with your preferred version.Install the
mongodb-orgmeta-package:sudo dnf install mongodb-orgDuring the installation you will prompted you to import the MongoDB GPG key. Type
yand hitEnter.The following packages will be installed on your system as a part of the
mongodb-orgpackage:mongodb-org-server- Themongoddaemon, and corresponding init scripts and configurations.mongodb-org-mongos- Themongosdaemon.mongodb-org-shell- The mongo shell, an interactive JavaScript interface to MongoDB, used to perform administrative tasks thought the command line.mongodb-org-tools- Contains several MongoDB tools for importing and exporting data, statistics, as well as other utilities.
Once the installation is completed, enable and start the MongoDB service:
sudo systemctl enable mongod --nowTo verify the installation, connect to the MongoDB database server and print the server version:
mongoRun the following command to display the MongoDB version:
db.version()The output will look like something like this:
4.2.3
Configuring MongoDB
The MongoDB configuration file is named mongod.conf and is located in the /etc directory. The file is in YAML
format.
The default configuration settings are sufficient in most cases. However, for production environments, we recommend uncommenting the security section and enabling authorization as shown below:
security:
  authorization: enabled
The authorization option enables Role-Based Access Control (RBAC)
that regulates users access to database resources and operations. If this option is disabled, each user will have access to any database and execute any action.
After making changes to the MongoDB configuration file, restart the mongod service:
sudo systemctl restart mongodFor more information about the MongoDB configuration options visit, the Configuration File Options documentation page.
Creating Administrative MongoDB User
If you enabled the MongoDB authentication, you’ll need to create an administrative user that can access and manage the MongoDB instance.
First, access the MongoDB shell with:
mongoType the following command to connect to the admin database:
use adminswitched to db admin
Create a new user named mongoAdmin with the userAdminAnyDatabase role:
db.createUser({user: "mongoAdmin",pwd: "changeMe",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
Successfully added user: {
	"user" : "mongoAdmin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}
Exit the mongo shell with:
quit()To test the changes, access the mongo shell using the administrative user you have previously created:
mongo -u mongoAdmin -p --authenticationDatabase adminMongoDB shell version v4.2.3
Enter password:
use adminswitched to db admin
Now, print the users with:
show users{
	"_id" : "admin.mongoAdmin",
	"user" : "mongoAdmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}
Conclusion
We’ve shown you how to install and configure MongoDB 4.2 on your CentOS 8 server.
Consult The MongoDB 4.2 Manual for more information on this topic.
If you hit a problem or have feedback, leave a comment below.


