You can support us by downloading this article as PDF from the Link below. Download the guide as PDF

In this guide, we will cover the steps to install MongoDB 4 on Ubuntu 18.04 / Ubuntu 16.04 Linux system. MongoDB is an open source NoSQL database system written in C++ that provides scalability, high performance/availability. NoSQL database systems are often referred to as Document-oriented databases.

MongoDB common use case is storage and management of Big Data-sized collections of literal documents like text documents, email messages, XML documents and many others.

To get a highly available MongoDB setup, use How to Setup MongoDB Replication On Ubuntu 18.04 LTS guide.

MongoDB 4 installation on Ubuntu 18.04/16.04

There are two ways of installing MongoDB on Ubuntu systems.

  • Install MongoDB from apt repository
  • Install MongoDB from a downloaded.deb package

This guide will demonstrate installation of MongoDB 4 on Ubuntu 18.04 and Ubuntu 16.04 system using apt repository method. Let’s now install MongoDB on Ubuntu 18.04 / Ubuntu 16.04 Linux.

Step 1: Import MongoDB public GPG Key:

Before you can install any package from MongoDB apt repository, you need to download and import GPG key to your system.

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

Step 2: Add MongoDB 4 APT Repository on Ubuntu 18.04 / Ubuntu 16.04

After importing GPG key, proceed to add the repository.

Ubuntu 18.04:

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

Ubuntu 16.04:

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

Step 3: Install MongoDB 4 on Ubuntu 18.04 / Ubuntu 16.04

Update package database and install MongoDB packages:

sudo apt update
sudo apt install -y mongodb-org

The service name is,mongod , you can start the application by running:

sudo systemctl enable --now mongod

Check status using:

$ sudo systemctl status mongod
● mongod.service - MongoDB Database Server
    Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
    Active: active (running) since Thu 2019-10-17 05:57:30 UTC; 32s ago
      Docs: https://docs.mongodb.org/manual
  Main PID: 20493 (mongod)
    CGroup: /system.slice/mongod.service
            └─20493 /usr/bin/mongod --config /etc/mongod.conf
 Oct 17 05:57:30 ubuntu18 systemd[1]: Started MongoDB Database Server.

The service should be listening on port 27017

$ netstat -tunelp | grep 27017
(Not all processes could be identified, non-owned process info
  will not be shown, you would have to be root to see it all.)
 tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      112        53728      -          

MongoDB main configuration file is /etc/mongod.conf You can tweak the settings to your liking, but remember to restart mongod service whenever you make a change.

Test connection:

$ mongo --eval 'db.runCommand({ connectionStatus: 1 })'
MongoDB shell version v4.2.0
 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
 Implicit session: session { "id" : UUID("85e2796a-51be-4a49-9c98-63b3ce353fee") }
 MongoDB server version: 4.2.0
 {
     "authInfo" : {
         "authenticatedUsers" : [ ],
         "authenticatedUserRoles" : [ ]
     },
     "ok" : 1
 }

You can confirm that everything is working fine from "ok" : 1.You can also try to create a test database and insert some dummy data.

# mongo
...
To enable free monitoring, run the following command:
db.enableFreeMonitoring()
---

> use test_db # This will create database called test_db
switched to db test_db
> db # Show current database
test_db
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> db.files.insert({"name":"rap"}) # Insert data to db
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test_db 0.000GB
> db.dropDatabase() # Drop our test db
> exit
bye

That’s all on how to install MongoDB 4 on Ubuntu 18.04 / Ubuntu 16.04 Linux server. Until next time, stay connected and enjoy your Tech life.

You can support us by downloading this article as PDF from the Link below. Download the guide as PDF