This tutorial covers all the steps required to install and configure MongoDB 7.0 on Rocky Linux 8 / Alma Linux 8.

MongoDB is a popular, versatile and modern non-relational database used for developing dynamic applications. It stores its data as separate documents in collections, and not rows and columns as used in relational databases. MongoDB is cross-platform and still offers support for both 32 and 64-bit systems.

Developed and maintained by MongoDB Inc., MongoDB offers the Community and Enterprise editions. The Community Edition is a free version that provides limited features and capabilities. The Enterprise Edition is provided as part of the Mongo Enterprise Advanced subscription. It provides advanced features and capabilities such as LDAP and Kerberos support, on-disk encryption, auditing and support for MongoDB development.

The first version of MongoDB was made available in 2009. The release was then followed by several other releases that focused on validating a new and largely unproven approach to database design. over the years, MongoDB has made several releases that attracted adoption not only from startups but also enterprises.

Below is a diagram that illustrates how MongoDB helps developers build with data for any class of modern application, all accessed via a unified API.

MongoDB 7.0 on Rocky Alma Linux 1

The latest release on MongoDB is version 7.0 comes with a lot of cool features and benefits that include:

  • Enhanced performance: In MongoDB 7.0, there are several performance improvements when working with time series data especially those that require high-volume datasets of all shapes. The enhancements lead to increased storage optimization, compression and query performance.
  • Streamlined developer experience: There are several improvements that include approximate percentiles, compound wildcard indexes, and bitwise operators just to ensure that developers enjoy performance and flexibility when indexing and querying data on MongoDB. This new release also enabled developers to implement user role variables within aggregation pipelines which enables a single view to display different data based on the permissions assigned to the user.
  • Stronger security: In MongoDB 7.0, there are improvements made to security with Queryable Encryption. This helps users to encrypt sensitive workloads and run equality queries on fully encrypted data.
  • Smoother migrations: There are enhancements made to make cluster-to-cluster sync (mongosync) more efficient in several scenarios. The upgrade now makes cluster-to-cluster more flexible when syncing between clusters with unlike topologies. Such an example is from replica sets to sharded clusters.

Now let’s dive in!

1. Configure MongoDB 7.0 Repo

The first step involves adding the MongoDB 7.0 repository to the Rocky Linux 8|Alma Linux 8. This can be achieved using the below command:

sudo tee /etc/yum.repos.d/mongodb-org-7.0.repo<<EOF
[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://www.mongodb.org/static/pgp/server-7.0.asc
EOF

Alternatively, you can download and install the RPM files directly from the MongoDB repository. On the page, select the required version to be installed correctly.

2. Install MongoDB 7.0

Once added, you can easily install MongoDB 7.0 from the YUM repository using the command:

sudo dnf install mongodb-org

Dependency Tree:

...
Transaction Summary
==============================================================================================
Install  8 Packages

Total download size: 159 M
Installed size: 603 M
Is this ok [y/N]: y

It is also possible to specify the versions of independent packages during the installation by replacing the desired versions here:

sudo dnf install -y mongodb-org-<version> mongodb-org-database-<version> mongodb-org-server-<version> mongodb-org-shell-<version> mongodb-org-mongos-<version> mongodb-org-tools-<version>

Once installed, start and enable the service:

sudo systemctl enable --now mongod

Check if the service is running:

$ systemctl status mongod
 mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2023-08-28 11:08:31 EDT; 2s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 15743 (mongod)
   Memory: 73.5M
   CGroup: /system.slice/mongod.service
           └─15743 /usr/bin/mongod -f /etc/mongod.conf

3. Configure MongoDB 7.0 after install

MongoDB stores its configuration file in the /etc/mongod.conf file. In this file, there are several modifications you can make to MongoDB.

To make adjustments, we will stop the service:

sudo systemctl stop mongod

Now open the file for editing:

sudo vim /etc/mongod.conf

The configurations you can make here include:

a. Securing MongoDB

You can enable password authentication on MongoDB by uncommenting/modifying the below line:

security:
  authorization: enabled

b. Enable Remote Access

To allow MongoDB to be accessed remotely, you need to make adjustments to the bind address. For this guide, we will allow the service to bind to all interfaces:

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

c. Change the default Data Path

There are situations when you want MongoDB to store its data in a custom path and not the default /var/lib/mongo path. For that case, you need to specify your path as shown:

  path: /data/log/mongodb/mongod.log   #where to write logging data.
...
  dbPath: /data/mongo    #Where and how to store data.

Once the storage and log paths have been modified. save the file and create the paths on your system.

sudo mkdir -p /data/mongo
sudo mkdir -p /data/log/mongodb
sudo touch /data/log/mongodb/mongod.log
sudo chown -R mongod:mongod /data

Proceed and make SELinux configurations. Begin by installing the required packages:

sudo dnf install policycoreutils-python-utils checkpolicy

Next, create the custom check policy for MongoDB:

cat > mongodb_cgroup_memory.te <<EOF
module mongodb_cgroup_memory 1.0;

require {
    type cgroup_t;
    type mongod_t;
    class dir search;
    class file { getattr open read };
}

#============= mongod_t ==============
allow mongod_t cgroup_t:dir search;
allow mongod_t cgroup_t:file { getattr open read };
EOF

Compile and apply the policy:

checkmodule -M -m -o mongodb_cgroup_memory.mod mongodb_cgroup_memory.te
semodule_package -o mongodb_cgroup_memory.pp -m mongodb_cgroup_memory.mod
sudo semodule -i mongodb_cgroup_memory.pp

Now load these contexts:

##For Log File Directory
sudo semanage fcontext -a -t mongod_log_t '/data/log/mongodb/mongod.*'
sudo chcon -Rv -u system_u -t mongod_log_t '/data/log/mongodb/mongod.log'
sudo restorecon -R -v '/data/log/mongodb/mongod.log' 

##For Data Directory
sudo semanage fcontext -a -t mongod_var_lib_t '/data/mongo.*'
sudo chcon -Rv -u system_u -t mongod_var_lib_t '/data/mongo'
sudo restorecon -R -v '/data/mongo'

Once all the modifications have been made, restart the MongoDB service:

sudo systemctl daemon-reload
sudo systemctl restart mongod

4. Access and Use MongoDB 7.0

To verify if MongoDB 7.0 is working, we will connect to it locally with the command:

$ mongosh
Current Mongosh Log ID:	64ec790c011395232344b701
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.6
Using MongoDB:		7.0.0
Using Mongosh:		1.10.6

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.

test> 

Once connected, you can then create a testuser with password as shown:

use admin
db.createUser(
{
user: "testuser",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)

To test if the user has been added, exit the shell:

> exit
bye

Now try accessing MongoDB 7.0 using the user and the set password.

$ mongosh -u testuser -p --authenticationDatabase admin
Enter password: *********
Current Mongosh Log ID:	64ecb93c1d96beea693bb345
Connecting to:		mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&authSource=admin&appName=mongosh+1.10.6
Using MongoDB:		7.0.0
Using Mongosh:		1.10.6

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

test> 

Since the user has admin privileges, we can create a test database and add collections to it:

use testdb
db.userdetails.insertOne(
   {F_Name: "fist name",
    L_NAME: "last name",
    ID_NO: "12345",
     AGE: "49",
     TEL: "+254654671"
   }
)

Verify if the collections have been added to the new testdb database

> show collections
userdetails

Voila!

Conclusion

Now that you have MongoDB 7.0 successfully installed on your Rocky Linux 8 / Alma Linux 8, feel free to explore other features offered by this new release. I hope this guide was of importance to you.

See more?

LEAVE A REPLY

Please enter your comment!
Please enter your name here