Table of Contents
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.
This guide will walk you through setting up MongoDB on Red Hat Enterprise Linux (RHEL) and CentOS Linux:
- RHEL / CentOS 8
- RHEL / CentOS 7
- RHEL / CentOS 6
Update your system before beginning installation.
# sudo yum update
# sudo reboot
Install MongoDB Community Edition
Install MongoDB with the yum package manager.
-
Create the /etc/yum.repos.d/mongodb-org-4.4.repo file for MongoDB.
# nano /etc/yum.repos.d/mongodb-org-4.4.repo
-
Paste the following into the editor:
[mongodb-org-4.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
-
Save and exit the file.
-
To install the latest stable version of MongoDB:
# sudo yum install -y mongodb-org
If you need to install a specific version or prevent automatic upgrades, see the MongoDB documentation.
Setup MongoDB
-
Start MongoDB.
# systemctl start mongod.service # systemctl enable mongod.service
-
Connect to MongoDB.
# mongo
Securing MongoDB
MongoDB is not secure by default. If you are installing MongoDB and launching it without configuring it for authentication, you are going to have a bad time. People can read, write, destroy, or alter data on your server without ever needing to login or authenticate in anyway. Securing the database is not hard to do and can be done in a few steps.
Secure MongoDB
First, start up your Mongo client. On Linux it is the command mongo
. Enter this block of text in, of course changing the placeholder parts to your own information.
db.createUser({
user: "USERNAME",
pwd: "PASSWORD",
roles: [
{
role: "readWrite",
db: "YOUR_DATABASE"
}
]
});
After you’re done, quit the mongo client and edit your MongoDB configuration file. Depending on your OS and distro, you will find it in one of these places.
/etc/mongodb.conf
/etc/mongod.conf
Change the following line, #security:
to the following.
security:
authorization: enabled
You should consider changing the bind port to localhost (127.0.0.1
) or bind it to a private IP that does not get exposed to the internet. Exposing your database to the internet is just a bad idea in general. This is what you should change.
# network interfaces
net:
port: 27017
bindIp: 634.234.102.6
Mind your spaces! Always in twos, never tabs. Afterwards restart your MongoDB database. On Linux it will be one of the following commands based on your distro of choice.
systemctl restart mongod
systemctl restart mongodb
Hope this guide will help you.
Latest Posts

Oppo Reno8 T

How To Install QElectroTech on Ubuntu 20.04 | 22.04 LTS

Extracting Embedded Images from PDF: A Step-by-Step Guide

10 Best Free and Open Source Video Editing Software

How to Set Up NFS Share on Debian
Trending Posts

How To Install Chrony (NTP) On CentOS 8, 7 & RHEL 8, 7

TAR Command and Examples

How To Install Google Chrome On macOS

How to Upgrade Windows 10 to Windows 11
