Install MongoDB 3.4 Database System on Ubuntu 16.04

MongoDB database Logo

Introduction to the MongoDB Database System

MongoDB is an open source database system, classified as NoSQL. Unlike a relational database, MongoDB stores data in documents, with the documents collected in collections. A MongoDB document is a data structure composed of field and value pairs, in the following format:

{
   field: value
   field: value
   field: value
   ...
}

For example:

{
   name: "Giuseppe"
   site: "Unixmen"
   groups: ["unixmen", "sysadmins", "linux"]
}

This example shows how the document format is similar to JSON. Data fields can vary from document to document and data structure can be changed over time.

MongoDB is a distributed database, so scaling, distribution and high availability are built in features. It is free and open source software, distributed under the terms of the GNU Affero General Public License (or GNU AGPL).

Prerequisites

  • 1 Ubuntu 16.04 server.

For this tutorial we have used a server running Ubuntu 16.04 with 3 GB of RAM.

Get MongoDB Packages

MongoDB is available on Ubuntu repositories. However, the packaged version is not the latest stable, as we can see by executing the following command:

[email protected]:~$ sudo apt-cache policy mongodb
mongodb:
  Installed: (none)
  Candidate: 1:2.6.10-0ubuntu1
  Version table:
     1:2.6.10-0ubuntu1 500
        500 http://it.archive.ubuntu.com/ubuntu xenial/universe amd64 Packages

We will use the official MongoDB repository for retrieving a newer version.

Import MongoDB Public Key

The Ubuntu package management tools ensure package consistency and authenticity by requiring that distributors sign packages with GPG keys. You can import MongoDB GPG public key by executing the following command:

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

Create a List File for MongoDB

Create a list file for MongoDB. This operation is required by apt. Execute:

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

Reload apt packages database by executing:

$ sudo apt-get update

Install MongoDB

apt is now configured. We can use it for installing MongoDB packages:

$ sudo apt-get install mongodb-org -y

apt will install the latest versions of several MongoDB packages, and create a new user, as showed by the command’s output:

Setting up mongodb-org-shell (3.4.4) ...
Setting up mongodb-org-server (3.4.4) ...
Adding system user `mongodb' (UID 111) ...
Adding new user `mongodb' (UID 111) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 117) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb

It will also create a new unit file for systemd, located in /lib/systemd/system, so we can launch MongoDB with the systemctl tool.

Start MongoDB

As previously mentioned, we can use systemctl to start MongoDB. First, execute the following command:

$ sudo systemctl start mongod

Check MongoDB status:

$ sudo systemctl status mongod

mongod.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: 
   Active: active (running) since ...

Next, enable automatic starting at boot time:

$ sudo systemctl enable mongod

With this, MongoDB installation is complete.

Starting MongoDB

When MongoDB service is running, just execute the following command for starting MongoDB shell:

$ mongo

In the shell, execute the help command for a list of available commands:

> help

It will print out:

        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        sh.help()                    sharding helpers
        rs.help()                    replica set helpers
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce

        show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time >= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, 'global' is default
        use <db_name>                set current database
        db.foo.find()                list objects in collection foo
        db.foo.find( { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell

Conclusion

This concludes our tutorial about installing MongoDB Community Edition on an Ubuntu 16.04 server. We’ll go through more details on how to use MongoDB in production environments in future tutorials. So, keep following Unixmen!