Cyberithub

Step by Step Guide to Install MongoDB on Ubuntu 20.04 LTS

Advertisements

In this article, I will take you through the step by step guide to install MongoDB on Ubuntu 20.04 LTS. MongoDB is a cross platform document-oriented NoSQL Database program which uses JSON like documents to store data. It is designed using C++ Programming Language. MongoDB is widely popular for being a high scalable and performance oriented database. It provides a unified database solutions to all those developers and programmers who are looking for features like agility and scalability along with some Real Time processing. It is also very easy to install and manage. We will see some more features below. More on MongoDB official Documentation.

Main Features of MongoDB

  • It supports Ad-hoc queries.
  • It provides high availability with replica sets.
  • It scales horizontally using sharding.
  • It can also be used as a File System.
  • It supports fixed-size collections called capped collections.
  • It supports multi-document ACID transactions.

Step by Step Guide to Install MongoDB on Ubuntu 20.04 LTS

Step by Step Guide to Install MongoDB on Ubuntu 20.04 LTS

Also Read: How to Install and Setup MediaWiki on Ubuntu 20.04 LTS [Easy Steps]

Step 1: Prerequisites

a) You should have a running Ubuntu 20.04 LTS System.

b) You should have sudo or root access to run privileged commands.

c) You should have apt or apt-get  utility installed in your System.

d) You should also have wget, apt-key and tee utility available in your System.

 

Step 2: Import Public Key

In the first step, you need to import the MongoDB public GPG Key from https://www.mongodb.org/static/pgp/server-5.0.asc using apt-key add command as shown below.

root@localhost:~# wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add - 
OK

 

Step 3: Create a List File for MongoDB

In the next step, you need to create a list mongodb-org-5.0.list for Ubuntu 20.04 LTS under /etc/apt/sources.list.d directory to download and install all the MongoDB packages from the added repository. This can be done by using below command.

root@localhost:~# 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
deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse

 

Step 4: Reload Local Package Database

Then update the local Package database with the new repository package information by using apt update or apt-get update command as shown below.

root@localhost:~# apt-get update
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,303 kB]
Get:5 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [554 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [279 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu focal-updates/universe i386 Packages [646 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [870 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [362 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [944 B]
Get:12 http://in.archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [10.4 kB]
Ign:13 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 InRelease
Get:14 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 Release [4,406 B]

 

Step 5: Install MongoDB Packages

Although you can download and install any specific version on MongoDB but if you want to install the latest stable version then you need to use apt-get install -y mongodb-org command as shown below.

root@localhost:~# apt-get install -y mongodb-org
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
mongodb-database-tools mongodb-mongosh mongodb-org-database mongodb-org-database-tools-extra mongodb-org-mongos mongodb-org-server mongodb-org-shell
mongodb-org-tools
The following NEW packages will be installed:
mongodb-database-tools mongodb-mongosh mongodb-org mongodb-org-database mongodb-org-database-tools-extra mongodb-org-mongos mongodb-org-server
mongodb-org-shell mongodb-org-tools
0 upgraded, 9 newly installed, 0 to remove and 314 not upgraded.
Need to get 147 MB of archives.
After this operation, 465 MB of additional disk space will be used.

 

Step 6: Start MongoDB

After successful installation, you need to start the MongoDB Service by using systemctl start mongod command as shown below. Then check the status using systemctl status mongod command. By default, MongoDB runs on Port 27017.

root@localhost:~# systemctl start mongod
root@localhost:~# systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Sun 2021-11-07 08:58:23 IST; 2s ago
Docs: https://docs.mongodb.org/manual
Main PID: 4608 (mongod)
Memory: 65.0M
CGroup: /system.slice/mongod.service
└─4608 /usr/bin/mongod --config /etc/mongod.conf

Then enable the service by using systemctl enable mongod command.

root@localhost:~# systemctl enable mongod
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.

If you want to change some database configuration then you can do it through /etc/mongod.conf file and then simply restart the daemon service to take the new configuration.

root@localhost:~# systemctl restart mongod

 

Step 7: Start Mongo Session

You can now start using MongoDB from mongosh session. You just need to run mongosh command to start the session.

root@localhost:~# mongosh
Current Mongosh Log ID: 618748999509a55395fe4acc
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB: 5.0.3
Using Mongosh: 1.1.2

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
The server generated these startup warnings when booting:
2021-11-07T08:58:25.149+05:30: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2021-11-07T08:58:25.342+05:30: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------

test>

 

Step 8: Uninstall MongoDB

To remove MongoDB completely from your System, first you need to stop mongod service using systemctl stop mongod command and get it confirmed by checking the status using systemctl status mongod command.

root@localhost:~# systemctl stop mongod
root@localhost:~# systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sun 2021-11-07 09:04:08 IST; 5s ago
Docs: https://docs.mongodb.org/manual
Process: 4608 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=0/SUCCESS)
Main PID: 4608 (code=exited, status=0/SUCCESS)

Then remove all the MongoDB previously installed packages using apt-get purge mongodb-org* -y command.

root@localhost:~# apt-get purge mongodb-org* -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'mongodb-org-unstable-tools' for glob 'mongodb-org*'
Note, selecting 'mongodb-org-mongos' for glob 'mongodb-org*'
Note, selecting 'mongodb-org-tools' for glob 'mongodb-org*'
Note, selecting 'mongodb-org-unstable-database-tools-extra' for glob 'mongodb-org*'
Note, selecting 'mongodb-org-unstable-server' for glob 'mongodb-org*'
Note, selecting 'mongodb-org-unstable-shell' for glob 'mongodb-org*'
Note, selecting 'mongodb-org-shell' for glob 'mongodb-org*'
Note, selecting 'mongodb-org-unstable-mongos' for glob 'mongodb-org*'
Note, selecting 'mongodb-org-database-tools-extra' for glob 'mongodb-org*'
Note, selecting 'mongodb-org-unstable' for glob 'mongodb-org*'
Note, selecting 'mongodb-org-tools-unstable' for glob 'mongodb-org*'

Finally, remove all the MongoDB data directories from /var/log directory using rm -r /var/log/mongodb and rm -r /var/lib/mongodb command as shown below.

root@localhost:~# rm -r /var/log/mongodb
root@localhost:~# rm -r /var/lib/mongodb

Leave a Comment