Saturday, February 17, 2024

A Step-by-Step Guide to Installing MongoDB on Oracle Linux 8 | How to install MongoDB on RedHat 8/ Rocky Linux 8

Introduction:
MongoDB is a powerful, open-source NoSQL database that provides flexibility and scalability for modern applications. In this guide, we'll walk through the process of installing MongoDB on Oracle Linux 8 and configuring it to ensure security best practices are followed.


Step 1: Prerequisites

Before we begin, make sure you have:

1. An instance of Oracle Linux 8 set up and running.

2. Root or sudo access to the server.


Step 2 – Install  MongoDB 

1. Open a terminal window on your Oracle Linux 8 server and update it.

#yum update

  After patch update make sure you reboot the server 


2. Add the MongoDB repository to your system:

#vim /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.


3. Install MongoDB:

# sudo dnf install -y mongodb-org

4. Start the MongoDB service and enable it to start on boot:

# sudo systemctl start mongod

# sudo systemctl enable mongod

mongod --version

Step 3: Configure MongoDB

1. Open the MongoDB configuration file:

# sudo vim /etc/mongod.conf

2. Bind MongoDB to localhost or IP:

bindIp: 127.0.0.1

3. Enable authentication:

security:

  authorization: enabled


4. Save and close the file.


Step 4: Secure MongoDB

1. Create an administrative user:

First, connect to the MongoDB instance with the 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: "admin",
    pwd: passwordPrompt(),
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]

  }

)


You will be asked to set a password.


2. Exit the MongoDB shell:

exit

3. Restart MongoDB to apply the changes:

sudo systemctl restart mongod

Step 5: Test MongoDB Authentication

1. Open the MongoDB shell as the administrative user:

# mongo -u admin -p --authenticationDatabase admin

or 

#mongo --port 27017 --authenticationDatabase admin -u admin -p



2. You'll be prompted to enter the password for the admin user.

3. Once logged in, you can create additional users and databases as needed.


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 testdb, run the following command:

use testdb

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

db.person.insertOne(
  { "Nirmal Singh" : "20",
   "Tom " : "18",
   "Harry" : "25"
  }
)

You can now list available databases using the following command db prompt:

db

You will get the following output:

testdb

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 output for all records:

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 formate:

[
	{
		"_id" : "admin.admin",
		"userId" : UUID("504b73he-aaed-4ad9-bb60-4fb8df334709"),
		"user" : "admin",
		"db" : "admin",
		"roles" : [
			{
				"role" : "userAdminAnyDatabase",
				"db" : "admin"
			},
			{
				"role" : "readWriteAnyDatabase",
				"db" : "admin"
			}
		],
		"mechanisms" : [
			"SCRAM-SHA-1",
			"SCRAM-SHA-256"
		]
	}
]


Step 7: Firewall Configuration (Optional)

If you have a firewall enabled on your server, you may need to open the MongoDB port (default is 27017) to allow external connections. Use the following command to open the port:


sudo firewall-cmd --zone=public --add-port=27017/tcp --permanent

# sudo firewall-cmd --reload


Conclusion:

Congratulations! You've successfully installed MongoDB on Oracle Linux 8 and configured it to ensure security. By following these steps, you can confidently deploy MongoDB for your applications while adhering to best practices for database security. Remember to regularly update MongoDB and review your security measures to protect your data effectively.

No comments:

Post a Comment

Mastering Cron Jobs in Linux: A Comprehensive Guide with Examples

Introduction: Cron jobs are an indispensable tool in the world of system administration and automation. They allow users to schedule tasks t...