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.repo
Add 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 theyum
package manager:
sudo yum install -y mongodb-org
- Start and Enable MongoDB
Start the MongoDB service:
sudo systemctl start mongod
Enable 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 mongod
If 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.conf
Add the following lines under the security
section:
security:
authorization: enabled
Save 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 admin
Enter 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 themongodump
command:bash mongodump --db your_database_name --out /path/to/backup
To restore a database from a backup, use themongorestore
command:bash mongorestore --db your_database_name /path/to/backup/your_database_name