MongoDB is a free and open source document database. It belongs to a family of databases called NoSQL. NoSQL is different from traditional relational databases, for example: MySQL and PostgreSQL.
In MongoDB, data is flexibly stored as documents in a JSON-like form. It does not require a pre-defined schema, and the data structure can always be changed.
This guide explains how to install and configure MongoDB Community Edition on CentOS 8 server.
MongoDB is not available on the CentOS 8 core software source. We will enable the official MongoDB software source and install the software package.
At the time of writing this article, the latest MongoDB version on the official MongoDB software source is 4.2. Before starting the installation, visit the MongoDB document Install on Red Hat to check if there is a newer release version.
Perform the following steps as root or a user with sudo privileges to install MongoDB on CentOS 8 system:
mongodb-org.repo
in the /etc/yum.repos.d/
directory to start the MongoDB source:sudo nano /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.asc
If you want to install an older version of MongoDB, replace 4.2 with the version you want.
mongodb-org
meta-package:sudo dnf install mongodb-org
During the installation process, you will be prompted to insert the MongoDB GPG key. Type y
and press Enter
to enter.
The following packages will be installed on your system as part of the mongodb-org
package:
mongodb-org-server
-mongod
daemon, corresponding initialization script and configuration. mongodb-org-mongos
-mongos
daemon. mongodb-org-shell
-mongo shell, an interactive JavaScript interface for MongoDB, mainly used to perform some management tasks through the command line. mongodb-org-tools
-Contains some MongoDB tools, such as: data import tools, data export tools, data statistics tools, etc.sudo systemctl enable mongod --now
mongo
Run the following command to display the MongoDB version number:
db.version()
The output might look like this:
4.2.3
The MongoDB configuration file is named: mongod.conf
, located in the /etc
directory. This file is in YAML
format.
The default configuration is suitable for most user scenarios. In any case, for production environments, we recommend opening the security chapter and enabling user authentication: /etc/mongod.conf
security:
authorization: enabled
This authorization
option enables Role-Based Access Control (RBAC)
, which specifies the user's access rules for data and operations. If this option is disabled, then every user can access any data and perform any operation.
After making any changes to the MongoDB configuration file, restart the mongod service:
sudo systemctl restart mongod
For information about MongoDB configuration options, please visit Configuration File Options Document Page.
If you have enabled MongoDB user authentication, you need to create an administrator user who can access and manage MongoDB instances.
First, access the MongoDB shell:
mongo
Enter the following command to connect to the admin
database:
use admin
switched to db admin
Create a new user with the name mongoAdmin
and assign the role of userAdminAnyDatabase
:
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:
quit()
To test the modification, use the administrator account you created earlier to access the mongo shell:
mongo -u mongoAdmin -p --authenticationDatabase admin
MongoDB shell version v4.2.3
Enter password:
use admin
switched to db admin
Now, print the user:
show users
{"_ id":"admin.mongoAdmin","user":"mongoAdmin","db":"admin","roles":[{"role":"userAdminAnyDatabase","db":"admin"}],"mechanisms":["SCRAM-SHA-1","SCRAM-SHA-256"]}
We have shown you how to install and configure MongoDB 4.2 on your CentOS 8 server.
Read The MongoDB 4.2 Manual to find more information on this topic.
Recommended Posts