How to install Mongodb on AWS EC2 Instance?

Setu Kumar Basak
4 min readJul 18, 2018

Recently, i was trying to install Mongodb on AWS EC2 instance. It was very tough for me to find out the exact steps to follow. After searching and implementing a lot, i could finally install mongodb on an AWS EC2 instance.

At first, we need to open the AWS EC2 Dashboard like below and then click the Launch Instance button.

Then, we need to select one Amazon Machine Image (AMI). In this case, we will select Ubuntu Server 16.04 LTS (HVM), SSD Volume Type.

Now, we need to choose an instance type. We can choose anyone. I am choosing the instance which is eligible for free tier and click the Review and Launch button.

Then, we need to click the Launch button.

An window will appear for selecting an existing key pair or creating a new key pair. We will choose Create a new key pair option from the drop-down and give MongoDB_Database as Key pair name. Then, we will download the key pair by clicking on Download Key Pair button. After that, will click the Launch Instances button to launch the EC2 instance.

It will take some time to launch the EC2 instance. We will click on the highlighted id of the instance.

Our instance will be up and running now.

We will give a name like MongoDB of the instance by clicking on the Name section.

Then, we have to install PuTTY from here. After installing PuTTY, we will open the PuTTY Key Generator. We can find the puttygen.exe in C:\Program Files\PuTTY folder or the folder where we have installed PuTTY.

Then, we have to load the key pair file that we have downloaded before in the PuTTY Key Generator.

Then, we will save the private key as MongoDB_Database.ppk by clicking on the Save private key button.

Then, we will go to the EC2 dashboard and copy the public ip address like below:

Now, we will open the PuTTY and paste the copied ip address as the Host Name for IP address.

Then, we will go to the SSH/Auth section and point the private key we have saved before. We will click Open button to open the terminal.

If we get below alert, we need to click on Yes button and move forward.

We will get a PuTTY terminal like below:

Now, we will login as: ubuntu. Next, we have to run some commands in the terminal.

Step 1 — Importing the Public Key

sudo apt-key adv — keyserver hkp://keyserver.ubuntu.com:80 — recv EA312927

Step 2 — Create source list file MongoDB

echo “deb http://repo.mongodb.org/apt/ubuntu “$(lsb_release -sc)”/mongodb-org/3.2 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Step 3 — Update the repository

sudo apt-get update

Step 4 — Install MongoDB

sudo apt-get install -y mongodb-org

Step 5 — Start MongoDB

sudo service mongod start

sudo service mongod status

Step 6 -enter the etc folder

cd /etc

Step 7 — Make Changes in mongod.conf file

sudo nano mongod.conf

comment the BindIP & save it.

Step 8 — Restart MongoDB

sudo service mongod restart

sudo service mongod status

After these all steps, our MongoDB server will be up and running in the AWS EC2 instance. We can write mongo in the terminal and then write show dbs to see the databases.

But, creating database or playing with it from terminal is kind of boring and hectic. So, in the next article, i will try to cover how to connect the mongodb server running on EC2 instance from robomongo.

Thanks everyone :).

--

--