In this guide, we are going to look at how to install MongoDB 4.4 on Oracle Linux 8. MongoDB is a document-oriented NoSQL database preferred for high volume data storage. Unlike the traditional databases that uses relational databases, MongoDB uses collection of documents. The documents have key-value pairs which are the basic units of data in MongoDB. The collections also have set of documents and functions equating to the relational databases tables. MongoDB offers both a Community and an Enterprise version of the database:

  • MongoDB Community: The available and free to use edition of MongoDB
  • MongoDB Enterprise: This is available as part of the MongoDB Enterprise Advanced subscription and comes with comprehensive support for MongoDB deployment. It includes features such as LDAP and Kerberos support, on-disk encryption, and auditing.

In this guide, we are going to look at the installation on MongoDB 4.4 Community Edition on Oracle Linux 8.

What are the features of MongoDB?

The following are some of the important features of MongoDB that would make you want to use MongoDB.

  • Indexing – you can index any field in the document
  • Replication – supports master slave replication
  • Support ad hoc queries – In MongoDB, you can search by field, range query and it also supports regular expression searches.
  • Load balancing – It has an automatic load balancing configuration because of data placed in shards.
  • Data duplication – MongoDB can run over multiple servers. The data is duplicated to keep the system up and also keep its running condition in case of hardware failure.
  • Supports map reduce and aggregation tools.
  • Uses JavaScript instead of Procedures.
  • It is a schema-less database written in C++.
  • Provides high performance.
  • Stores files of any size easily without complicating your stack.
  • Easy to administer in the case of failures.
  • Supports JSON data model with dynamic schemas, Auto-sharding for horizontal scalability and Built in replication for high availability.

Install MongoDB 4.4 on Oracle Linux 8

Use the following steps to install MongoDB 4.4 on Oracle Linux 8. MongoDB packages are available in the official project provided repositories. This will be added to the system prior to the installation of MongoDB 4.4 on Oracle Linux 8.

Step 1: Update your system packages

Always start by ensuring your system is up to date before installations.

sudo dnf -y update

Step 2: Add MongoDB 4.4 repository

We are going to create a file /etc/yum.repos.d/mongodb-org-4.4.repo so that we can install MongoDB 4.4 directly using yum

Add the following content and save the repository file.

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

Step 3: Install MongoDB packages

Having added repository, we will go ahead to install the latest version of MongDB using the below command:

sudo dnf install -y mongodb-org

To install a specific release of MongoDB, you need to specify each component with the version as shown:

sudo dnf install -y mongodb-org mongodb-org-server mongodb-org-shell mongodb-org-mongos mongodb-org-tools

Note that you can specify any version to install. Yum, however, upgrades the packages whenever there is a new version. To prevent this upgrade, pin the packages by editing /etc/yum.conf file. Add the below line to pin MongoDB packages.

$ exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools

Step 4: Run MongoDB community edition on Oracle Linux 8

The next steps discuss the necessary prerequisites for running MongoDB 4.4

ulimit

Unix-like operating systems mostly limit the resources a process can have and this could affect the functionality of MongoDB. This should be adjusted accordingly. A ulimit value below 64000 will not work for MingoDB 4.4

Directory Paths

By default, MongoDB uses the below directories with the user mongod

  • /var/lib/mongo (the data directory)
  • /var/log/mongodb (the log directory)

Note that if you installed MongoDB using repository, the directories will be created alongside the user mongod. However, if you installed through downloading tarballs, the directory will not be created and you will need to manually create them.

Create MongoDB directories

Run the below commands if you need to manually create MongoDB directories.

sudo mkdir -p /var/lib/mongo
sudo mkdir -p /var/log/mongodb

Set the owner and group of these directories to mongod user.

sudo chown -R mongod:mongod <directory>

Use Custom Directories for MongoDB

To use other directory other that the default MongoDB directory, we will first create the directory then edit mongodb.conf file and modify the path parameters.

$ sudo mkdir /some/data/directory
$ sudo mkdir  /some/log/directory/mongod.log

Open /etc/mongodb.conf file and edit as shown then save.

sudo vim /etc/mongodb.conf

Look out for systemLog and storage parameters and update your new directories accordingly.

storage:
  dbPath /some/data/directory
systemLog:
  path  /some/log/directory/mongod.log

Ensure that mongod user has access to the created directories

$ sudo chown -R mongod:mongod <directory>

Step 5: Configure SElinux

SElinux policy prevents MongoDB from accessing /sys/fs/cgroup, required for determining the available memory on your system. To run Selinux in enforcing mode, we need to adjust SElinux policy with the following steps:

Ensure the system has checkpolicy package installed.

$ sudo dnf install checkpolicy

Next Create a custom policy file mongodb_cgroup_memory.te:

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

Now compile and load the custom policy module using the below commands:

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

MongoDB is now able to access the required files with SElinux enforcing.

Non-Default MongoDB Directory Path(s)

Update the SELinux policy to allow the mongod service to use the new directory: Run the below command.

$ semanage fcontext -a -t <type> </some/MongoDB/directory.*>

where specify one of the following types as appropriate:

  • mongod_var_lib_t for data directory
  • mongod_log_t for log file directory
  • mongod_var_run_t for pid file directory

Update the SELinux user policy for the new directory:

$ chcon -Rv -u system_u -t <type> </some/MongoDB/directory>

where specify one of the following types as appropriate:

  • mongod_var_lib_t for data directory
  • mongod_log_t for log directory
  • mongod_var_run_t for pid file directory

Apply the updated SELinux policies to the directory:

$ restorecon -R -v </some/MongoDB/directory>

For non-default MongoDB ports, run the command:

$ semanage port -a -t mongod_port_t -p tcp <portnumber>

Step 6: Managing MongoDB on Oracle Linux 8

The following steps show how we can manage MongoDB 4.4 on Oracle Linux 8

Start, stop, restart and Enable MongoDB

Run the following commands to start, enable, stop and restart MongoDB

$ sudo systemctl start mongod
$ sudo systemctl enable mongod
$ sudo systemctl stop mongod
$ sudo systemctl restart mongod

If mongod fails to start, reload daemon and run the start command again

$ sudo systemctl daemon-reload
$ sudo systemctl start mongod

Check MongoDB status

To check if MongoDB is running, we use the command:

$ sudo systemctl status mongod

You should get an output as below if running

Begin using MongoDB

Start a mongo shell on the same host machine as the mongod

mongo
>

To find out the current database you are in, simply run the command db

db
test

To list databases, run the command:

show databases
admin 0.000G
config 0.000G
local 0.000G

To use a partcular database, use the command use

 use <db-name>

To create a new database, run the use command. In this case, since the database does not exist, it will be created.

use <new-db-name>

That’s it for now. You have successfully installed MongoDB 4.4 on Oracle Linux 8. Enjoy your usage and check below more interesting Linux guides.

Learning materials:

Books:

MongoDB: The Definitive Guide: Powerful and Scalable Data Storage

$59.99
$36.17  in stock
18 new from $34.54
11 used from $26.99
Free shipping
Buy Now
Amazon.com
as of September 23, 2020 9:19 am

Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems

$59.99
$34.77  in stock
35 new from $30.74
19 used from $26.77
Free shipping
Buy Now
Amazon.com
as of September 23, 2020 9:19 am

NoSQL with MongoDB in 24 Hours, Sams Teach Yourself

$34.99
$27.20  in stock
15 new from $22.17
17 used from $17.89
Free shipping
Buy Now
Amazon.com
as of September 23, 2020 9:19 am

Videos:

$15.14
$151.37
in stock
Udemy.com
$15.14
$151.37
in stock
Udemy.com
$15.14
$151.37
in stock
Udemy.com

LEAVE A REPLY

Please enter your comment!
Please enter your name here

18 − 10 =