How to Install MongoDB Community Edition on Debian 8 “Jessie” and on Ubuntu 16.04 “Xenial” Linux Systems

undefined

In this post, we will show you how to install MongoDB community edition on Linux systems (Debian Family “Debian 8 and Ubuntu 16.04”). This tutorial will help you to install the latest stable release “MongoDB 3.4, exists at the time of writing this post” on Debian 8 and Ubuntu 16.04 LTS Linux systems.

MongoDB is a NoSQL database intended for storing large amounts of data in document-oriented storage with dynamic schemas. NoSQL refers to a database with a data model other than the tabular format used in relational databases such as MySQL, PostgreSQL, and Microsoft SQL. MongoDB features include: full index support, replication, high availability, and auto-sharding. So, let’s start our installation steps.

Prerequisites

Before begin our installation steps, we need to check the following points.
  • Our Debian 8, Ubuntu 16.04 must be 64-bit systems, and Ubuntu must be LTS (long-term support). Run the following command:
$ uname -r
3.16.0-4-amd64
  • We need to run the following steps as the root user, run the following command:
$ sudo su -

Step 1: Add MongoDB Apt Repository

The first step is adding the MongoDB apt repository, this step is consists of two inner steps, run the following commands:

1. First import public key of MongoDB apt repository in our system using following command:

# apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

2. Create the MongoDB APT repository file in /etc/apt/sources.list.d/mongodb-org-3.4.list. Depending on your OS run one of the following commands:

  • For Ubuntu 16.04
# echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.4.list
  • For Debian 8
# echo "deb http://repo.mongodb.org/apt/debian jessie/mongodb-org/3.4 main" | tee /etc/apt/sources.list.d/mongodb-org-3.4.list

Now, Lets install the MongoDB packages

Step 2 : Install MongoDB Server

Now, we’ll use the apt package manager to install mongodb-org package “A metapackage that will automatically install the four component packages mongodb-org-server, mongodb-org-mongos, mongodb-org-shell, and mongodb-org-tools”. Run the following commands will install latest stable version available.

# apt-get update
# apt-get install -y mongodb-org

Step 3 : Enable, Start, and Restart MongoDB Server

As we successfully installed all the four packages included in the mongodb-org package, we need to enable and start our MongoDB server.

To enable the server to start at reboot and boot time, run the following command:

# systemctl enable mongod

To start our MongoDB server, run the following command:

# systemctl start mongod

To restart MongoDB server “in case of configuration changes”, run the following command:

# systemctl restart mongod

Step 4: Checking the MongoDB Version and running service

To check installed MongoDB version, run the following command:

# mongo --version
MongoDB shell version v3.4.2
git version: 3f76e40c105fc223b3e5aac3e20dcd026b83b38b
OpenSSL version: OpenSSL 1.0.1k 8 Jan 2015
allocator: tcmalloc
modules: none
build environment:
 distmod: debian81
 distarch: x86_64
 target_arch: x86_64

As you see, we are using MongoDB version v3.4.2 for Debian 8.

Now, we’ll check that the service is up and running by running netstat command, run the following command:

# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address     Foreign Address   State     PID/Program name 
tcp        0      0 127.0.0.1:27017   0.0.0.0:*         LISTEN    24356/mongod 
tcp        0      0 0.0.0.0:3306      0.0.0.0:*         LISTEN    4304/mysqld 
tcp        0      0 0.0.0.0:111       0.0.0.0:*         LISTEN    1222/rpcbind 
tcp        0      0 0.0.0.0:22        0.0.0.0:*         LISTEN    340/sshd 
.....

As you see, our MongoDB server is up and listening on the localhost IP “127.0.0.1”and  port “27017”. The listening IP and port are the default shipped configuration.

Hints:

1. The default MongoDB configuration will accept connection only from the machine MongoDB packages installed on.
2. To change the listening IP, edit the configuration file "/etc/mongod.conf", and comment/remove the line start with "bindIp: 127.0.0.1", then restart you MongoDB server.

A final check we run is connecting to the MongoDB using command line and execute some test commands for checking proper working:

# mongo
MongoDB shell version: 3.4.2
connecting to: test

> show dbs
Semari 1.353GB
local 0.000GB
test 0.000GB

> exit

As you see, we used mongo command to connect to the server, by default we connected to the test database. Also we listed all the databases we have on the server by running “show dbs” command.

Extra Step: Uninstall MongoDB Community Edition

If you want to completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs.

Hints:

1. This process will completely remove MongoDB, its configuration, and all databases.
2. This process is not reversible, so ensure that all of your configuration and data is backed up before proceeding.

Run the following steps to completely uninstall MongoDB server.

  • Stop the MongoDB Server.
    You need to stop the mongod process by issuing the following command:
# systemctl stop mongod
  •  Remove the installed Packages.
    Remove any MongoDB packages that you had previously installed.
# apt-get purge mongodb-org*
  • Remove Data and Log Directories.
    Remove MongoDB databases and log files.
# rm -rf /var/log/mongodb
# rm -rf /var/lib/mongodb

Finally, I hope this article is helpful for you.

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

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