How to Install and Securing MongoDB on Ubuntu 18.04 and 20.04

How can we help?
< All Topics

How to Install and Securing MongoDB on Ubuntu 18.04 and 20.04

Many people wonder on how to install and securing MongoDB especially in Ubuntu 18.04 and 20.04. Before that, let’s define what is MongoDB.

MongoDB is a general purpose document database which stores its data in JSON-like documents. It also claimed this is the most natural way to think about data.

#JSON file example
  "os": "Ubuntu",
  "company": "Canonical Ltd.",
  "founder": {
    "name": "Mark Shuttleworth"
  "age": "16",
  "flavours": [
    "👽︎ Kubuntu",
    "✔ Lubuntu",

MongoDB has features that traditional database in Relational Database Management (RDBMS), like MariaDB and PostgreSQL don’t have or need additional steps to set up properly, such as:

  1. Expressive and Flexible, which means you can create dynamic data structure, no database schema that you have to define first. Also, MongoDB supports schema-less database where in RDBMS you have to define the schema first, and you have to follow that schema.
  2. MongoDB can be easily scale out just by adding a new server as replica which will add more READ performance.
  3. In High availability terms, MongoDB cluster can automatically fail over to active node when a Master Node is down.
## MySQL way
# initialization 
   `os` VARCHAR(150) NOT NULL,
   `company` VARCHAR(150) NOT NULL,
   `founder` VARCHAR(150) NOT NULL,
   `age` INT, 
   `flavours` VARCHAR(150) NOT NULL)

# inserting the data
INSERT INTO `JSON_in_RDBMS` (`os`, `company`, `founder`, `age`, `flavours`) VALUES ('Ubuntu1', 'Canonical Ltd.', 'Mark Shuttleworth', '16', 'Kubuntu, Lubuntu, MATE, Xubuntu')

Installing MongoDB on Ubuntu

This tutorial will cover installation MongoDB Community Edition on 64-bit Ubuntu Linux LTS (long-term support) releases using the apt package manager, especially on the 18.04 (Bionic) and 20.04 (Focal) LTS releases on x86_64 architecture.

1. Import the public key used by the package management system.

# Make sure you have installed the gnupg package
sudo apt-get install gnupg
# Import the MongoDB public GPG Key
wget -qO - | sudo apt-key add -

2. Create a list file for MongoDB repository

# Create the /etc/apt/sources.list.d/mongodb-org-4.4.list file for Ubuntu
echo "deb [ arch=amd64,arm64 ] $(lsb_release -sc)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
# Then make sure the local package database reloaded
sudo apt-get update

3. Install MongoDB 

# Install the latest stable package
sudo apt-get install -y mongodb-org

4. Run MongoDB

# Start the process on systemd init 
sudo systemctl start mongod

5. MongoDB is up and running !

# Enable Autostart the process 
sudo systemctl enable mongod
# Restart the process 
sudo systemctl restart mongod
# Stop the process
sudo systemctl stop mongod
# Verify the process running or stopped
sudo systemctl status mongod

Securing MongoDB on Ubuntu

1. Connect to the MongoDB

# First Connect to the mongo instance
$ mongo
MongoDB shell version v4.4.3
connecting to: mongodb://
Implicit session: session { “id” : UUID(“0cfcaad9-7c56-40ea-81f8-af273c014d5e”) }
MongoDB server version: 4.4.3
Welcome to the MongoDB shell.

2. Create the user

# Copy paste this to mongodb shell

use ubi_db
user: “ubi_user”,
pwd: “ub1_p4ss”,
roles: [ { role: “readWrite”, db: “ubi_db” },
{ role: “read”, db: “ubi_db_other” } ]

If successfully created, it will show like this output

3. Then connect & authenticate as the user

# Inline output on mongo shell command
. > use ubi_db
switched to db ubi_db
. > db.auth(“ubi_user”, “ub1_p4ss”){x:1})
. >{x:1})
WriteResult({ “nInserted” : 1 })
. > use ubi_db_other
switched to db ubi_db_other
. >{})

Table of Contents

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

Ready, Set, Cloud

Ready, Set, Cloud