Introduction
MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL).
MongoDB is a popular document-based and general-purpose NoSQL database engine that stores data in JSON format.
In this tutorial, you will learn how to install MongoDB on CentOS 8.
Install MongoDB
Just follow the steps below
- Add MongoDB Repository
vim /etc/yum.repos.d/mongodb.repo
- Then add the following:
[mongodb-org-5]
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
- The next step will be to install MongoDB using the command below:
dnf install mongodb-org
- Start and enable MongoDB
systemctl start mongod
systemctl enable mongod
- Check status of MongoDB
systemctl status mongod
![](https://unixcop.com/wp-content/uploads/2022/01/image-130-1024x333.png)
- Confirm that Mongod service is listening.
netstat -ntlpu
OR
netstat -ntlpu | grep -i mongo
![](https://unixcop.com/wp-content/uploads/2022/01/image-131.png)
- Access MongoDB Shell with the below command
mongo
Output:
![](https://unixcop.com/wp-content/uploads/2022/01/image-132-1024x452.png)
- Create a MongoDB Admin User
- switch to the database admin
> use admin
- Then create a new MongoDB user by running the code below
> db.createUser(
{
user: "mongod_admin",
pwd: "unixcop@Password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
The output should be as shown below:
![](https://unixcop.com/wp-content/uploads/2022/01/image-133.png)
- List MongoDB users with:
> show users
![](https://unixcop.com/wp-content/uploads/2022/01/image-134.png)
- Configure the authentication for MongoDB, because all users can access the shell and execute any commands, which is not recommended at all for security purposes.
- To enable authentication open the /lib/systemd/system/mongod.service file.
vim /lib/systemd/system/mongod.service
- Then edit the Environment parameter as shown.
Environment="OPTIONS= --auth -f /etc/mongod.conf"
![](https://unixcop.com/wp-content/uploads/2022/01/image-135.png)
- Reload the system and restart MongoDB.
systemctl daemon-reload
systemctl restart mongod
- Again, Access the mongoDB shell with:
mongo
- Then list users after switching to admin user, just type:
> use admin
> show users
You should get an error as shown.
![](https://unixcop.com/wp-content/uploads/2022/01/image-136.png)
- To authenticate, simply pass the credentials as shown.
> db.auth('mongod_admin', 'unixcop@Password')
- List users again
> show users
![](https://unixcop.com/wp-content/uploads/2022/01/image-137.png)
- To exit the database engine run:
> exit
![](https://unixcop.com/wp-content/uploads/2022/01/image-138.png)
Conclusion
That’s it…
In this guide, we showed you how to install MongoDB on CentOS 8.