There are two main types of databases. Relational databases that use SQL to structure their data in tables, and non-relational databases that store data in any other way except using SQL tables. The non-relational databases are also referred to as NoSQL databases. NoSQL in full could be Non-SQL or Not-only SQL, whichever you choose to go with.

NoSQL databases are known for working with big data in large enterprises. Big data involves large sets of data that cannot be processed with traditional known methods. Data is stored in different methods like document databases, key-value databases, wide-column stores, and graph databases. Several known types of NoSQL databases include MongoDB, DynamoDB, CouchBase, Redis, Cassandra, etc.

MongoDB NoSQL Database

MongoDB is a type of NoSQL database that is open-source, cross-platform, and uses JSON-like documents with optional schemas. MongoDB is written in C++ and provides fast performance, high availability, and automatic scaling for high-volume data storage.

Features

  • Replication for better data availability and stability.
  • Offers Load Balancing support for fast data processing.
  • Ad-hoc queries for real-time analytics.
  • Indexing for better query executions.
  • Support for multiple storage engines.
  • Duplication of data.
  • Schema-less NoSQL database.
  • Horizontal scalability.
  • support map aggregation

There are options available for MongoDB use.

  • MongoDB Atlas is a multi-cloud developer platform that provides a fully managed database service for modern applications.
  • MongoDB Enterprise Server which is the commercial edition with added capabilities.
  • MongoDB Community Server is a free distributed database with a flexible document data model.

This guide will show you how to install the MongoDB database on CentOS 9|AlmaLinux 9|RHEL 9.

Install MongoDB on CentOS 9|AlmaLinux 9|RHEL 9

Update our system packages.

sudo yum update -y

After restart, create a repository file from which you can install MongoDB directly using the yum package.

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

Install MongoDB database.

sudo yum install -y mongodb-org

Start the MongoDB service.

sudo systemctl start mongod

If you receive an error while trying to start, run the following command then start the service again.

sudo systemctl daemon-reload

To enable the service on boot run the following command.

sudo systemctl enable mongod

Check its status by running the status command.

$ systemctl status mongod
 mongod.service - MongoDB Database Server
     Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
     Active: active (running) since Wed 2022-11-02 11:14:38 UTC; 58s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 1248 (mongod)
     Memory: 69.5M
        CPU: 1.314s
     CGroup: /system.slice/mongod.service
             └─1248 /usr/bin/mongod -f /etc/mongod.conf

Nov 02 11:14:37 rocky9.mylab.io systemd[1]: Starting MongoDB Database Server...
Nov 02 11:14:37 rocky9.mylab.io mongod[1246]: about to fork child process, waiting until server is ready for connections.
Nov 02 11:14:37 rocky9.mylab.io mongod[1248]: forked process: 1248
Nov 02 11:14:38 rocky9.mylab.io mongod[1246]: child process started successfully, parent exiting
Nov 02 11:14:38 rocky9.mylab.io systemd[1]: Started MongoDB Database Server.

Try and access the MongoDB shell using the following command.

$ mongosh

Create a Database on MongoDB

Once logged in the MongoDB shell, use the following command to create a database.

> use Database-Name

Then it will switch to the new database

> use technixleo
switched to db technixleo

To verify the new database is created, use the following command.

> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
technixleo  0.000GB

To drop a database, use the following command. Ensure the database you want to drop is the one you are currently using.

> db.dropDatabase()

Configure custom MongoDB data directory

By default, MongoDB runs in the /var/lib/mongod which is the default directory. To set a new directory, we follow the following steps.

Stop the MongoDB service first.

sudo systemctl stop mongod

Create a new directory for the MongoDB Project files.

sudo mkdir -p /data/MongoDB

Edit the configuration file to add the new directory.

 sudo vi /etc/mongod.conf

Edit the path for your new directory.

dbPath: /data/MongoDB

Save and exit the file.

Edit the user’s permissions to the new directory.

sudo chown -R mongod:mongod  /data/MongoDB

Update the SELinux to allow mongod service to use the new directory

sudo semanage fcontext -a -t mongod_var_lib_t ' /data/MongoDB.*'

Run the following command to update security context changes

sudo chcon -Rv -u system_u -t  mongod_var_lib_t ' /data/MongoDB'

Run the following command to apply changes to the new directory.

sudo restorecon -R -v ' /data/MongoDB'

Start the MongoDB service.

sudo systemctl restart mongod

Use the following command to check the context changes.

$ ls -lZ /MongoDB
drwxr-xr-x. 4 mongod mongod system_u:object_r:mongod_var_lib_t:s0 4096 Jun 15 15:26 mongo

Uninstall MongoDB from CentOS 9|AlmaLinux 9|RHEL 9

Stop the MongoDB service.

sudo systemctl stop mongod

Remove any MongoDB packages installed using the following command.

sudo yum erase $(rpm -qa | grep mongodb-org)

Remove data directories.

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongo

Note that these commands contain the path of default directories. If you changed yours, you will have to use that directory to remove those files. For example;

sudo rm -r /MongoDB

Conclusion

From this guide, You have gone through the basic concepts of the NoSQL database and MongoDB database. We have also installed MongoDB on CentOS 9|AlmaLinux 9|RHEL 9 systems. MongoDB provides a fast way of processing large sets of data without the use of the known traditional methods of structuring data in SQL tables.

More guides on RHEL 9 based systemd:

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here