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.
Integrating MongoDB in an AWS EC2 instance involves several steps. Here’s a general outline to guide you through the process.
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.
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
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
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
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
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.
Access MongoDB
Test MongoDB
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.
Security
Ensure your MongoDB instance is properly secured with authentication and firewall rules.
Backup
Implement regular backups using AWS services like EBS snapshots or MongoDB's backup utilities.
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.