Install MongoDB on EC2( Amazon Linux 2)

This tutorial explains, How to Install MongoDB on EC2 (Amazon Linux 2)

MongoDB is an opensource NoSQL database that keeps data as Jason-like structure, unlike SQL database that stores data in table structure.

Also ReadHow to install Anaconda on Linux

&&    15 Important PostgreSQL commands you must know

Install MongoDB on Ubuntu using Yum

Step 1– Update Amazon Linux 2

 $ yum update  -y

Step 2 – Create Yum repository

 $ nano /etc/yum.repos.d/mongodb-org-4.2.repo

Put the following content inside  /etc/yum.repos.d/mongodb-org-4.2.repo file and save and exit the file using     CTRL + x command.

name=MongoDB Repository

Step 2– Install MongoDB

  $ sudo yum install mongodb-org -y

Step 3- Check which init System your platform use to start/stop/restart MongoDB service.

$ ps -no-headers -o comm 1

 i) If output of above command is  systemd , run following command to start/stop/restart and check status of  the MondoDB service.

        $ sudo systemctl status mongod

        $ sudo systemctl start mongod

        $ sudo systemctl stop mongod

        $ sudo systemctl restart mongod 

ii) If output of above command is init, run the following command to start/stop/restart and check status of the MongoDB Service.

       $ sudo service mongod start   

       $ sudo service mongod status

       $ sudo service mongod stop

       $ sudo service mongod restart

Step 4 – check the MongoDB process and configuration file path by using following command.

   $ps -ef | grep mongod

Step 5- Connect Mongodb

Connect MongoDB by using the following command


Step 6 -Check Mongodb Version

  $mongo – -version

Some Basic Commands for MongoDB

1) To show all databases in MongoDB

   > show dbs ;

2)  create a database or switch to already created databases

  > use my-db;

  Note: This DB will not show in show dbs; command until we do not insert any data in this. 

3) Insert data into the newly created database.

$ db.Any data that represents your collection .insert({“data you want “:”value of the data”})

  > db.names.insert({“username_name”:”chandan”})

4) show your collections

 > show collections;

5) Delete your collections of your collection.drop();

 > db.”names”.drop() ;

6) Drop the database

Make sure to switch to the database you want to delete and run the following command.

 > db.dropDatabase() ;


Click to tweet this tip !

I hope you enjoyed this tutorial and learned to Install MongoDB on EC2. If you think this is helpful, please do share this post with others. Please also share your valuable feedback, comment or any query in the comment box. I will really happy to resolve your all queries.

Thank You

If you think we helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

You may also like…

Leave a Reply