How to Install MongoDB on Ubuntu 16.04

Gravatar Image


MongoDB is a free, open source document-oriented NoSQL database program that is renowned for its high performance. MongoDB is written in C++ and stores it’s data in a JSON style format called BSON or Binary JSON.It was introduced in 2009 and is being developed by MongoDB Inc.

In this tutorial, we will install MongoDB community version on an Ubuntu 16.04 based VPS.


Note: Run all commands in this tutorial without sudo if you execute them from the root user.

Adding MongoDB’s official repository

To ensure the credibility of the packages Ubuntu makes sure they are signed with GPG keys.
Let’s begin by importing the GPG keys we need for the official MongoDB repository:

 # sudo apt-key adv --keyserver hkp:// --recv EA312927

Next add the MongoDB repository in /etc/apt/sources.list.d using this command:

 # echo "deb "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

And then issue the update command so Ubuntu can read the packages from the newly added repository:

 # sudo apt-get update

Installing MongoDB

To start installing MongoDB from the repository we just added, we issue the command:

 # sudo apt-get install -y mongodb-org

Although the MongoDB repository now provides the unit file in the package, we left this part of the tutorial for educational purposes as it can be used to install other services.

Now we need to create a systemd unit file for MongoDB. First, let us explain briefly what systemd unit files are. Unit files keep information about services, sockets, devices, basically, any resource managed by systemd which is an init system used by a large number of Linux distributions.

Create the file in the /etc/systemd/system/ directory using nano:

 # sudo nano /etc/systemd/system/mongodb.service

Paste the following text below:

Description=High-performance, schema-free document-oriented database

ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf


Make sure to save (press Ctrl+O) and close (press Ctrl+X) the file.
Now we have to update systemd to include our newly created service and we enable and start the service:

 # sudo systemctl daemon-reload
 # sudo systemctl enable mongod
 # sudo systemctl start mongod

Check to see if the service is running:

 # systemctl status mongod

The output should look something like this:

● mongodb.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2017-06-29 07:13:54 CDT; 8s ago
 Main PID: 4734 (mongod)
   CGroup: /system.slice/mongodb.service
           └─4734 /usr/bin/mongod --quiet --config /etc/mongod.conf

Jun 29 07:13:54 test systemd[1]: Started High-performance, schema-free document-oriented database.

Configuring MongoDB administrator username

To set up the MongoDB administrator username and password first we need to open the MongoDB shell, type in:

 # mongo

Inside the mongo shell type this command to switch to the admin database:

 > use admin

Now let’s create the administrator username and set a password for the username:

 > db.createUser({user:"admin", pwd:"admin54321-", roles:[{role:"root", db:"admin"}]})

Note: You can substitute the value in pwd for your own password, like this: pwd:”mypassword”.
The output from the command above should look like this:

Successfully added user: {
        "user" : "admin",
        "roles" : [
                        "role" : "root",
                        "db" : "admin"

Type this command in the shell to exit the shell:

 > exit

Enable MongoDB authentication

Open /lib/systemd/system/mongod.service with nano:

 # sudo nano /lib/systemd/system/mongod.service

On the ExecStart line add a new option argument –auth, the line should look like this:

 ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf

Make sure to save (press Ctrl+O) and close (press Ctrl+X) the file.
Update systemd to include the new version of our modified service file:

 # sudo systemctl daemon-reload

Then restart MongoDB so the changes take effect:

 # sudo systemctl restart mongod

Now connect to the MongoDB shell using this command:

 # mongo -u admin -p --authenticationDatabase admin

You’ll get prompted for a password, enter the password you set above.
Once you are inside the shell verify you’re authenticated with the administrator user we created by issuing this command:

 > db.runCommand({connectionStatus : 1})

The output should look like this:

        "authInfo" : {
                "authenticatedUsers" : [
                                "user" : "admin",
                                "db" : "admin"
                "authenticatedUserRoles" : [
                                "role" : "root",
                                "db" : "admin"
        "ok" : 1

That’s it, you’ve successfully installed MongoDB on Ubuntu 16.04.