Step 1: Add the MongoDB Repository

First, create a .repo file for MongoDB. This will make sure you can install the latest stable version of MongoDB.

Create the repo file:

sudo nano /etc/yum.repos.d/mongodb-org-6.0.repo

Add the next content to the file:

[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc

3. Save and exit: Press Ctrl + O to write out, then press Enter. Press Ctrl + X to exit the editor.

Step 2: MongoDB 6.0 install

Once the repository is added, install MongoDB using yum.

  1. Update the YUM package index:
sudo yum update -y

2. Install MongoDB:

sudo yum install -y mongodb-org

Step 3: Start MongoDB Service

After the installation, you can start the MongoDB service.

  1. Start MongoDB:
sudo systemctl start mongod

Allow MongoDB to start on boot:

sudo systemctl enable mongod

Check the status:

sudo systemctl status mongod

If everything is working fine, the status should show that MongoDB is running.

Step 4: Set up MongoDB (Optional)

If you need to make any changes to the MongoDB configuration, you can do so. Edit the MongoDB configuration file.

Open the configuration file

sudo nano /etc/mongod.conf

Make necessary changes (e.g., bind IP, port).

Restart MongoDB to apply changes:

sudo systemctl restart mongod

Step 5: Access MongoDB in CentOS 7

Now, you can access MongoDB using the MongoDB shell.

  1. Start the MongoDB shell
mongo

This will connect you to the MongoDB instance running on your localhost.

Step 6: Allow Remote Access (Optional)

If you want to allow remote access to your MongoDB server:

  1. Edit the MongoDB configuration file:
sudo nano /etc/mongod.conf

2. Find the bindIp directive and change it from 127.0.0.1 to 0.0.0.0 to allow connections from any IP address

bindIp:  0.0.0.0

Restart MongoDB to apply changes:bashCopy codesudo systemctl restart mongod

Step 7: Set up Firewall (Optional)

If you have a firewall enabled, you need to allow the MongoDB default port (27017) through the firewall.

  1. Allow MongoDB port:
sudo firewall-cmd --zone=public --add-port=27017/tcp --permanent sudo firewall-cmd --reload

Step 8: secure MongoDB (Optional)

For production environments, it’s recommended to enable authentication in MongoDB.

  1. Create an administrative user
use admin db.createUser({ user: "admin", pwd: "your-password", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })

Enable authentication by editing the MongoDB configuration file:

sudo nano /etc/mongod.conf

Uncomment or add the security section:

security: authorization: enabled

Restart MongoDB:

sudo systemctl restart mongod

Log in with your user credentials:

mongo -u admin -p --authenticationDatabase admin

That’s it! MongoDB should now be installed and running on your CentOS 7 system.

Leave a Reply

Discover more from Geeky Codes

Subscribe now to keep reading and get access to the full archive.

Continue reading