MongoDB is a popular NoSQL database that offers flexibility and scalability for storing and managing data. In this guide, we will walk you through the step-by-step process of installing MongoDB 4 on AlmaLinux 8. By following these instructions, you will be able to set up a robust and reliable database system for your applications.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- AlmaLinux 8 installed on your server.
- SSH access to your server with administrative privileges.
Step 1: Adding the MongoDB Repository
To install MongoDB on AlmaLinux 8, we need to add the MongoDB repository manually since it is not present in the default repository. Follow the steps below to add the repository:
- Log in to your server via SSH as the root user.
- Create a repository file using the following command:
# vi /etc/yum.repos.d/mongodb.repo
- Add the following lines to the repository file and save it:
[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
Step 2: Installing MongoDB
Once the repository is added, we can proceed with the installation of MongoDB. Follow the steps below to install MongoDB 4 on AlmaLinux 8:
- Run the following command to install MongoDB:
# dnf install -y mongodb-org
This command will install the MongoDB packages and all its dependencies.
- Start the MongoDB service:
# systemctl start mongod
- Enable MongoDB to start automatically on system boot:
# systemctl enable mongod
- Verify the status of the MongoDB service:
# systemctl status mongod
You should see the following output indicating that MongoDB is running:
● mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2021-02-16 23:10:54 EST; 46s ago Docs: https://docs.mongodb.org/manual Main PID: 2236 (mongod) Memory: 102.3M CGroup: /system.slice/mongod.service └─2236 /usr/bin/mongod -f /etc/mongod.conf
MongoDB is now successfully installed on your AlmaLinux 8 server.
Step 3: Accessing the MongoDB Shell
To interact with the MongoDB database, you can use the MongoDB shell. Follow the steps below to access the MongoDB shell:
- Enter the MongoDB shell by running the following command:
# mongo
This will launch the MongoDB shell with the default configuration.
- You should see the following output when you enter the MongoDB shell for the first time:
MongoDB shell version v4.4.4 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("9a6a4ff9-53ad-4e1e-b49b-ff089559b9e1") } MongoDB server version: 4.4.4 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see https://docs.mongodb.com/ Questions? Try the MongoDB Developer Community Forums https://community.mongodb.com --- The server generated these startup warnings when booting: 2021-02-16T23:10:54.569-05:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted 2021-02-16T23:10:54.569-05:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' ---
You are now connected to the MongoDB shell and can start executing commands.
Step 4: Creating a MongoDB Admin User
To secure your MongoDB installation, it is recommended to create an admin user. Follow the steps below to create a MongoDB admin user:
- Switch to the database named “admin” by running the following command in the MongoDB shell:
> use admin
- Create an admin user by running the following code:
> db.createUser( { user: "shapehost", pwd: "Enter-A-Password-Here", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
Make sure to replace “Enter-A-Password-Here” with a strong and secure password.
You should see the following output indicating that the user has been created successfully:
Successfully added user: { "user" : "shapehost", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
- To verify the user creation, run the following command to list all MongoDB users:
> show users
You should see the newly created user in the output:
{
"_id" : "admin.shapehost",
"userId" : UUID("5b78c7d3-b956-4330-8d77-7212a7cff7c1"),
"user" : "shapehost",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
Your MongoDB admin user is now created and ready to use.
Step 5: Enabling Authentication for MongoDB
By default, MongoDB allows access to the shell without authentication, which can pose security risks. To enable authentication, follow the steps below:
- Open the MongoDB service configuration file using a text editor:
# vi /lib/systemd/system/mongod.service
- Find the following line in the configuration file:
Environment="OPTIONS=-f /etc/mongod.conf"
Change it to the following:
Environment="OPTIONS= --auth -f /etc/mongod.conf"
Save and exit the configuration file.
- Restart the MongoDB service to apply the changes:
# systemctl restart mongod
- To test the authentication, try listing the users without authentication:
> show users
You will receive an error indicating that authentication is required.
- Authenticate with the admin user by running the following command:
> db.auth('shapehost', 'Enter-A-Password-Here')
Replace “Enter-A-Password-Here” with the password you set for the admin user.
If the authentication is successful, you can now run any MongoDB command without issues.
Conclusion
Congratulations! You have successfully installed MongoDB 4 on AlmaLinux 8 and created an admin user with authentication enabled. You can now start using MongoDB for your applications and enjoy the benefits of a flexible and scalable NoSQL database.
If you encounter any technical difficulties or need further assistance, feel free to reach out to the Shape.host support team via Support Ticket. Shape.host offers a range of reliable and efficient cloud hosting solutions, including Cloud VPS, to help businesses thrive in the digital landscape.