Step-by-Step Guide to Installing MongoDB on Red Hat or CentOS
Install MongoDB Community Edition on Red Hat or CentOS
MongoDB Community Edition is a powerful NoSQL database ideal for high-performance and scalable applications. Installing MongoDB on Red Hat or CentOS involves adding the official MongoDB repository, installing the required packages, and configuring the database.
This guide provides a step-by-step walkthrough to install MongoDB Community Edition on Red Hat or CentOS systems.
Prerequisites
Before you begin, ensure the following:
1. Operating System: Red Hat Enterprise Linux (RHEL) or CentOS 7/8.
2. Root Access: Administrator privileges are required to execute system commands.
3. Internet Connection: To download packages from the MongoDB repository.
Step 1: Add the MongoDB Repository
1. Import MongoDB's GPG Key:
# Import the public GPG key sudo rpm --import https://www.mongodb.org/static/pgp/server-6.0.asc
2. Create a MongoDB Repository File:
For CentOS/RHEL 7, create a repository configuration file:
# Create the repo file for MongoDB sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo <<EOF [mongodb-org-6.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/6.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc EOF
For CentOS/RHEL 8:
# Create the repo file for MongoDB sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo <<EOF [mongodb-org-6.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/6.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc EOF
Step 2: Install MongoDB
Install the MongoDB server and related packages:
# Install MongoDB Community Edition sudo yum install -y mongodb-org
Step 3: Start and Enable MongoDB
Start the MongoDB service and configure it to start on boot:
# Start the MongoDB service sudo systemctl start mongod # Enable MongoDB to start on boot sudo systemctl enable mongod
Step 4: Verify Installation
Check the status of the MongoDB service:
# Check MongoDB service status sudo systemctl status mongod
Access the MongoDB shell to verify connectivity:
# Open the MongoDB shell mongosh
You should see the MongoDB shell prompt if the installation is successful.
Step 5: Configure Firewall (Optional)
Allow MongoDB traffic through the firewall:
# Allow MongoDB default port 27017 sudo firewall-cmd --add-port=27017/tcp --permanent sudo firewall-cmd --reload
Step 6: Security and Configuration
Enable Authentication
To enable authentication, modify the configuration file /etc/mongod.conf:
1. Open the configuration file:
sudo nano /etc/mongod.conf
2. Add or modify the security section:
security: authorization: "enabled"
3. Restart MongoDB to apply changes:
sudo systemctl restart mongod
Example Use Case
Insert a Document
Code:
// Insert a sample document into the 'test' collection
db.test.insertOne({ name: "MongoDB", type: "Database", version: 6.0 });
Output:
{ acknowledged: true, insertedId: ObjectId("64f9b0c5e7b8f4d5c8a9d3a2") }
Troubleshooting
Common Issues and Fixes
Issue | Solution |
---|---|
mongod fails to start | Check the log file /var/log/mongodb/mongod.log for error messages. |
Firewall blocks connection | Ensure port 27017 is open for incoming traffic. |
Authentication issues | Verify authorization is enabled and the correct username/password is used. |
Additional Notes:
1. MongoDB Version Compatibility: Ensure the MongoDB version you are installing is compatible with your system.
2. SELinux Configuration: If SELinux is enabled, adjust the security policies to allow MongoDB to function correctly.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/mongodb/installation-mongodb-on-redhat-centos.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics