paint-brush
How to Integrate MongoDB in an AWS EC2 Instanceby@suylakshmanan
New Story

How to Integrate MongoDB in an AWS EC2 Instance

by Suyambukani LakshmananJuly 15th, 2024
Read on Terminal Reader
tldt arrow

Too Long; Didn't Read

Integrating MongoDB in an AWS EC2 instance involves several steps. The installation steps can vary depending on the Linux distribution you choose. To import the MongoDB public GPG key, run curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.gpg --dearmor.
featured image - How to Integrate MongoDB in an AWS EC2 Instance
Suyambukani Lakshmanan HackerNoon profile picture

Integrating MongoDB in an AWS EC2 instance involves several steps. Here’s a general outline to guide you through the process.

  1. Launch an EC2 Instance
    • Log in to your AWS Management Console.


    • Navigate to EC2, and launch an instance. Choose an appropriate Amazon Machine Image (AMI) based on your needs (e.g., Amazon Linux, Ubuntu).


    • Ensure your security group allows inbound traffic on the MongoDB port (default is 27017) from your IP address or a specific range.

  1. Connect to Your EC2 Instance

    • Once your instance is running, connect to it using SSH. For instance, ssh -i /path/to/your/key.pem ec2-user@your-instance-public-dns

  2. Install MongoDB

    • Update the package list and install MongoDB. The installation steps can vary depending on the Linux distribution you choose.


    a. For Ubuntu:

    • Update apt-get - sudo apt-get update

    • Install gnupg, sudo apt-get install gnupg curl

    • To import the MongoDB public GPG key, run

      curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor

    • Create the following file on Ubuntu

      echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

    • Update apt-get - sudo apt-get update

    • Install MongoDB sudo apt-get install -y mongodb-org


    b. For Amazon Linux:

    • Add MongoDB repository sudo vi /etc/yum.repos.d/mongodb-org-4.4.repo

    • Add the following:

      [mongodb-org-4.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/4.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc

    • Install MongoDB sudo yum install -y mongodb-org


  3. Start the mongod process by issuing the following commandsudo systemctl start mongod


    Verify that MongoDB has started successfully, sudo systemctl status mongod


    You can optionally ensure that MongoDB will start following a system reboot by issuing the following command: sudo systemctl enable To Stop MongoDB. As needed, you can stop the mongod process by issuing the following command: sudo systemctl stop mongod


    To restart MongoDB, sudo systemctl restart mongod

  4. If you receive an error similar to the following when starting mongod: Failed to start mongod.service: Unit mongod.service not found. Run - sudo systemctl daemon-reload


  5. Configure MongoDB

    • -MongoDB should start automatically after installation. You may need to adjust the configuration ‘/etc/mongod.conf‘ for network binding and security settings based on your requirements.


  6. Access MongoDB

    • By default, MongoDB should be accessible locally on port 27017 ‘mongodb://127.0.0.1:27017*‘*. If you need remote access, adjust your security group settings and MongoDB configuration accordingly.


  7. Test MongoDB

    • Verify MongoDB is running and accessible - ‘mongo --host your-instance-public-dns‘


  8. Create the user administrator

    In the admin database, add a user with the userAdminAnyDatabase role. This database acts as an admin DB which we create only for authentication purposes.


    Issue the following command to switch to admin DB even though you haven’t created it. It will be created automatically when you issue the command use admin.


  9. Security

    Ensure your MongoDB instance is properly secured with authentication and firewall rules.


  10. Backup

    Implement regular backups using AWS services like EBS snapshots or MongoDB's backup utilities.


  11. Scaling

    Consider using AWS services like DocumentDB or MongoDB Atlas for managed MongoDB clusters if your requirements expand beyond a single EC2 instance.


    By following these steps, you should be able to integrate MongoDB into your AWS EC2 instance successfully. Adjustments may be necessary based on specific requirements or changes in AWS services and MongoDB versions.