w3resource

Step-by-Step Guide to Install MongoDB Community Edition on Amazon Linux


Install MongoDB Community Edition on Amazon Linux

MongoDB Community Edition is a powerful NoSQL database that is perfect for modern applications. Installing MongoDB on Amazon Linux requires setting up the MongoDB repository, installing the necessary packages, and configuring the database for proper operation.

This guide explains the steps to install MongoDB Community Edition on Amazon Linux.


Prerequisites

Before starting the installation, ensure the following:

    1. Operating System: Amazon Linux 2 or Amazon Linux 2023.

    2. User Privileges: Root or sudo access.

    3. Internet Access: Required for downloading MongoDB packages.


Step 1: Import MongoDB GPG Key

The GPG key ensures the integrity of downloaded packages. Import it using the following command:

# Import MongoDB GPG key
sudo rpm --import https://pgp.mongodb.com/server-6.0.asc

Step 2: Create the MongoDB Repository

Add MongoDB's official repository to your system:

# Create a MongoDB repository file
echo -e "[mongodb-org-6.0]\nname=MongoDB Repository\nbaseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/6.0/x86_64/\ngpgcheck=1\ngpgkey=https://pgp.mongodb.com/server-6.0.asc\nenabled=1" | sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo

Step 3: Install MongoDB Packages

Update the package index and install MongoDB:

# Update package list
sudo yum update -y

# Install MongoDB Community Edition
sudo yum install -y mongodb-org

Step 4: Start and Enable MongoDB Service

    1. Start the MongoDB service:

    # Start MongoDB service
    sudo systemctl start mongod
    

    2. Enable MongoDB to start automatically on boot:

    # Enable MongoDB on system boot
    sudo systemctl enable mongod
    

Step 5: Verify MongoDB Installation

Check the status of MongoDB to ensure it is running:

# Check the status of the MongoDB service
sudo systemctl status mongod

You can also connect to the MongoDB shell to confirm installation:

# Open the MongoDB shell
mongosh

Step 6: Optional Configuration

Enable Authentication

To secure your database, enable user authentication:

    1. Edit the MongoDB configuration file:

    sudo nano /etc/mongod.conf
    

    2. Under the security section, add:

    security:
      authorization: "enabled"
    

    3. Restart the MongoDB service:

    sudo systemctl restart mongod
    

Open Firewall Port

To allow external connections to MongoDB, open port 27017:

# Open MongoDB port
sudo firewall-cmd --permanent --add-port=27017/tcp
sudo firewall-cmd --reload

Example Commands for MongoDB on Amazon Linux

Insert a Document

Code:


// Insert a document into the 'products' collection
db.products.insertOne({ name: "Laptop", price: 1200, inStock: true });

Query Documents

Code:


// Find all documents in the 'products' collection
db.products.find();

Troubleshooting Tips

Issue Solution
MongoDB service fails to start Check the log at /var/log/mongodb/mongod.log for error messages.
GPG key import issues Ensure the key URL is correct and your system has network connectivity.
Remote connection problems Verify MongoDB's bind IP in /etc/mongod.conf and check firewall rules.

Additional Notes:

  • Updates: Use sudo yum update periodically to update MongoDB.
  • Data Backup: Always back up your database using tools like mongodump before making major changes.
  • Compatibility: Ensure your MongoDB version is compatible with your Amazon Linux version.


Become a Patron!

Follow us on Facebook and Twitter for latest update.

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-amazon-linux.php