Saturday, November 16, 2024
Google search engine
HomeData Modelling & AIInstall MongoDB 4.4 on CentOS 7 / Fedora 37/36/35/34

Install MongoDB 4.4 on CentOS 7 / Fedora 37/36/35/34

Welcome to our guide on how to install MongoDB 4.4 on CentOS 7 / Fedora 37/36/35/34. MongoDB is an open source NoSQL database system written in C++. It is designed for high scalability, performance, and availability. MongoDB is good for the following applications:

  • 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

Step 1: Update System

We always work on an updated system

### CentOS ###
sudo yum -y update

### Fedora ###
sudo dnf -y update

Step 2: Add DNF/YUM MongoDB Repositories

The first step in the installation of MongoDB 4 on CentOS 7 / Fedora is to add the repositories to the system.

CentOS 7:

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

Fedora 37/36/35/34:

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

Update Yum cache index:

sudo yum clean all
sudo yum makecache

List available YUM repositories:

$ sudo yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.mirror.constant.com
 * extras: nc-centos-mirror.iwebfusion.net
 * updates: mirror.grid.uchicago.edu
repo id                                                                    repo name                                                                        status
base/7/x86_64                                                              CentOS-7 - Base                                                                  10,072
droplet-agent/x86_64                                                       DigitalOcean Droplet Agent                                                            5
extras/7/x86_64                                                            CentOS-7 - Extras                                                                   498
mongodb-org-4.4                                                            MongoDB Repository                                                                  111
updates/7/x86_64                                                           CentOS-7 - Updates                                                                2,579
repolist: 13,265

Step 3: Install MongoDB 4.4 on CentOS 7 / Fedora

Next is to install mongodb-org package on CentOS 7 / Fedora.

Install MongoDB 4 on Fedora.

sudo dnf -y install mongodb-org

Install MongoDB 4 on CentOS 7:

sudo yum -y install mongodb-org

You can check the version of MongoDB installed using the following command:

$ mongo -version
MongoDB shell version v4.4.4
Build Info: {
    "version": "4.4.4",
    "gitVersion": "8db30a63db1a9d84bdcad0c83369623f708e0397",
    "openSSLVersion": "OpenSSL 1.1.1q  FIPS 5 Jul 2022",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel80",
        "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

Step 4: Configure MongoDB (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

Step 5: Start & Enable 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
 mongod.service - MongoDB Database Server
     Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; preset: disabled)
     Active: active (running) since Wed 2023-01-25 13:24:21 UTC; 1s ago
       Docs: https://docs.mongodb.org/manual
    Process: 2509 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
    Process: 2510 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
    Process: 2511 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
    Process: 2512 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
   Main PID: 2514 (mongod)
     Memory: 57.8M
        CPU: 862ms
     CGroup: /system.slice/mongod.service
             └─2514 /usr/bin/mongod -f /etc/mongod.conf

Jan 25 13:24:20 fedora.mylab.io systemd[1]: Starting mongod.service - MongoDB Database Server...
Jan 25 13:24:21 fedora.mylab.io mongod[2512]: about to fork child process, waiting until server is ready for connections.
Jan 25 13:24:21 fedora.mylab.io mongod[2514]: forked process: 2514
Jan 25 13:24:21 fedora.mylab.io mongod[2512]: child process started successfully, parent exiting
Jan 25 13:24:21 fedora.mylab.io systemd[1]: Started mongod.service - MongoDB Database Server.

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

MongoDB Book

RELATED ARTICLES

Most Popular

Recent Comments