How to Setup MongoDB on Amazon Linux EC2

2020/2/113 min read
bookmark this
Responsive image

This blog shows how to set up a MongoDB database environment on the cloud, AWS EC2 with Amazon Linux. 

Pre requirement

  • You'll need an AWS account, if you never set up an AWS account before, go here to create an AWS account.

Create EC2 Instance

First, we'll need to set up an EC2 instance, then we'll SSH into the EC2 instance and set up the MongoDB database.

Create EC2 Instance with AWS Linux AMI.

Choose t2.micro.

During the setup everything as default is fine for this blog, we'll use default VPC, as long as the new EC2 can access the internet. 

For the security group, we'll just open port 22, so we can SSH later to into EC2 for setup MongoDB.

Setup MongoDB at EC2 AWS Linux

After SSH into EC2, we'll need to create mongodb-org-4.4.repo file at location /ect/yum.repos.d.

cd /etc/yum.repos.d

Then, let's create mongodb-org-4.4.repo file.

nano mongodb-org-4.4.repo

 This will open the file window, copy the following mongod repo at the file and save it.

[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

Now, mongod 4.4 repo is ready, you should be able to install mongodb package.

sudo yum install -y mongodb-org

Once you see start downloading mongodb packages, that's a good sign. Once install MongoDB package is finish, you can verify by checking this folder is exist or not. If the folder exists, means MongoDB installation is done and you should be good to run MongoDB.

cd /var/lib/mongo

Enable MongoDB on Linux

Run following command will start MongoDB process at the EC2 Instance

sudo systemctl start mongod

Run this command will enable mongod, even after reboot the EC2

sudo systemctl enable mongod

Now, at this point, your MongoDB on Linux Server should be ready. If you have data you can import or restore it to the MongoDB server.

Create Database, Collection, and Record

Type mongo then it will start MongoDB shell, now let's play around at the server to check everything works.

mongo

Create Database

The first command will show all the databases, then trying to create a new database myDB.

show dbs
use myDB

Create Table and Record

This command will create myUser table and insert a new record.

db.myUser.insert({name: "john"});

Use mongoexport  to export data to JSON

We'll use mongoexport to export the data we just add to the myUser table. By default, the mongoexport tools are not installed. You'll need to install by run following command.

sudo yum list installed mongodb-database-tools

Now, all other tools mongodump, mongorestore, mongoimport, mongoexport, bsondump, mongostat, mongotop, mongofiles should be available to use.

The following command will output myUser table and generate data to myUser.json file.

mongoexport --collection=myUser --db=myDB --out=myUser.json

To Summarize

This blog is about how to set up MongoDB on Amazon Linux and will use database tools for backup or restore data. Following are all the steps.

cd /etc/yum.repos.d

nano mongodb-org-4.4.repo

[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

sudo yum install -y mongodb-org

sudo systemctl start mongod