How to Setup MongoDB on Amazon Linux EC2

2020/02/113 min read
bookmark this
Responsive image

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Create EC2 Instance
  4. Setup MongoDB on EC2 Amazon Linux
  5. Enable MongoDB on Linux
  6. Create Database, Collection, and Record
  7. Use mongoexport to Export Data to JSON
  8. Conclusion

Introduction

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

Prerequisites

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

Create EC2 Instance

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

Create an EC2 instance with the AWS Linux AMI.

Choose t2.micro.

During the setup, everything can be left as default for this blog. We'll use the default VPC, as long as the new EC2 instance can access the internet.

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

Setup MongoDB on EC2 Amazon Linux

After SSHing into the EC2 instance, we'll need to create the mongodb-org-4.4.repo file at the location /etc/yum.repos.d.

cd /etc/yum.repos.d

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

nano mongodb-org-4.4.repo

This will open the file editor. Copy the following MongoDB repo configuration into 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 the MongoDB 4.4 repo is ready. You should be able to install the MongoDB package.

sudo yum install -y mongodb-org

Once you see it start downloading MongoDB packages, that's a good sign. Once the MongoDB package installation is finished, you can verify by checking whether the following folder exists. If the folder exists, the MongoDB installation is done and you should be good to run MongoDB.

cd /var/lib/mongo

Enable MongoDB on Linux

Run the following command to start the MongoDB process on the EC2 instance.

sudo systemctl start mongod

Run this command to enable mongod so it starts automatically even after rebooting the EC2 instance.

sudo systemctl enable mongod

Now, at this point, your MongoDB on the 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 to start the MongoDB shell. Now let's play around on the server to check that everything works.

mongo

Create Database

The first command will show all the databases, then we'll create a new database called myDB.

show dbs
use myDB

Create Table and Record

This command will create the 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 added to the myUser table. By default, the mongoexport tools are not installed. You'll need to install them by running the following command.

sudo yum list installed mongodb-database-tools

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

The following command will export the myUser table and generate data to a myUser.json file.

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

Conclusion

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

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