Install MongoDB on Ubuntu 16.04 LTS

on Tutorial • August 29th, 2017 • Write for Hevo

MongoDB is a free, distributed, and an open-source document database at its core that powers high availability, horizontal scaling, and geographic distribution. It stores data in JSON-like documents and supports Ad hoc queries, indexing, and real-time aggregation.

Install MongoDB

Step 1: Import the MongoDB repository

Import the public key used by the package management system.

The Ubuntu package management tools ensure package consistency and authenticity by verifying that they are signed with GPG keys. The following command will import the MongoDB public GPG key.

> sudo apt-key adv --keyserver hkp:// --recv 7F0CEB10</span>
Create a source list file for MongoDB

Create the /etc/apt/sources.list.d/mongodb-org-3.4.list list file using the command below.

> echo "deb xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

Update the local package repository

> sudo apt-get update

Step 2: Install the MongoDB packages

Install the latest stable version of MongoDB:
> sudo apt-get install -y mongodb-org
Install a specific release of MongoDB:

You must specify each component package specifically with their version number, check the following example:

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

Step 3: Launch MongoDB as a service on Ubuntu 16.04

We need to create a unit file, which tells systemd how to manage a resource. Most common unit type, service, determine how to start or stop the service, auto-start etc.

Create a configuration file named mongodb.service in /etc/systemd/system to manage the MongoDB service.

> sudo vim /etc/systemd/system/mongodb.service

Copy the following contents in the file.

#Unit contains the dependencies to be satisfied before the service is started.
Description=MongoDB Database
# Service tells systemd, how the service should be started.
# Key `User` specifies that the server will run under the mongodb user and
# `ExecStart` defines the startup command for MongoDB server.
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
# Install tells systemd when the service should be automatically started.
# `` means the server will be automatically started during boot.
Update the systemd service with the command stated below:
> systemctl daemon-reload
Start the service with systemcl.
> sudo systemctl start mongodb
Check if mongodb has been started on port 27017 with netstat command:
> netstat -plntu
Check if the service has started properly.
> sudo systemctl status mongodb

The output to the above command will show `active (running)` status with the PID and Memory/CPU it is consuming.

Enable auto start MongoDB when system starts.
> sudo systemctl enable mongodb

Stop MongoDB

> sudo systemctl stop mongodb

Restart MongoDB

> sudo systemctl restart mongodb

Step 4: Configure and Connect MongoDB

Open mongo shell

Open MongoDB shell on your server by typing below command:

> mongo
Switch to the database admin
> use admin
Create the root user
> db.createUser({user:"admin", pwd:”password", roles:[{role:"root", db:"admin"}]})

Exit from the MongoDB shell.

Connect MongoDB

Restart MongoDB( command mentioned above ) and connect with user created with this command:

> mongo -u admin -p admin123 --authenticationDatabase admin

You can see the mongo connecting. Check the databases using the following command:

> show dbs

Step 5: Uninstall MongoDB

Warning: All configuration and databases will be completely removed after this process. And it is irreversible, so ensure that all of your configuration and data is backed up before proceeding.

Remove packages

Stop the service using the above command and run below command to remove packages.

> sudo apt-get purge mongodb-org*
Remove data database/directories and log files.
> sudo rm -r /var/log/mongodb
> sudo rm -r /var/lib/mongodb

You have now learned how to install (and uninstall) MongoDB on Ubuntu 16.04.

If you have ever faced issues installing Kafka on Ubuntu, here’s a step wise guide.

No-code Data Pipeline for your Data Warehouse