Table of Contents
To install MongoDB on CentOS, you can follow these steps. MongoDB is a popular NoSQL database management system.
1. Add MongoDB Repository:
MongoDB provides an official repository for CentOS. Create a repo file for MongoDB:
sudo nano /etc/yum.repos.d/mongodb-org-4.4.repoAdd 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.ascSave and close the file.
2. Install MongoDB:
Now, install the MongoDB packages:
sudo yum install -y mongodb-org3. Start MongoDB:
Start the MongoDB service:
sudo systemctl start mongod4. Enable MongoDB to Start on Boot:
Enable MongoDB to start on boot:
sudo systemctl enable mongod5. Check MongoDB Status:
Verify that MongoDB is running without errors:
sudo systemctl status mongod6. Access MongoDB Shell:
Access the MongoDB shell to interact with the database:
mongo7. Secure MongoDB (Optional):
It’s recommended to secure your MongoDB installation. Create an administrative user and enable authentication.
Access the MongoDB shell:
mongoSwitch to the admin database:
use adminCreate an administrative user. Replace <username> and <password> with your desired username and password:
db.createUser({user: "<username>", pwd: "<password>", roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"]})Exit the MongoDB shell:
exitEdit the MongoDB configuration file:
sudo nano /etc/mongod.confAdd the following lines to enable authentication:
security: authorization: enabledSave and close the file.
Restart MongoDB:
sudo systemctl restart mongodNow, you’ll need to provide the created username and password when connecting to MongoDB.
That’s it! You have successfully installed MongoDB on CentOS. Adjust the configuration based on your specific requirements and security considerations.