Install MongoDB on Ubuntu 20.04

|
Last Updated:
|
|
Install MongoDB on Ubuntu 20.04

Follow through this tutorial to learn how to install MongoDB on Ubuntu 20.04. According to mongodb.com, “MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era...It is a scalable, flexible NoSQL document database platform designed to overcome the relational databases approach and the limitations of other NoSQL solutions.

Read about top 5 features of MongoDB.

Installing MongoDB on Ubuntu 20.04

MongoDB is available in both a community and an enterprise version;

  • MongoDB Community is the source available and free to use edition of MongoDB.
  • MongoDB Enterprise is available as part of the MongoDB Enterprise Advanced subscription and includes comprehensive support for your MongoDB deployment. MongoDB Enterprise also adds enterprise-focused features such as LDAP and Kerberos support, on-disk encryption, and auditing.

In this tutorial, we will learn how to install MongoDB community edition on Ubuntu 20.04.

Install MongoDB Community Edition on Ubuntu 20.04

Install MongoDB APT Repo on Ubuntu 20.04

To be able to install the latest stable versions of MongoDB on Ubuntu 20.04, you need to install MongoDB APT repository list.

As of this writing, MongoDB 4.4 is the current stable LTS release version. Be sure to replace the version numbers in the commands below accordingly.

Begin by installing the MongoDB APT repo signing key;

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

Next, install MongoDB APT Repo;

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

Update package cache;

apt update

Install latest version of MongoDB Community Edition on Ubuntu 20.04

Once the repo list is in place, run the command below to install the latest version of MongoDB on Ubuntu 20.04.

apt install -y mongodb-org

Running MongoDB on Ubuntu 20.04

When installed, MongoDB creates a systemd unit file called, mongod.service, under /lib/systemd/system/.

This service unit file can be used to manage MongoDB.

To start MongoDB service, execute the command;

systemctl start mongod

To check the status;

systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-03-09 16:56:41 UTC; 2min 8s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 6618 (mongod)
     Memory: 60.4M
     CGroup: /system.slice/mongod.service
             └─6618 /usr/bin/mongod --config /etc/mongod.conf

Mar 09 16:56:41 ubuntu20 systemd[1]: Started MongoDB Database Server.

To enable it to start on boot;

systemctl enable mongod

You can restart or stop is by executing the commands below respectively;

systemctl restart mongod
systemctl stop mongod

Connecting to MongoDB on Ubuntu

By default, MongoDB listens on TCP port 27017 by default, on a localhost.

apt install net-tools
netstat -altnp | grep :27
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      6618/mongod

In order to connect to it, you need to be logged in to the same host on which MongoDB is running and simply execute the command below;

mongo

The command above will launch an interactive MongoDB shell;

MongoDB shell version v4.4.4
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9d73e6f2-1a6e-4ea7-8887-a71f81dc1cd9") }
MongoDB server version: 4.4.4
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
---
...
---
>

To obtain help while logged into the MongoDB interactive shell, run the help command.

help
> help
	db.help()                    help on db methods
	db.mycoll.help()             help on collection methods
	sh.help()                    sharding helpers
	rs.help()                    replica set helpers
	help admin                   administrative help
	help connect                 connecting to a db help
	help keys                    key shortcuts
	help misc                    misc things to know
	help mr                      mapreduce

	show dbs                     show database names
	show collections             show collections in current database
	show users                   show users in current database
	show profile                 show most recent system.profile entries with time >= 1ms
	show logs                    show the accessible logger names
	show log [name]              prints out the last segment of log in memory, 'global' is default
	use <db_name>                set current database
	db.mycoll.find()             list objects in collection mycoll
	db.mycoll.find( { a : 1 } )  list objects in mycoll where a == 1
	it                           result of the last line evaluated; use to further iterate
	DBQuery.shellBatchSize = x   set default number of items to display on shell
	exit                         quit the mongo shell
>

Uninstall MongoDB CE on Ubuntu 20.04

If for some reasons you want to uninstall MongoDB and remove it completely on Ubuntu 20.04, you would simply have to stop and purge its by running the commands below;

systemctl stop mongod
apt remove --purge --auto-remove mongodb-org

Remove MongoDB databases;

rm -rf /var/lib/mongodb

Learn how to enable MongoDB authentication by following the link below;

Enable Authentication on MongoDB

Reference

Install MongoDB community Edition on Ubuntu

Further Reading

Getting Started with MongoDB

Other Tutorials

Install LibModsecurity with Apache on Ubuntu 20.04

Update/Change Kibana Visualization Index Pattern

Request control during screen share in Teams on Linux

Install Arkime (Moloch) Full Packet Capture tool on Ubuntu

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
gen_too
Co-founder of Kifarunix.com, Linux Tips and Tutorials. Linux/Unix admin and author at Kifarunix.com.

Leave a Comment