Install MongoDB on Amazon Linux 2, AWS ec2 instance.

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.

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 CentOS 6, CentOS 7, and CentOS 8. 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/Amazon Linux 2.

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

For CentOS 6/7/8:

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.

For CentOS 6/7/8:

$ sudo yum install mongodb-org -y

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

For CentOS 7/8:

$ sudo systemctl start mongod

For CentOS 6:

$ sudo service mongod start 

Check the Status:

For CentOS 7/8:

$ sudo systemctl status mongod

For CentOS 6:

$ sudo service mongod status 

Restart the service:

For CentOS 7/8:

$ sudo systemctl restart mongod

For CentOS 6:

$ sudo service mongod restart 

Stop the service:

For CentOS 7/8:

$ sudo systemctl stop mongod

For CentOS 6:

$ sudo service mongod stop 

Start the service at the time of Booting:

For CentOS 7/8

$ sudo systemctl enable mongod

For CentOS 6

$ sudo chkconfig mongod on

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

$ mongo

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

For CentOS 6/7/8:

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
[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.

For CentOS 6/7/8

$ sudo yum install mongodb-org -y

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

So after the successful installation, we will start the services of the MongoDB server by “systemctl command”.

For CentOS 7/8

$ sudo systemctl start mongod

For CentOS 6

$ sudo service mongod start

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

For CentOS 7/8

$ sudo systemctl status mongod

For CentOS 6

$ sudo service mongod status

If we have to restart the service of MongoDB in the CentOS server then we will use the below command.

For CentOS 7/8

$ sudo systemctl restart mongod

For CentOS 6

$ sudo service mongod restart

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

In CentOS 7/8

$ sudo systemctl stop mongod

In CentOS 6

$ sudo service mongod stop.

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

In CentOS 7/8

$ sudo systemctl enable mongod

In CentOS 6

$ sudo chkconfig mongod on

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.

In CentOS 7/8

$ sudo systemctl restart mongod

In CentOS 6

$ sudo service mongod restart

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.

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

install MongoDB on 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.

11 Replies to “Install MongoDB on Amazon Linux 2, AWS ec2 instance.

Give your valuable time