MongoDB is a powerful open-source document-oriented database that allows efficient storage and retrieval of large-scale data. As a NoSQL database, MongoDB does not rely on traditional table structures for data storage. In this guide, we will walk you through the step-by-step process of installing MongoDB 4 on Rocky Linux 8, enabling you to leverage its robust features for your data-driven applications.
Adding the MongoDB Repository
MongoDB is not included in the default Rocky Linux 8 repository, so we need to add it manually. Follow these steps to add the MongoDB repository:
- Open the terminal and create a new file for the repository configuration:
vi /etc/yum.repos.d/mongodb.repo
- Paste the following configuration into the 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
Note: At the time of writing this article, version 4.4 was the latest. Make sure to check the MongoDB website for the latest version during the installation process.
Installing MongoDB
Now that we have added the MongoDB repository, we can proceed with the installation. Execute the following command in the terminal:
yum install mongodb-org
This command will download and install MongoDB and its dependencies on your Rocky Linux 8 system.
Starting and Enabling MongoDB
After the installation is complete, start MongoDB and enable it to automatically start on boot. Run the following commands:
systemctl start mongod systemctl enable mongod
To verify the status of MongoDB, use the following command:
systemctl status mongod
The output should indicate that MongoDB is active and running:
● mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor pres> Active: active (running) since Tue 2021-05-18 09:53:46 EDT; 34s ago Docs: https://docs.mongodb.org/manual Main PID: 5638 (mongod) Memory: 128.5M CGroup: /system.slice/mongod.service └─5638 /usr/bin/mongod -f /etc/mongod.conf
Accessing MongoDB Shell
To interact with MongoDB, you can access its shell. Use the following command in the terminal:
mongo
This will open the MongoDB shell, and you should see the following output:
MongoDB shell version v4.4.6 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("714ed2f1-1f91-46f8-8fb8-626fa3a60acf") } MongoDB server version: 4.4.6 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
Creating MongoDB and Admin User
To ensure secure access to MongoDB, it is recommended to create a dedicated user for administrative tasks. Follow these steps to create a new MongoDB user:
- Switch to the admin database by running the following command in the MongoDB shell:
use admin
- Now, create a new user with the desired username and password:
db.createUser( { user: "mongod_admin", pwd: "YOUR-PASSWORD", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
Note: Replace “YOUR-PASSWORD” with a strong password of your choice.
The output should confirm the successful creation of the user:
Successfully added user: { "user" : "mongod_admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
To view the list of MongoDB users created, run the following command in the MongoDB shell:
show users
The output will display the details of the created user(s):
> show users
{ "_id" : "admin.mongod_admin", "userId" : UUID("ff01436e-1cc8-4f37-8929-3009537c3c47"), "user" : "mongod_admin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] }
Configuring Authentication for MongoDB
To enhance the security of your MongoDB installation, it is essential to enable authentication. Follow these steps to configure authentication for MongoDB:
- Open the MongoDB service file using the following command:
vi /lib/systemd/system/mongod.service
- Locate the
[Service]
section, and within it, find theEnvironment
parameter. Modify it to include the--auth
flag and specify the configuration file path:
Environment="OPTIONS= --auth -f /etc/mongod.conf"
After modifying the file, it should resemble the following:
[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 changes and exit the editor.
- To apply the changes, reload the system and restart the MongoDB service:
systemctl daemon-reload
systemctl restart mongod
Authenticating MongoDB Users
With authentication enabled, you need to authenticate MongoDB users to access the database. Follow these steps to authenticate as the admin user:
- Open the MongoDB shell:
mongo
- Authenticate using the admin user credentials:
db.auth('mongod_admin', 'YOUR-PASSWORD')
Note: Replace “YOUR-PASSWORD” with the actual password you set for the admin user.
If the authentication is successful, you will see “1” as the output.
- To verify the authentication, you can list the users by running the following command in the MongoDB shell:
show users
The output should display the details of the authenticated user(s).
Conclusion
Congratulations! You have successfully installed MongoDB 4 on Rocky Linux 8 and configured authentication for secure access. You can now leverage the power of MongoDB to efficiently store and manage your data for your applications.
Remember to regularly update MongoDB to benefit from the latest features and security enhancements. Stay tuned for more informative articles and tutorials on MongoDB and other cutting-edge technologies.
If you’re looking for reliable and scalable hosting solutions, consider Shape.host’s Linux SSD VPS services. Their expert team ensures optimal performance and security for your applications. Visit Shape.host today to explore their hosting options and take your business to new heights.