Deploying MongoDB in Oracle Cloud (OCI) Linux VM

If you’ve ever wanted a guide for running MongoDB Enterprise/Community in your Oracle Cloud Infrastructure (OCI) environment, we have good news for you!

Prerequisites

  • You have deployed a VM 2.1 with Oracle Linux 7.9 (OEL7) in Oracle Cloud Infrastructure.
  • Your install of Oracle Linux 7.9 is using pip3.6 by default.
  • Python 3.6 or higher is installed.
  • You have access to root either directly or via sudo. By default in OCI, you are connected like an “opc” user with sudo privilege.

If you don’t yet have an OCI account, you can quickly sign up for one today by registering for an Oracle Cloud Free Tier account.

Afterwards, check developer.oracle.com/linux for even more Linux content.

MongoDB Installation

Create an /etc/yum.repos.d/mongodb-enterprise-5.0. repo file in the yum configuration so that you can install MongoDB Enterprise directly with this command:

vi /etc/yum.repos.d/mongodb-enterprise-5.0.repo

Paste these lines in that file:

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

Execute the next command using the yum file:

sudo yum install -y mongodb-enterprise

Your result should look like this:

Installed:
  mongodb-enterprise.x86_64 0:5.0.2-1.el7

Dependency Installed:
  cyrus-sasl.x86_64 0:2.1.26-23.el7                                    cyrus-sasl-gssapi.x86_64 0:2.1.26-23.el7
  mongodb-database-tools.x86_64 0:100.5.0-1                            mongodb-enterprise-cryptd.x86_64 0:5.0.2-1.el7
  mongodb-enterprise-database.x86_64 0:5.0.2-1.el7                     mongodb-enterprise-database-tools-extra.x86_64 0:5.0.2-1.el7
  mongodb-enterprise-mongos.x86_64 0:5.0.2-1.el7                       mongodb-enterprise-server.x86_64 0:5.0.2-1.el7
  mongodb-enterprise-shell.x86_64 0:5.0.2-1.el7                        mongodb-enterprise-tools.x86_64 0:5.0.2-1.el7
  mongodb-mongosh.x86_64 0:1.0.5-1.el7                                 net-snmp.x86_64 1:5.7.2-49.el7_9.1
  net-snmp-agent-libs.x86_64 1:5.7.2-49.el7_9.1                        net-snmp-libs.x86_64 1:5.7.2-49.el7_9.1

Complete!

The install is pretty simple using the yum installation script. Let’ s start with setting up the MongoDB Environment

MongoDB Setup

By default, MongoDB has a daemon configuration file in /etc/mongod.conf and runs using the mongod user account with the following default directories:

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

The package manager creates the default directories during installation. The owner and group name are mongod.

Using Non-Default Directories with MongoDB

To use a data directory and/or log directory other than the default directories:

Create the new directory or directories and then edit the configuration file /etc/mongod.conf and modify the following fields accordingly:

storage.dbPath to specify a new data directory path (e.g. /some/data/directory)
systemLog.path to specify a new log file path (e.g. /some/log/directory/mongod.log)

Ensure that the user running MongoDB has access to the directory or directories:

sudo chown -R mongod:mongod <directory>

For example, create a “data” directory:

sudo mkdir /data/mongodb 
sudo chown -R mongod:mongod /data/mongodb

Change the configuration in /etc/mongod.conf and the dbPath variable.

vi /etc/mongod.conf

storage:
dbPath: /var/lib/mongo
dbPath: /data/mongodb
journal:
enabled: true
engine:
wiredTiger:

If you change the user that runs the MongoDB process, you must give the new user access to these directories.

MongoDB SELinux Configuration (Optional)

Configure SELinux if enforced. See SELinux.

Start MongoDB

You can start the mongod process by issuing the following command:

sudo systemctl start mongod

Verify that MongoDB has started successfully

You can verify that the mongod process has started successfully by issuing the following command:

sudo systemctl status mongod

You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:

sudo systemctl enable mongod

Stop MongoDB

As needed, you can stop the mongod process by issuing the following command:

sudo systemctl stop mongod

Restart MongoDB.

You can restart the mongod process by issuing the following command:

sudo systemctl restart mongod

You can follow the state of the process for errors or important messages by watching the output in the /var/log/mongodb/mongod.log file.

Begin using MongoDB

Start a mongosh session on the same host machine as mongod. You can run mongosh without any command-line options to connect to a mongod that is running on your localhost with default port 27017.

mongosh

The result should look like:

Current Mongosh Log ID: 61a0e720ab709eb9973092bc
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:          5.0.2
Using Mongosh:          1.0.5

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

------
   The server generated these startup warnings when booting:
   2021-11-26T13:54:31.358+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2021-11-26T13:54:31.358+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
------

Enterprise test>

You’re now connected to Mongodb on Oracle Cloud Infrastructure!