How to install MongoDB Server on Linux 9

Share on Social Media

In this tutorial, you will learn how to install MongoDB Server on Linux 9 that includes all Red Hat based distributions.

What is MongoDB?:

MongoDB is a leading NoSQL database that revolutionizes data storage and retrieval. It employs a document-oriented model, allowing for flexible and dynamic schemas. Each record, or document, can store data in a JSON-like format, making it exceptionally adaptable to evolving data structures. This flexibility empowers developers to work with complex data sets, accelerating development cycles.

One of MongoDB’s key strengths is its scalability. It excels at handling large volumes of data and high transaction loads, making it a go-to choice for applications with extensive data requirements. Its distributed architecture enables seamless horizontal scaling across multiple nodes, ensuring robust performance under heavy workloads.

Furthermore, MongoDB boasts exceptional speed in data retrieval and querying. It leverages indexes, sharding, and a powerful query language to swiftly locate and manipulate data. This capability is crucial for real-time applications and analytics, providing users with rapid access to critical information.

Security is a paramount concern in modern databases, and MongoDB addresses this with advanced features. It supports authentication, authorization, and encryption, safeguarding data from unauthorized access or tampering. Additionally, role-based access control and auditing mechanisms grant administrators fine-grained control over user permissions.

Read Also: How to install MongoDB on CentOS 8

In conclusion, MongoDB stands as a versatile and high-performance NoSQL database, favored by developers for its flexibility, scalability, and speed. Its document-oriented approach and distributed architecture make it an ideal choice for applications dealing with diverse and voluminous data sets. With robust security features, MongoDB ensures that sensitive information remains protected. By embracing MongoDB, businesses can build powerful, efficient, and secure applications that thrive in today’s data-driven landscape.

Video to install MongoDB Server on Linux:

YouTube player

Environment Specification:

We are using a minimal Rocky Linux 9 virtual machine with following specifications.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 4 GB
  • Storage – 40 GB
  • Operating System – Rocky Linux release 9.2 (Blue Onyx)
  • Hostname – mongodb-01.centlinux-com.preview-domain.com
  • IP Address – 192.168.18.83/24

Server Initial Setup:

Login to your Rocky Linux server as root user by using a ssh client.

Set a hostname and Local DNS resolution for your Linux machine.

# hostnamectl set-hostname mongodb-01.centlinux-com.preview-domain.com
# echo "192.168.18.83 mongodb-01 mongodb-01.centlinux-com.preview-domain.com" >> /etc/hosts

As per best practice, update software packages in your Linux operating system before installing MongoDB database server.

# dnf update -y

Sometimes, the above command updates your Linux Kernel. If it happens then you should reboot your Linux OS before installing MongoDB software.

# reboot

After reboot, check the version of Linux OS and Kernel, that are being used in this installation guide.

# cat /etc/os-release
NAME="Rocky Linux"
VERSION="9.2 (Blue Onyx)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Rocky Linux 9.2 (Blue Onyx)"
ANSI_COLOR="0;32"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:rocky:rocky:9::baseos"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
SUPPORT_END="2032-05-31"
ROCKY_SUPPORT_PRODUCT="Rocky-Linux-9"
ROCKY_SUPPORT_PRODUCT_VERSION="9.2"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.2"

# uname -r
5.14.0-284.30.1.el9_2.x86_64

Install MongoDB Server on Linux:

To install MongoDB server on Linux, you need to add Official yum repository of MongoDB in your Linux OS.

Create a repo file by using vim text editor.

# vi /etc/yum.repos.d/mongodb-org-7.0.repo

Add following lines in this file.

[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc

Save and exit from vim text editor by using :wq command.

Build yum cache for newly added yum repository.

# dnf makecache
MongoDB Repository                              7.0 kB/s |  14 kB     00:02
Rocky Linux 9 - BaseOS                          1.7 kB/s | 4.1 kB     00:02
Rocky Linux 9 - AppStream                       626  B/s | 4.5 kB     00:07
Rocky Linux 9 - Extras                          1.9 kB/s | 2.9 kB     00:01
Metadata cache created.

Execute following dnf command to install MongoDB database server.

# dnf install -y mongodb-org

After MongoDB installation, check the version of your NoSQL database server by executing following command.

# mongod --version
db version v7.0.2
Build Info: {
    "version": "7.0.2",
    "gitVersion": "02b3c655e1302209ef046da6ba3ef6749dd0b62a",
    "openSSLVersion": "OpenSSL 3.0.7 1 Nov 2022",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel90",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Post Installation Configuration:

Execute following command to delete the temporary file: mongodb-27017.sock.

# rm -f /tmp/mongodb-27017.sock

Enable and start mongod service.

# systemctl enable --now mongod

Check the status of mongod service for any possible errors.

# systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; preset: disabled)
     Active: active (running) since Mon 2023-10-16 18:34:44 PKT; 1min 22s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 1707 (mongod)
     Memory: 86.8M
        CPU: 2.711s
     CGroup: /system.slice/mongod.service
             └─1707 /usr/bin/mongod -f /etc/mongod.conf

Oct 16 18:34:44 mongodb-01.centlinux-com.preview-domain.com systemd[1]: Started MongoDB Database Server.
Oct 16 18:34:44 mongodb-01.centlinux-com.preview-domain.com mongod[1707]: {"t":{"$date":"2023-10-16T13:34:44.539Z"},"s":"

MongoDB 7.0 requires Kernel parameter:vm.max_map_count set to a higher value. Therefore, execute following command to do this.

# echo "vm.max_map_count=262144" >> /etc/sysctl.d/99-mongod.conf

Your NoSQL database also required to disable Transparent Hugepages. Therefore, edit GRUB configuration file by using vim text editor.

# vi /etc/default/grub

Find and edit GRUB_CMDLINE_LINUX variable.

GRUB_CMDLINE_LINUX="resume=/dev/mapper/rl-swap rd.lvm.lv=rl/root rd.lvm.lv=rl/swap transparent_hugepage=never"

Regenerate grub.cfg file based on new settings.

# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Adding boot menu entry for UEFI Firmware Settings ...
done

Reboot your Linux OS to apply changes.

# reboot

Secure your MongoDB Server:

Execute mongosh (MongoDB Shell) to login to your NoSQL server.

# mongosh
Current Mongosh Log ID: 652d43352747d53396b68cdc
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.0.1
Using MongoDB:          7.0.2
Using Mongosh:          2.0.1
mongosh 2.0.2 is available for download: https://www.mongodb.com/try/download/shell

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2023-10-16T19:04:37.082+05:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------

test>

Create an database admin user and set a password for it.

test> use admin
switched to db admin
admin> db.createUser(
... {
... user: "mongouser",
... pwd: passwordPrompt(), // or cleartext password
... roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
... }
... )
Enter password
**********{ ok: 1 }
admin> quit

Login to your NoSQL database server by using the newly created user.

# mongosh -u mongouser
Enter password: **********
Current Mongosh Log ID: 652d44176a7b04cc4b2967b6
Connecting to:          mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.0.1
Using MongoDB:          7.0.2
Using Mongosh:          2.0.1
mongosh 2.0.2 is available for download: https://www.mongodb.com/try/download/shell

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2023-10-16T19:08:53.218+05:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------

test> db
test

Important Directories:

  • Default Data Directory: /var/lib/mongo
  • Default Log Directory: /var/log/mongodb

Conclusion:

In this installation guide, you have successfully learned to install MongoDB server on Linux 9.