Installation guides

Installing MongoDB on RHEL

Step-by-Step Guide

  1. 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
  1. 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
  1. Install MongoDB
    Install MongoDB using the yum package manager:
   sudo yum install -y mongodb-org
  1. Start and Enable MongoDB
    Start the MongoDB service:
   sudo systemctl start mongod

Enable MongoDB to start on boot:

   sudo systemctl enable mongod
  1. 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.

  1. Log In to MongoDB
    Use the MongoDB shell (mongo) to connect to the MongoDB server:
   mongo
  1. 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" } ]
   })
  1. 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
  1. 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.

  1. 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" } ] })
  2. Backup and Restore Databases (Optional)
    To back up a MongoDB database, use the mongodump command:
    bash mongodump --db your_database_name --out /path/to/backup
    To restore a database from a backup, use the mongorestore command:
    bash mongorestore --db your_database_name /path/to/backup/your_database_name
Other Recent Posts