We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – How To Install MongoDB 4 on CentOS 7 / RHEL 7

Learn how to – How To Install MongoDB 4 on CentOS 7 / RHEL 7.

In this guide, we will cover the steps to install the latest release of MongoDB community edition on your CentOS 7 server. MongoDB is an open source NoSQL database system written in C++ designed to ensure scalability, high performance, and availability.

MongoDB common use case is storage and management of Big Data-sized collections of literal documents like text documents, email messages, XML documents, and many others.

Install MongoDB 4 on CentOS 7 / RHEL 7

MongoDB 4 is installed on CentOS 7 / RHEL 7 using the upstream repository. Add the repository to your CentOS 7 / RHEL 7 server by running below commands:

cat <<EOF | sudo tee /etc/yum.repos.d/mongodb.repo
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
EOF

Once the repo has been added, install mongodb-org package

sudo yum install mongodb-org

Accept installation process by pressing the y key in your keyboard:

....

Transaction Summary
======================================================================================================================================================================================================
Install  1 Package  (+9 Dependent packages)
Upgrade             ( 1 Dependent package)

Total download size: 100 M
Is this ok [y/d/N]: y
.....
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                  44 MB/s | 100 MB  00:00:02
Retrieving key from https://www.mongodb.org/static/pgp/server-4.4.asc
Importing GPG key 0x90CFB1F5:
 Userid     : "MongoDB 4.4 Release Signing Key <packaging@mongodb.com>"
 Fingerprint: 2069 1eec 3521 6c63 caf6 6ce1 6564 08e3 90cf b1f5
 From       : https://www.mongodb.org/static/pgp/server-4.4.asc
Is this ok [y/N]: y
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-9.2009.1.el7.centos.x86_64 (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Is this ok [y/N]: y

The installation of the above package will install the following dependency packages:

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

Configure MongoDB on CentOS 7 / RHEL 7

When the packages are installed, you can start customizing and configuring MongoDB 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

Open port on the firewall

If you have firewalld running on your server and would like MongoDB service to be accessible over the network, open it on the firewall:

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="10.1.2.0/24" port protocol="tcp" port="27017" accept"

Using secondary disk for MongoDB data (Optional)

You can always use a dedicated disk / virtual disk to store MongoDB data. This can be configured like below

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 onde 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
  Physical volume "/dev/vdb1" successfully created.

$ sudo vgcreate vg11 /dev/vdb1
  Volume group "vg11" successfully created

$ sudo lvcreate -n data -l 100%FREE vg11
Logical volume "data" created

Step 4: Create XFS filesystem on the Logical Volume created

$ sudo mkfs.xfs /dev/mapper/vg11-data
meta-data=/dev/mapper/vg11-data 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

$ sudo vi /etc/fstab
/dev/mapper/vg11-data /data xfs defaults 0 0

$ sudo mkdir /data
$ sudo mount -a

Step 6: Create a folder for MongoDB data

sudo mkdir /data/mongo
sudo chown -R mongod:mongod /data/mongo
sudo chmod -R 775 /data/mongo

Step 7: Confirm that the partition mount was successful:

$ df -hT | grep  /var/lib/mongo
/dev/mapper/vg11-mongodb xfs        50G   33M   50G   1% /var/lib/mongo

Step 8: Change MongoDB data store location

$ sudo vi /etc/mongod.conf
storage:
dbPath: /data/mongo 
journal:
  enabled: true

Starting MongoDB Service

When all is set, start and set mongod service to start on boot.

sudo systemctl enable --now mongod
sudo systemctl status mongod

For Authentication, check our guide on How to configure MongoDB authentication.

Cart

Your Cart is Empty

Back To Shop