MongoDB is a popular NoSQL database that provides a flexible and scalable solution for storing and managing data. In this comprehensive guide, we will walk you through the step-by-step process of installing MongoDB 5 on AlmaLinux 8. By following this guide, you will be able to set up MongoDB quickly and efficiently on your AlmaLinux server.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- An AlmaLinux 8 server with root access.
- Basic knowledge of the Linux command line.
Now let’s dive into the installation process.
Step 1: Adding the MongoDB Repository
To install MongoDB on AlmaLinux, we need to add the MongoDB repository as it is not present in the default repository. Follow these steps to add the repository:
- Open a terminal on your AlmaLinux server.
- Create a repository file using the following command:
# vi /etc/yum.repos.d/mongodb.repo
- Add the following lines to the file and save it:
[mongodb-org-5.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/5.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc
Step 2: Installing MongoDB
Now that we have added the MongoDB repository, let’s proceed with the installation process. Run the following command to install MongoDB:
# dnf install -y mongodb-org
This command will install the MongoDB package and its dependencies on your AlmaLinux server.
Step 3: Starting and Enabling MongoDB
After the installation is complete, start and enable the MongoDB service using the following commands:
# systemctl start mongod # systemctl enable mongod
These commands will start the MongoDB service and configure it to start automatically on system boot.
To verify the status of the MongoDB service, run the following command:
# systemctl status mongod
If the service is active and running, you should see an output similar to the following:
● mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2021-10-25 13:36:18 EDT; 3s ago Docs: https://docs.mongodb.org/manual Main PID: 57058 (mongod) Memory: 156.6M CGroup: /system.slice/mongod.service └─57058 /usr/bin/mongod -f /etc/mongod.conf
Step 4: Accessing the MongoDB Shell
To interact with the MongoDB database, we can use the MongoDB shell. Follow these steps to access the MongoDB shell:
- Open a new terminal window.
- Run the following command:
# mongo
You should now see the MongoDB shell prompt, indicating that you have successfully connected to the MongoDB server:
MongoDB shell version v5.0.3 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("283a2949-15c6-413e-a0f2-6aa95c1bae3a") } MongoDB server version: 5.0.3
Please note that the “mongo” shell has been deprecated and will be removed in an upcoming release. We recommend using the new “mongosh” shell for improved usability and compatibility.
Step 5: Creating a MongoDB Admin User
To secure your MongoDB installation, it is recommended to create an admin user. Follow these steps to create a MongoDB admin user:
- Switch to the
admin
database by running the following command in the MongoDB shell:
> use admin
- Create an admin user by running the following code, replacing
Enter-A-user-Here
with a username andEnter-A-Password-Here
with a strong and secure password:
> db.createUser( { user: "Enter-A-user-Here", pwd: "Enter-A-Password-Here", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
This command will create a new admin user with the specified username and password.
To verify that the user has been created successfully, run the following command:
> show users
You should see the newly created user listed in the output.
Step 6: Enabling Authentication for MongoDB
To enhance the security of your MongoDB installation, it is recommended to enable authentication. Follow these steps to enable authentication:
- Open the MongoDB configuration file using a text editor:
# vi /etc/mongod.conf
- Find the line starting with
security:
and add the following line below it:
authorization: enabled
This will enable authentication for MongoDB.
Save the changes and exit the editor.
- Restart the MongoDB service to apply the configuration changes:
# systemctl restart mongod
Step 7: Authenticating with MongoDB
Now that authentication is enabled, you need to authenticate before executing any commands in the MongoDB shell. Follow these steps to authenticate:
- In the MongoDB shell, run the following command, replacing
YOUR-PASSWORD
with the actual password you set for the admin user:
> db.auth('Enter-A-user-Here', 'YOUR-PASSWORD')
This command will authenticate the user with the specified credentials.
- Once authenticated, you can run any command in the MongoDB shell. For example, you can list the users by running the following command:
> show users
Conclusion
Congratulations! You have successfully installed MongoDB 5 on AlmaLinux 8 and secured it with authentication. You can now start leveraging the power of MongoDB for your data storage and management needs.
Remember to regularly update and maintain your MongoDB installation to ensure optimal performance and security. If you encounter any issues or have any further questions, the MongoDB documentation is a valuable resource.
Shape.host offers reliable and scalable Cloud VPS services to meet your hosting needs. With our secure and efficient solutions, you can focus on your business while we take care of your infrastructure. Contact us today to learn more about how Shape.host can help you succeed.