Step-by-Step Guide
- Update the Package Index
Open a terminal and update the package index to ensure you have the latest information about available packages:
sudo yum update -y- Add the MongoDB Repository
Create a repository file for MongoDB:
sudo nano /etc/yum.repos.d/mongodb-org-4.4.repoAdd the following content to the file:
[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- Install MongoDB
Install MongoDB using theyumpackage manager:
sudo yum install -y mongodb-org- Start and Enable MongoDB
Start the MongoDB service:
sudo systemctl start mongodEnable MongoDB to start on boot:
sudo systemctl enable mongod- Verify MongoDB Installation
Check the MongoDB service status to ensure it is running:
sudo systemctl status mongodIf MongoDB is running, you should see an active (running) status.
- Log In to MongoDB
Use the MongoDB shell (mongo) to connect to the MongoDB server:
mongo- Set Up a New MongoDB User
To create a new MongoDB user, use the following commands within the MongoDB shell:
use admin
db.createUser({
user: "admin",
pwd: "your_password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})- Enable Authentication (Optional)
To enable authentication, edit the MongoDB configuration file:
sudo nano /etc/mongod.confAdd the following lines under the security section:
security:
authorization: enabledSave and close the file. Restart MongoDB to apply the changes:
sudo systemctl restart mongod- Test MongoDB Installation
To test the MongoDB installation, use the MongoDB shell to connect to the MongoDB server:
mongo -u admin -p --authenticationDatabase adminEnter the password for the admin user when prompted.
- Create a New Database (Optional)
To create a new database and a user for that database, use the following commands within the MongoDB shell:use your_database_name db.createUser({ user: "your_username", pwd: "your_password", roles: [ { role: "readWrite", db: "your_database_name" } ] }) - Backup and Restore Databases (Optional)
To back up a MongoDB database, use themongodumpcommand:bash mongodump --db your_database_name --out /path/to/backup
To restore a database from a backup, use themongorestorecommand:bash mongorestore --db your_database_name /path/to/backup/your_database_name


