Introduction
MongoDB, a NoSQL database, offers flexibility and scalability for handling large amounts of data. In this detailed blog post, we will walk through the step-by-step process of deploying MongoDB on CentOS, ensuring a smooth and successful installation.
Prerequisites:
Before we begin, make sure you have a CentOS server with root or sudo access. Also, ensure that your server meets MongoDB’s system requirements.
Step 1: Update System Packages
sudo yum update -y
Step 2: Configure the MongoDB Repository
Create a MongoDB repository file in the ‘yum’ repository directory.
sudo vi /etc/yum.repos.d/mongodb-org.repo
Add the following content:
[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 exit.
Step 3: Install MongoDB
Install MongoDB using the following command:
sudo yum install -y mongodb-org
Step 4: Start MongoDB
Start the MongoDB service and enable it to start on boot:
sudo systemctl start mongod
sudo systemctl enable mongod
Step 5: Adjust Firewall Settings
If you are using firewalld, allow MongoDB traffic:
sudo firewall-cmd --zone=public --add-port=27017/tcp --permanent
sudo firewall-cmd --reload
Step 6: Access MongoDB Shell
Access the MongoDB shell to interact with the database:
mongo
Step 7: Create Admin User (Optional but Recommended)
use admin
db.createUser({
user: "admin",
pwd: "your_secure_password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
Step 8: Secure MongoDB Installation
Edit the MongoDB configuration file to enable authentication:
sudo vi /etc/mongod.conf
Find the security section and uncomment and modify the following lines:
security:
authorization: enabled
Save and exit, then restart MongoDB:
sudo systemctl restart mongod
Step 9: Connect to MongoDB with Authentication
Connect to the MongoDB shell with authentication:
mongo -u admin -p your_secure_password --authenticationDatabase admin
Step 10: Conclusion
Congratulations! You have successfully deployed MongoDB on CentOS. Remember to follow best practices for securing your database and regularly updating MongoDB for the latest features and security patches.
This comprehensive guide should help you seamlessly set up MongoDB on your CentOS server. If you encounter any issues, refer to the official MongoDB documentation for additional troubleshooting. Happy coding!