Install MongoDb on Ubuntu AWS Server
Install MongoDb on Ubuntu AWS Server

Hi Guys, Welcome to Proto Coders Point. In this AWS tutorial, we will Install MongoDB on Amazon ubuntu server instance.

In you don’t know how to create AWS ubuntu server & get connected to aws server using WinSCP, then checkout below article, where I have explained in step by step instruction create ubuntu server on AWS.


Lets get started

Install MongoDB community server edition on ubuntu AWS server

Below are some commands that you need to run on your ubuntu aws server to install mongoDB on cloud server.

1. Connect to AWS server & load putty terminal

I assume, you know how to connect to AWS cloud server using WinSCP & open the putty terminal to install mongoDB on server side.

winscp start putty terminal
winscp start putty terminal
putty terminal
putty terminal

Here is where you need to run cmds to install mongoDB.


2. Import MongoDB public GPG keys

To Install mongoDB we need to first download/import GPG key, in WinSCP putty terminal run below command

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

The cmd will respond OK.

If you get error, saying gnupg not install, then install it and retry above cmd.

1. Install gnupg

sudo apt-get install gnupg

2. retry importing GPG key

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

3. Create list file for MongoDB

Create a file which will hae mongodb org list version for your ubuntu.

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

4. local package reload

Run below cmd to reload all the local package database.

sudo apt-get update

5. Install MongoDb on ubuntu server aws

Now, finally we can install MongoDB on Ubuntu server, run below command.

Install MongoDb latest version

sudo apt-get install -y mongodb-org

Install specific version on mongo database

sudo apt-get install -y mongodb-org=5.0.5 mongodb-org-database=5.0.5 mongodb-org-server=5.0.5 mongodb-org-shell=5.0.5 mongodb-org-mongos=5.0.5 mongodb-org-tools=5.0.5

After successfuly installation, run below 6 echo cmds.

echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-database hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

We need to hold/pin, mongoDb from getting unintended upgrade version of mongodb-org, so below 6 cmd will hold, apt-get upgrade to update mongo database version.


6. How to start mongod service

To start/run, stop, or restart mongod server check below cmds

  1. sudo systemctl start mongod
  2. sudo systemctl stop mongod
  3. sudo systemctl restart mongod

Run mongoDb query

Start mongoD, just enter mongosh, which will take you to mongodb shell, where you can execute mongod queries to perform CRUD operations like creating/insertinf data, reading, updating & deleting the data from database.

mongosh

Therefore, we have successfully installed mongodb on ubuntu aws server.



Uninstall mongoDB from ubuntu completely

You can easily remove/uninstall mongod from ubuntu completely.

follow below steps to remove mongodb.

1. Stop mongod

sudo systemctl stop mongod

2. remove mongodb-org package

sudo apt-get purge mongodb-org*

3. remove/delete data directory

The below cmd will remove database & log files

sudo rm -r /var/log/mongodb

sudo rm -r /var/lib/mongodb


Conclusion

Therefore, above are the steps and command to install mongodb and uninstall it in ubuntu operating system.