Install MongoDB on AWS EC2 Instances.

We will see how to install MongoDB on AWS ec2 Instances, amazon Linux 2 or we can install MongoDB of any version on the AWS Linux 2 server in this article.

The Amazon Linux 2 server is the RPM-based server with the 5 years Long Term Support by AWS. (Amazon Web Services).

MongoDB is a No-SQL database which is written in C++, It uses a JSON like structure. MongoDB is a cross-platform and document-oriented database.

The initial release of the MongoDB was on 11 February 2009, you can find the main website of the MongoDB as well the git repository

So let’s install MongoDB on AWS EC2 Instance. Find the cheat sheet below for Quick Installation.

After come and read the whole post to get detailed knowledge about the Installation of MongoDB.

Cheat Sheet of How to Install MongoDB on AWS ec2 instance

STEP 1: Add Repository into the AWS ec2 instance/Amazon Linux 2.

Create a file with the name “mongo-4.4.repo” into the “/etc/yum.repos.d/” the directory or you can use any editor of your choice and paste the below lines.

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

STEP 2: Install MongoDB 4.4 on AWS ec2 instance/Amazon Linux 2.

$ sudo yum install mongodb-org -y

STEP 3: Start the Service of MongoDB Server on AWS ec2 instance/Amazon Linux 2.

$ sudo systemctl start mongod

Check the Status for MongoDB.

$ sudo systemctl status mongod

Restart the service for MongoDB.

$ sudo systemctl restart mongod

Stop the service for MongoDB:

$ sudo systemctl stop mongod

Start the service at the time of Booting:

$ sudo systemctl enable mongod

STEP 4: Use the “mongo” command in the terminal or BASH to connect with MongoDB.

$ mongo

STEP 1: Add Repository to Install MongoDB on AWS ec2 instance.

Create a file with the name “mongo-4.4.repo” into the “/etc/yum.repos.d/” the directory or you can use any editor of your choice and paste the below lines.

Install MongoDB on AWS ec2 instance amazon linux 2
Repo for MongoDB
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc

So, after that save and quit the file but if you want to install version 4.0 into the CentOS Server then replace the configuration like below.

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

So we have made 3 changes into our repo file,

1. we have to change the name. — [mongodb-org-4.0]

2. we have to change baseurl — baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/

3. we have to change the gpgkey version — gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

As you will notice we have just made the changes in the version of the MongoDB.

STEP 2: Install MongoDB 4.4 on the AWS ec2 instance/Amazon Linux 2.

So first, we will update the repository of MongoDB now we will install the MongoDB service.

$ sudo yum install mongodb-org -y

STEP 3: Start the Service of MongoDB on the AWS ec2 instance/Amazon Linux 2.

So we have install MongoDB on AWS, now we will start the services of the MongoDB server by “systemctl command”.

$ sudo systemctl start mongod

So to check the Status of the MongoDB service use status in the place of start

$ sudo systemctl status mongod
Start the Service for MongoDB

So if we have to restart the service for MongoDB on the AWS then we will use the below command.

$ sudo systemctl restart mongod

If we have to stop the service of MongoDB then use the stop option

$ sudo systemctl stop mongod

What if we want to start the service when my server got rebooted.

$ sudo systemctl enable mongod

So we have successfully installed the MongoDB server and use “mongo” in terminal or bash to check are you able to connect.

$ mongo

Allow MongoDB to connect from Remote IP

If you have to connect your MongoDB server from a Remote IP, then you have to make some configuration changes and restart the service of MongoDB.

So first we will look into the configuration file of the MongoDB server “/etc/mongod.conf”

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1

So you will find that the bind IP is 127.0.0.1 which will allow to local only, and we want to allow to any remote IP then change the bind IP to “0.0.0.0”

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

After that we will require restart to MongoDB service, so that we can access the port 27017 to any remote IP.

$ sudo systemctl restart mongod

If still not working then check your firewall, which will be active, now you have two option can inactive the firewall or you can allow the 27107 port number.

If you are facing any issue while installing MongoDB let us know, we will help you out. So we have successfully Install MongoDB on AWS.

Thanks to reading the article, learn how to install MongoDB on Ubuntu, Linux basic command, how to install MySQL.

install MongoDB on AWS amazon linux, install MongoDB on amazon linux AMI, install MongoDB on amazon ec2, install MongoDB on AWS server, install MongoDB shell amazon linux, install mongo shell on amazon linux, install MongoDB in AWS ec2 instance, install MongoDB in AWS linux.

Give your valuable time