MongoDB is a powerful open-source document-oriented database that is designed to store and manage large-scale data efficiently. As a NoSQL (Not Only SQL) database, MongoDB offers a flexible structure for data storage and retrieval, making it a popular choice for modern applications. In this article, we will guide you through the process of installing MongoDB 5 on Rocky Linux 8, allowing you to harness the capabilities of this robust database.
Installing MongoDB Repository
Before we can install MongoDB on Rocky Linux 8, we need to add the MongoDB repository to our system. By adding the repository, we ensure that we have access to the latest version of MongoDB and can easily update it in the future. Follow the steps below to add the MongoDB repository:
- Open a terminal and create a new file for the MongoDB repository configuration:
vi /etc/yum.repos.d/mongodb.repo
- Paste the following configuration into the file:
[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
Note: At the time of writing this article, version 5.0 was the latest release of MongoDB. Make sure to check the MongoDB website for the latest version during the installation.
- Save the file and exit the text editor.
Installing MongoDB
Now that we have added the MongoDB repository, we can proceed with the installation of MongoDB 5 on Rocky Linux 8. Follow the steps below to install MongoDB:
- Open a terminal and run the following command to install MongoDB:
sudo yum install mongodb-org
- Wait for the installation process to complete. Once finished, MongoDB will be installed on your system.
Starting and Enabling MongoDB
After installing MongoDB, we need to start the MongoDB service and ensure that it starts automatically on system boot. Follow the steps below to start and enable MongoDB:
- Start the MongoDB service by running the following command:
sudo systemctl start mongod
- Enable MongoDB to start on system boot with the following command:
sudo systemctl enable mongod
- Verify the status of the MongoDB service by running the following command:
sudo systemctl status mongod
You should see an output similar to the following indicating that MongoDB is active and running:
● 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 11:42:59 EDT; 2 days ago Docs: https://docs.mongodb.org/manual Main PID: 56769 (mongod) Memory: 148.3M CGroup: /system.slice/mongod.service └─56769 /usr/bin/mongod -f /etc/mongod.conf
Accessing MongoDB Shell
With MongoDB installed and running, we can now access the MongoDB shell to interact with the database. The MongoDB shell provides a command-line interface for executing queries and managing the database. Follow the steps below to access the MongoDB shell:
- Open a terminal and run the following command to access the MongoDB shell:
mongo
You should see an output similar to the following indicating a successful connection 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("55af8151-15f9-470e-bbf6-2f5a49b6bb85") } MongoDB server version: 5.0.3
Note: The MongoDB team has introduced a new shell called “mongosh” that offers improved usability and compatibility. While the “mongo” shell is still available, it has been deprecated and will be removed in future releases. It is recommended to start using “mongosh” for better user experience. You can find installation instructions for “mongosh” in the MongoDB documentation.
- Once you have successfully accessed the MongoDB shell, you can begin executing commands and interacting with the database.
Creating MongoDB and Admin User
To secure your MongoDB installation, it is recommended to create a dedicated MongoDB user with administrative privileges. This user will be used to manage the database and perform administrative tasks. Follow the steps below to create a MongoDB user:
- Switch to the
admin
database by running the following command in the MongoDB shell:
use admin
- Create a new MongoDB user with administrative privileges by running the following command:
db.createUser({ user: "mongod_admin", pwd: "YOUR-PASSWORD", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })
Note: Replace “YOUR-PASSWORD” with a strong password for the MongoDB admin user.
- After executing the command, you should see an output similar to the following indicating that the user has been successfully created:
Successfully added user: { "user" : "mongod_admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
- To verify the user creation, you can run the following command to list all the MongoDB users:
show users
The output should display the newly created user and their assigned roles.
Configuring Authentication for MongoDB
To enhance the security of your MongoDB installation, it is essential to enable authentication. By enabling authentication, you ensure that only authorized users can access and manage the database. Follow the steps below to configure authentication for MongoDB:
- Open the MongoDB service configuration file in a text editor:
sudo vi /lib/systemd/system/mongod.service
- Locate the
[Service]
section in the file and find theEnvironment
parameter. Edit the parameter to include the--auth
option and specify the MongoDB configuration file path as shown below:
Environment="OPTIONS= --auth -f /etc/mongod.conf"
After making the changes, the file should look like this:
[Service]
User=mongod
Group=mongod
Environment="OPTIONS= --auth -f /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
- Save the file and exit the text editor.
- To apply the configuration changes, reload the systemd daemon by running the following command:
sudo systemctl daemon-reload
- Restart the MongoDB service to apply the new configuration:
sudo systemctl restart mongod
- If you try to access the MongoDB shell without authentication, you should receive an error message indicating that authentication is required.
- To authenticate and access the MongoDB shell, use the following command, replacing “YOUR-PASSWORD” with the actual password for the MongoDB admin user:
db.auth('mongod_admin','YOUR-PASSWORD')
After successful authentication, you can execute any MongoDB commands and interact with the database.
- To verify that authentication is working correctly, you can use the
show users
command to list the MongoDB users again.
Conclusion
Congratulations! You have successfully installed MongoDB 5 on Rocky Linux 8 and configured authentication for enhanced security. You can now leverage the power and flexibility of MongoDB to store and manage your data efficiently. The MongoDB shell provides a versatile command-line interface for executing queries and managing the database. Remember to follow best practices for securing your MongoDB deployment and regularly update to the latest version for bug fixes and new features.
If you are looking for reliable and scalable hosting solutions for your MongoDB databases, consider Shape.host’s Linux SSD VPS services. Shape.host offers high-performance virtual private servers optimized for running MongoDB and other database systems. With their professional support and robust infrastructure, Shape.host ensures a smooth and hassle-free hosting experience. Visit Shape.host to explore their hosting options and unleash the full potential of your MongoDB deployments.