How to Install MongoDB on CentOS 8

This tutorial exists for these OS versions

On this page

  1. Prerequisites
  2. Install MongoDB
  3. Create a MongoDB Admin User
  4. Configure MongoDB Authentication
  5. Conclusion

MongoDB is a free, open-source and document-oriented database engine that provides access to non-relational databases. MongoDB saves the data in JASON like documents along with dynamic schema so it provides better performance compared to other databases. MongoDB is a NoSQL database means it does not support SQL to manipulate the stored data. MongoDB provides a rich set of features including, Ad hoc queries, Indexing, Replication, Load balancing, File storage, Aggregation, Transactions and much more. MongoDB is one of the most popular database engines across all systems and has been used in a number of large scale production deployments.

In this tutorial, we will show you how to install and use MongoDB on CentOS 8 server.

Prerequisites

  • A server running CentOS 8.
  • A root password is configured on your server.

Install MongoDB

By default, MongoDB is not available in the CentOS 8 default repository. So you will need to add MongoDB repository to your system. You can add it by creating the file mongodb.repo inside the directory /etc/yum.repos.d/:

nano /etc/yum.repos.d/mongodb.repo

Add the following lines:

[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/development/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

Save and close the file. Then, install MongoDB with the following command:

dnf install mongodb-org

After installing MongoDB, start the MongoDB service and enable it to start after system reboot:

systemctl start mongod
systemctl enable mongod

You can now check the status of MongoDB with the following command:

systemctl status mongod

You should get the following output:

systemctl status mongod

Output

? mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2019-10-28 03:59:12 EDT; 5min ago
     Docs: https://docs.mongodb.org/manual
  Process: 737 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 735 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 732 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 726 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 914 (mongod)
   Memory: 216.1M
   CGroup: /system.slice/mongod.service
           ??914 /usr/bin/mongod --auth -f /etc/mongod.conf

Oct 28 03:58:14 centos8 systemd[1]: Starting MongoDB Database Server...
Oct 28 03:58:28 centos8 mongod[737]: about to fork child process, waiting until server is ready for connections.
Oct 28 03:58:28 centos8 mongod[737]: forked process: 914
Oct 28 03:59:12 centos8 mongod[737]: child process started successfully, parent exiting
Oct 28 03:59:12 centos8 systemd[1]: Started MongoDB Database Server.

Next, you can access the MongoDB shell with the following command:

mongo

You should get the following output:

MongoDB shell version v4.2.1-rc0-5-g87a606d
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e8e052c8-7c47-4908-9a29-d7825bac037a") }
MongoDB server version: 4.2.1-rc0-5-g87a606d
Server has startup warnings: 
2019-10-28T04:07:55.106-0400 I  CONTROL  [initandlisten] 
2019-10-28T04:07:55.107-0400 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-10-28T04:07:55.107-0400 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-10-28T04:07:55.107-0400 I  CONTROL  [initandlisten] 
2019-10-28T04:07:55.107-0400 I  CONTROL  [initandlisten] 
2019-10-28T04:07:55.107-0400 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-10-28T04:07:55.107-0400 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-10-28T04:07:55.107-0400 I  CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

Create a MongoDB Admin User

Next, create an Admin user for MongoDB with administrative privileges. First, open the MongoDB shell with the following command:

mongo

Next, switch the database to admin as shown below:

>use admin

Next, create a new MongoDB user with the following command:

> db.createUser(
{
user: "mongodadmin",
pwd: "password123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

You should get the following output:

Successfully added user: {
	"user" : "mongodadmin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}

Next, list your admin user with the following command:

>show users

You should see the following output:

{
	"_id" : "admin.mongodadmin",
	"userId" : UUID("f6e908db-e393-44a9-8c77-0fdb1c2baa0e"),
	"user" : "mongodadmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

Configure MongoDB Authentication

By default, MongoDB allows all users to access the MongoDB shell and run any commands. So it is recommended to configure the MongoDB authentication to prevent other users to run any command without sufficient privileges.

First, you will need to enable the MongoDB authentication by editing the file /lib/systemd/system/mongod.service.

nano /lib/systemd/system/mongod.service

Change the following line:

Environment="OPTIONS= --auth -f /etc/mongod.conf"

Save and close the file. Then, reload the systemd daemon and restart the MongoDB service with the following command:

systemctl --system daemon-reload
systemctl restart mongod

Now, login to the MongoDB shell and try to see the users without auhtentication:

mongo
> use admin
> show users

You should see the following error:

2019-10-28T04:13:15.346-0400 E  QUERY    [js] uncaught exception: Error: command usersInfo requires authentication :
[email protected]/mongo/shell/utils.js:25:13
[email protected]/mongo/shell/db.js:1638:15
[email protected]/mongo/shell/utils.js:883:9
[email protected]/mongo/shell/utils.js:790:15
@(shellhelp2):1:1

Now, run the following command to authenticate the MongoDB:

> db.auth('mongodadmin', 'password123')
> show users

You should see the admin user with their roles in the following output:

{
	"_id" : "admin.mongodadmin",
	"userId" : UUID("f6e908db-e393-44a9-8c77-0fdb1c2baa0e"),
	"user" : "mongodadmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

Conclusion

In the above tutorial, we learned how to install MongoDB and configure MongoDB user authentication. I hope you have now enough knowledge to create your own database and start building an application with MongoDB. Feel free to ask me if you have any questions.

Share this page:

Suggested articles

0 Comment(s)

Add comment