Atlantic.Net Blog

How to Install and Secure MongoDB on Rocky Linux 8

MongoDB is a high-performance, document-oriented NoSQL database used to handle high traffic and large volumes of data. It is written in the C ++ programming language and is available for the Windows, Mac OS X, and Linux operating systems. Instead of using NoSQL, MongoDB stores the data in JSON format. This means you can store your records without worrying about the data structure such as the number of fields or types of fields to store values. It is a highly scalable, flexible, and distributed NoSQL database.

In this post, we will show you how to install MongoDB on Rocky Linux 8.

Prerequisites

  • A server running Rocky 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 Rocky Linux 8 as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your server, run the following command to update your base system with the latest available packages.

dnf update -y

Step 2 – Install MongoDB

By default, the MongoDB package is not included in the Rocky Linux 8 default repo, so you will need to create a MongoDB repo in your system. You can create it using the following command:

nano /etc/yum.repos.d/mongodb.repo

Add the following lines:

[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, then install the MongoDB package using the following command:

dnf install mongodb-org

After the installation, start and enable the MongoDB service with the following command:

systemctl start mongod
systemctl enable mongod

You can check the status of MongoDB using the following command:

systemctl status mongod

Sample output:

● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-10-12 15:27:51 UTC; 10s ago
     Docs: https://docs.mongodb.org/manual
  Process: 18587 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 18585 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 18583 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 18581 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 18589 (mongod)
   Memory: 64.4M
   CGroup: /system.slice/mongod.service
           └─18589 /usr/bin/mongod -f /etc/mongod.conf

Now, verify the MongoDB version using the following command:

mongod --version

Sample output:

db version v4.4.9
Build Info: {
    "version": "4.4.9",
    "gitVersion": "b4048e19814bfebac717cf5a880076aa69aba481",
    "openSSLVersion": "OpenSSL 1.1.1g FIPS  21 Apr 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel80",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Step 3 – How to Use MongoDB

You can connect to the MongoDB shell using the following command:

mongo

After connected, run the following command to list all databases:

> db

Sample output:

test

Next, create a new database named admin using the following command:

> use admin

To create a collection and insert some data, run the following command:

> db.linux.insertOne(
  { "RockyLinux" : "8",
   "centos" : "8",
   "debian" : "11"
  }
)

To verify the collection, run:

> show collections

Sample output:

linux

To display your data, run:

> db.linux.find()

Sample output:

{ "_id" : ObjectId("6165aa323a2df0ac96aa216a"), "RockyLinux" : "8", "centos" : "8", "debian" : "11" }

Step 4 – Enable MongoDB Authentication

By default, MongoDB is connected without login, so it is a good idea to enable authentication in MongoDB.

First, log in to MongoDB with the following command:

mongo

Next, create a MongoDB admin user and set a password:

> use admin
> db.createUser(
  {
    user: "mongoadmin",
    pwd: "password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

Sample output:

Successfully added user: {
	"user" : "mongoadmin ",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "admin"
		}
	]
}

To verify your users, run:

> db.getUsers()

Sample output:

[
	{
		"_id" : "admin.mongoadmin ",
		"userId" : UUID("ba2efd4e-84eb-4000-9d27-c0c337b4d7d8"),
		"user" : "mongoadmin ",
		"db" : "admin",
		"roles" : [
			{
				"role" : "readWrite",
				"db" : "admin"
			}
		],
		"mechanisms" : [
			"SCRAM-SHA-1",
			"SCRAM-SHA-256"
		]
	}
]

Next, edit the MongoDB configuration file and enable authentication:

nano /etc/mongod.conf

Add the following line:

security:
   authorization: enabled

Save and close the file, then restart the MongoDB service to apply the changes:

systemctl restart mongod

You can now connect to the MongoDB by specifying a user and password as shown below:

mongo -u mongoadmin -p

Step 5 – Uninstall MongoDB

If you want to remove the MongoDB from your system, first stop the MongoDB service:

systemctl stop mongod

Next, remove the MongoDB package by running the following command:

dnf remove mongodb-org

Next, remove the MongoDB logs and data directories by running the following command:

rm -rf /var/lib/mongodb

Conclusion

In the above guide, we explained how to install MongoDB on RockyLinux 8. We also explained how to use MongoDB and enable authentication in MongoDB. Give it a try on your dedicated server from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a 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