Atlantic.Net Blog

How to Install and Secure MongoDB on Oracle Linux 8

Open-source MongoDB is the most popular document-oriented database management system. It is cross-platform and uses JSON-like documents with optional schemas. MongoDB is written in C++ and is used for developing mission-critical and modern dynamic applications. The data objects are stored as separate documents in a collection in the MongoDB database versus in a traditional relational database system, where rows and columns are used. It also offers an easy querying and indexing functionality that allows developers to query complex document-based data sets easily.

In this post, we will explain how to install and use MongoDB on Oracle Linux 8.

Prerequisites

  • A server running Oracle Linux 8 on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Step 1 – Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Oracle Linux 8 as an operating system with at least 1GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Step 2 – Add MongoDB Repository

The MongoDB package is not available in the Oracle Linux 8 default repo, so you will need to create the MongoDB repo on your system.

You can create it by running the following command:

nano /etc/yum.repos.d/mongodb-org-4.4.repo

Add the following contents:

[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

Save and close the file once you are done.

Step 3 – Install MongoDB on Oracle Linux 8

After creating a MongoDB repo, you can install the MongoDB package by just running the following command:

dnf install mongodb-org -y

Once the MongoDB is installed, start and enable the MongoDB service with the following command:

systemctl start mongod
systemctl enable mongod

To verify the MongoDB version, run the following command:

mongod --version

You will get the following output:

db version v4.4.14
Build Info: {
    "version": "4.4.14",
    "gitVersion": "0b0843af97c3ec9d2c0995152d96d2aad725aab7",
    "openSSLVersion": "OpenSSL 1.1.1k  FIPS 25 Mar 2021",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel80",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Step 4 – Configure MongoDB

By default, MongoDB can be connected without any authentication. For security purposes, it is recommended to enable MongoDB authentication.

First, edit the MongoDB configuration file and enable authentication:

nano /etc/mongod.conf

Add the following lines:

security:
 authorization: enabled

Save and close the file when you are finished.

Step 5 – Create an Admin User for MongoDB

Next, you will need to create an admin user and set a password to manage the MongoDB databases.

First, connect to the MongoDB instance with the following command:

mongo

Once you are connected, create a database named admin using the following command:

use admin

Next, create an admin user and set a password with the following command:

db.createUser(
{
user: "mongoadmin",
pwd: passwordPrompt(),
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)

You will be asked to set a password as shown below:

Enter password: 
Successfully added user: {
	"user" : "mongoadmin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		},
		"readWriteAnyDatabase"
	]
}

Next, exit from the MongoDB shell with the following command:

exit

Finally, restart the MongoDB service to apply the changes:

systemctl restart mongod

You can now connect to the MongoDB instance using the administrator credentials:

mongo --port 27017 --authenticationDatabase "admin" -u "mongoadmin" -p

Once you are logged in, you should see the following output:

MongoDB shell version v4.4.14
Enter password: 
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1d5be9b1-d0bd-4b6a-9ca7-adecddf5f9dd") }
MongoDB server version: 4.4.14

Step 6 – Create a Database in MongoDB

In this section, we will show you how to interact with the MongoDB database.

To create a database named wpdb, run the following command:

use wpdb

Next, add some data to the wpdb database using the following command:

db.person.insertOne(
  { "Hitesh Jethva" : "41",
   "Vyom Jethva" : "8",
   "Raj Shah" : "18"
  }
)

You can now list available databases using the following command:

db

You will get the following output:

wpdb

To show documents in your database, run the following command:

show collections

You will get the following output:

person

To show the contents of your database collection, run the following command:

db.person.find()

You will get the following output:

{ "_id" : ObjectId("62a0d1047f7e6794c2dd6615"), "Hitesh Jethva" : "41", "Vyom Jethva" : "8", "Raj Shah" : "18" }

To switch the database to admin, use the following command:

use admin

To list all users, run the following command:

db.getUsers()

You will get a list of all users in the following output:

[
	{
		"_id" : "admin.mongoadmin",
		"userId" : UUID("544b73fe-aaed-4ad2-bb60-4fb8df334004"),
		"user" : "mongoadmin",
		"db" : "admin",
		"roles" : [
			{
				"role" : "userAdminAnyDatabase",
				"db" : "admin"
			},
			{
				"role" : "readWriteAnyDatabase",
				"db" : "admin"
			}
		],
		"mechanisms" : [
			"SCRAM-SHA-1",
			"SCRAM-SHA-256"
		]
	}
]

Conclusion

In this guide, we learned how to install and use the MongoDB database on Oracle Linux 8. We also learned how to enable MongoDB authentication and interact with a MongoDB database. You can now easily install and manage the MongoDB databases. Try it on dedicated hosting from Atlantic.Net!

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year