Home / How To / How to install MongoDB on CentOS 8

How to install MongoDB on CentOS 8



MongoDB is a free, open source and document-oriented database engine that provides access to non-relational databases. MongoDB saves data in JASON-like documents together with a dynamic schema to provide better performance compared to other databases. MongoDB is a NoSQL database means that it does not support SQL for manipulating stored data. MongoDB offers a rich set of features including ad hoc issues, indexing, replication, load balancing, file storage, aggregation, transactions and much more. MongoDB is one of the most popular database engines in all systems and has been used in a number of large-scale production installations.

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.

Installing MongoDB

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

9659008achtenano /etc/yum.repos.d/mongodb.repo file 1919900900 Add the following lines: [19659010] 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 startup:

  systemctl start mongod 
systemctl enable mongod [19659009] 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 setting: disabled)
Active: active (running) since Mon 2019-10-28 03:59:12 EDT; 5 minutes ago
Document: https://docs.mongodb.org/manual
Process: 737 ExecStart = / usr / bin / mongod $ OPTIONS (code = terminated, status = 0 / SUCCESS)
Process: 735 ExecStartPre = / usr / bin / chmod 0755 / var / run / mongodb (code = terminated, status = 0 / SUCCESS)
Process: 732 ExecStartPre = / usr / bin / chown mongod: mongod / var / run / mongodb (code = terminated, status = 0 / SUCCESS)
Process: 726 ExecStartPre = / usr / bin / mkdir -p / var / run / mongodb (code = terminated, 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]: Launch MongoDB database server ...
Oct 28 03:58:28 centos8 mongod [737]: about to start the child process, waiting until the 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 closing
Oct 28 03:59:12 centos8 systemd [1]: Started MongoDB database server.

Then 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
connects 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
The server has boot warnings:
2019-10-28T04: 07: 55.106-0400 IN 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 unlimited.
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 IN CONTROL [initandlisten] ** WARNING: / sys / kernel / mm / transparent_hugepage / enabled is & # 39; always & # 39 ;.
2019-10-28T04: 07: 55.107-0400 IN CONTROL [initandlisten] ** We suggest setting it to "never"
2019-10-28T04: 07: 55.107-0400 I CONTROL [initandlisten]
---
Activate MongoDB's free cloud-based monitoring service, which then receives and displays
statistics about your distribution (hard disk usage, CPU, operating statistics, etc.).

Monitoring data will be available on a MongoDB website with a unique URL available to you
and who you share the URL with. MongoDB can use this information to create a product
improvements and to suggest MongoDB products and distribution options to you.

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

Create a MongoDB Admin User

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

  mongo  

Then change the database to admin as shown below:

> use admin  

Next, create a new MongoDB user with following command:

> db.createUser (
{
user: "mongodadmin",
pwd: "password123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
[19659008] You should get the following output:

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

Then 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 execute commands. So it is recommended to configure MongoDB authentication to prevent other users from running any command without adequate permissions.

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

  nano /lib/systemd/system/mongod.service Chapter19659009achteChang the following line: 

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

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

  systemctl - system daemon-reload 
systemctl restart mongod

Now you log in to the MongoDB shell and try to see the users without authentication :

  mongo 
> use admin
> show users

You should see the following error:

 2019-10-28T04: 13: 15.346-0400 E QUERY [js] unknown exception : Error: command-userInfo 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 MongoDB:

> db.auth (& # 39; mongodadmin & # 39 ;, & # 39; password123 & # 39;) 
> 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 now have enough knowledge to create your own database and start building an application with MongoDB. Feel free to ask me if you have any questions.


Source link