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.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 close the file.
2. Install MongoDB:
Now, install the MongoDB packages:
sudo yum install -y mongodb-org
3. Start MongoDB:
Start the MongoDB service:
sudo systemctl start mongod
4. Enable MongoDB to Start on Boot:
Enable MongoDB to start on boot:
sudo systemctl enable mongod
5. Check MongoDB Status:
Verify that MongoDB is running without errors:
sudo systemctl status mongod
6. Access MongoDB Shell:
Access the MongoDB shell to interact with the database:
mongo
7. Secure MongoDB (Optional):
It’s recommended to secure your MongoDB installation. Create an administrative user and enable authentication.
Access the MongoDB shell:
mongo
Switch to the admin
database:
use admin
Create 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:
exit
Edit the MongoDB configuration file:
sudo nano /etc/mongod.conf
Add the following lines to enable authentication:
security: authorization: enabled
Save and close the file.
Restart MongoDB:
sudo systemctl restart mongod
Now, 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.