Install MongoDB — A Beginner’s Guide to MongoDB Installation on Ubuntu with Secure Access Control Part.1

Photo by Fabian Grohs

In this guide, I’m going to teach you how to install MongoDB on your Linux Server. MongoDB is a cross-platform document-oriented database program with a key feature of being a non-SQL database. It stores data as documents in a JSON format which makes it easier to use in your programs.

In this installation, I am going to use MongoDB v4.2.8 LTS and Ubuntu v16.04.5 LTS. I will use root user throughout installation and database configuration but please note that, do not use root for anything other than user creation on Linux servers as for server administration best practices!

If you don’t need a customizable, flexible, and easily scalable solution of self-installation on your servers, you may also use MongoDB Cloud service. This service is free with limited server capabilities which are enough for development purposes. You can also upgrade your system later for production usage.

Here are the major steps to follow:

  1. Install MongoDB
  2. Determine your built-in init system
  3. Start MongoDB Service
  4. Create database users
  5. Enable Secure Access Control
  6. Authenticate with your user

MongoDB Installation with the Package Management System

Import the MongoDB public key on your server

Run the following command and import the public key that will be used by your package management system.

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

If the output is not “OK” probably because your server doesn’t have GnuPG installed already, so run the following command and try again adding the key. If this is not the problem you are facing, you are going to spend some time in Stack Overflow.

sudo apt-get install gnupg

Create a list file for MongoDB

Create the /etc/apt/sources.list.d/mongodb-org-4.2.list file for Ubuntu 16.04 (Xenial).

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

Reload local package database

Run the following line to reload the local package database.

sudo apt-get update

Install the MongoDB packages.

In this section, you can choose either the latest stable version or a specific release that you need.

To install the latest stable version, issue the following command.

sudo apt-get install -y mongodb-org

To install a specific release, you must specify each component package individually along with the version number, as in the following example.

sudo apt-get install -y mongodb-org=4.2.8 mongodb-org-server=4.2.8 mongodb-org-shell=4.2.8 mongodb-org-mongos=4.2.8 mongodb-org-tools=4.2.8

In addition, if you are looking for a stable environment where no unintended upgrades will occur, you can pin the package at the currently installed version with the following commands.

echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

Continue reading in Part 2 for Running the MongoDB.