Objective

The objective is to install MongoDB on Ubuntu 18.04 Bionic Beaver Linux

Operating System and Software Versions

  • Operating System: - Ubuntu 18.04 Bionic Beaver
  • Software: - MongoDB 3.6 or higher

Requirements

Privileged access to your Ubuntu System as root or via sudo command is required.

Difficulty

EASY

Conventions

  • # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ - requires given linux commands to be executed as a regular non-privileged user

Instructions

Add MongoDB repository

First head over to the official MongoDB download page and take a note of the latest MongoDB version.

Note: At the time of writing there is currently no official release for Ubuntu 18.04 Bionic. From this reason we will be using Xenial version instead.

Open up terminal and enter the following linux commands while changing your desired mongoDB version and Ubuntu release codename:
$ codename=xenial
$ mongodb=3.6
Once the above variables are set, simply enter the below command to import MongoDB release signing key:
$ wget -qO- https://www.mongodb.org/static/pgp/server-${mongodb}.asc | sudo apt-key add
Next, add the repository:
$ sudo bash -c "echo deb http://repo.mongodb.org/apt/ubuntu ${codename}/mongodb-org/$mongodb multiverse > /etc/apt/sources.list.d/mongodb-org.list"
update the repository index:
$ sudo apt update


Install MongoDB

Use the following linux command to install MongoDB on Ubuntu 18.04 Bionic server:
$ sudo apt-get install -y mongodb-org

Start MongoDB Database

After installation the MongoDB database does not start by default. To start the database enter:
$ sudo service mongod start
Confirm the MongoDB status:
$ service mongod status
● mongod.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
   Active: active (running) since Wed 2018-02-21 14:29:07 AEDT; 7s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 2449 (mongod)
   CGroup: /system.slice/mongod.service
           └─2449 /usr/bin/mongod --config /etc/mongod.conf

Feb 21 14:29:07 ubuntu systemd[1]: Started High-performance, schema-free document-oriented database.
If you wish to start MongoDB database after your Ubuntu 18.04 server restart execute:
$ sudo systemctl enable mongod
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.


Connecting to MongoDB remotely

By default MongoDB listens on a loopback interface 127.0.0.1only. This means that any remote connections will be refused with an error:
W NETWORK  [thread1] Failed to connect to Mongodb-server:27017, in(checking socket for error after poll), reason: Connection refused
E QUERY    [thread1] Error: couldn't connect to server Mongodb-server:27017, connection attempt failed :
To bind MongoDB on specific IP address you need to edit MongoDB's configuration file /etc/mongod.conf. For example, to bind MongoDB to all network interfaces open the config file:
$ sudo nano /etc/mongod.conf 
Next, change line bindIp: 127.0.0.1 to bindIp: 0.0.0.0. Once ready restart MongoDB database:
$ sudo service mongod restart
In case you have a UFW firewall enabled you will also need to open TPC port 27017 to incoming traffic:
$ sudo ufw allow from any to any port 27017 proto tcp
To connect remotely use the command:
$ mongo --host MONGODB-IP-OR-HOST:27017

Change MongoDB default port

To change the default MongoDB listening port number on Ubuntu 18.04 edit MongoDB's configuration file /etc/mongod.conf and updated line port: 27017 to any desired number. Once ready restart MongoDB database:
$ sudo service mongod restart