
Welcome to our guide on installing MongoDB 7.0 on CentOS Stream and Fedora Linux. MongoDB is a powerful open-source NoSQL database written in C++, designed for high scalability, excellent performance, and strong reliability. It’s well-suited for a variety of applications, such as:
- Evolving data requirements.
- Blogs and content management.
- Mobile and social networking sites
- E-commerce product catalogue.
- configuration management
- Real-time analytics and high-speed logging
Follow the steps below to install, configure, and start using MongoDB on your CentOS Stream or Fedora Linux system. This guide will walk you through everything from setting up the necessary repositories to getting MongoDB up and running smoothly, so you can begin managing your databases with ease.
1 – Update System
We always work on an updated system
### CentOS ###
sudo yum -y update
### Fedora ###
sudo dnf -y update
2 – Add MongoDB RPM Repository
The first step in the installation of MongoDB 4 on CentOS / Fedora is to add the repositories to the system.
CentOS Stream 9:
cat <<EOF | sudo tee /etc/yum.repos.d/mongodb.repo
[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/9/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-7.0.asc
EOF
CentOS Stream 8:
cat <<EOF | sudo tee /etc/yum.repos.d/mongodb.repo
[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-7.0.asc
EOF
Fedora:
cat <<EOF | sudo tee /etc/yum.repos.d/mongodb.repo
[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/9/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-7.0.asc
EOF
Update Yum cache index:
sudo yum clean all && sudo yum makecache -y
List available YUM repositories:
$ sudo yum repolist
repo id repo name
appstream Rocky Linux 8 - AppStream
baseos Rocky Linux 8 - BaseOS
cri-o CRI-O
docker-ce-stable Docker CE Stable - x86_64
epel Extra Packages for Enterprise Linux 8 - x86_64
extras Rocky Linux 8 - Extras
mongodb-org-7.0 MongoDB Repository
packages-microsoft-com-mssql-server-2019 packages-microsoft-com-mssql-server-2019
packages-microsoft-com-prod packages-microsoft-com-prod
remi-modular Remi's Modular repository for Enterprise Linux 8 - x86_64
remi-safe Safe Remi's RPM repository for Enterprise Linux 8 - x86_64
3 – Install MongoDB on CentOS / Fedora
Next is to install mongodb-org
package on CentOS Stream or Fedora Linux.
sudo yum -y install mongodb-org
You can check the version of MongoDB installed using the following command:
$ mongod --version
db version v7.0.20
Build Info: {
"version": "7.0.20",
"gitVersion": "b6513ce0781db6818e24619e8a461eae90bc94fc",
"openSSLVersion": "OpenSSL 3.2.4 11 Feb 2025",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "rhel90",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
The installation of mongodb-org
package will install:
- mongodb-org-server – This provides MongoDB daemon
- mongod mongodb-org-mongos – This is a MongoDB Shard daemon
- mongodb-org-shell – This provides a shell to MongoDB
- mongodb-org-tools – MongoDB tools used for export, dump, import e.t.c
4 – Additional MongoDB Configurations (optional)
You can customize MongoDB installation before starting the service.
Label MongoDB port
If you have SELinux in enforcing mode, you may need to label port 27017
sudo semanage port -a -t mongod_port_t -p tcp 27017
Allow MongoDB Port on the firewall
If you have an active firewalld
service on your server and would like MongoDB service to be accessible over the network, allow port 27017/tcp
:
sudo yum -y install firewalld && sudo systemctl enable --now firewalld
sudo firewall-cmd --add-port=27017/tcp --permanent
sudo firewall-cmd --reload
You can also limit access based on source address
sudo firewall-cmd --permanent --add-rich-rule "rule family="ipv4" \
source address="192.168.5.0/24" port protocol="tcp" port="27017" accept"
Using secondary disk for MongoDB data (Optional)
You can choose to use a dedicated disk to store MongoDB data.
Step 1: Partition secondary disk for MongoDB data:
$ lsblk | grep vdb
vdb 252:16 0 50G 0 disk
Step 2: Create a GPT partition table for the secondary disk, it can be more than one disk:
sudo parted -s -a optimal -- /dev/vdb mklabel gpt
sudo parted -s -a optimal -- /dev/vdb mkpart primary 0% 100%
sudo parted -s -- /dev/vdb align-check optimal 1
Step 3: Create LVM volume, this will make it easy to extend the partition:
sudo pvcreate /dev/vdb1
sudo vgcreate vg0 /dev/vdb1
sudo lvcreate -n mongo -l 100%FREE vg0
Step 4: Create XFS
filesystem on the Logical Volume created.
$ sudo mkfs.xfs /dev/mapper/vg0-mongo
meta-data=/dev/mapper/vg0-mongo isize=512 agcount=4, agsize=6553344 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=26213376, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=12799, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
Step 5: Create a mount point and mount the partition
echo "/dev/mapper/vg0-mongo /var/lib/mongo xfs defaults 0 0" | sudo tee -a /etc/fstab
sudo mount -a
sudo chown -R mongod:mongod /var/lib/mongo
sudo chmod -R 775 /data/mongo
Step 7: Confirm that the partition mount was successful:
$ df -hT | grep /var/lib/mongo
/dev/mapper/vg0-mongo xfs 50G 33M 50G 1% /var/lib/mongo
Step 8: Set MongoDB data store location
$ sudo vim /etc/mongod.conf
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
5 – Start MongoDB Service
The last step is to start and enable MongoDB service to start on boot
sudo systemctl start mongod.service
sudo systemctl enable mongod.service
Check status by running:
systemctl status mongod.service
Connect to MongoDB shell
When MongoDB server is running, we can use MongoDB shell by executing mongosh
command:
$ sudo mongosh
Current Mongosh Log ID: 6694d09ccf71458329482f8a
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.12
Using MongoDB: 7.0.12
Using Mongosh: 2.2.12
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
------
The server generated these startup warnings when booting
2024-07-15T07:32:15.347+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2024-07-15T07:32:15.347+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' in this binary version
2024-07-15T07:32:15.347+00:00: vm.max_map_count is too low
------
test>
For Authentication, check our guide on How to configure MongoDB authentication.
This tutorial doesn’t work on fedora 36, there is a dependecy fail when you install mongo.
Hello. Please try with updated article and let us know if it works.
Hi Josphat! Thanks for the update, works like a charm!
Cool thanks.