Write Review

MongoDB is a document-centric NoSQL database program distinguished for its exceptional performance. It is a free, open-source solution published under the GNU Affero General Public License (GPL) and is commonly used in the modern day web applications. The application manifests the top qualities of a distributed database at its core, therefore horizontal scaling, geographical distribution, and high availability are built-in.

MongoDB is written in C++ language and stores data in a format called Binary JSON (BSON). The flexible JSON-like documents mean the data fields can vary from one document to the other and the structure can be modified over time. In addition, the database only offers unique packages for 64bit Long-term Support (LTS) Ubuntu releases. For example, the latest Ubuntu 18.04 (Bionic Beaver), Ubuntu 16.04 (Xenial XERUS), and Ubuntu 14.04 (Trusty Tahr).

Installing and configuring MongoDB on your Ubuntu 18.04 is a simple task that should not take much of your time. However, some steps may be intricate for newbies, and that’s why we compiled this guide. This tutorial will help you learn how to install and configure MongoDB 3.6 on your Ubuntu 18.04.

Ready? Let’s start

What to accomplish with this tutorial:

  • Install MongoDB version 3.6
  • Configure the MongoDB database


Before we embark on our endeavor, confirm that you have the following:

  • A 64bit Ubuntu server 18.04
  • Root privileges

Special note: If you haven't yet purchased a hosting plan, we recommend that you consult with HostAdvice's expert and user reviews before doing so. Go to HostAdvice's best MongoDB hosting, best VPS hosting, or best Linux Hosting to find the best rated web hosts in these categories.

Part One: Installing MongoDB

Step 1:
Import the Public key

The Ubuntu advanced package tool (package manager apt) requires the GNU Privacy Guard (GPG) of the software distributor to authenticate and ensure consistency of the package. For this reason, the first step when installing MongoDB is importing the GPG key to your Ubuntu server. Run the following command to import the key:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5

Step 2:
Create a MongoDB repository

Once you import the key, the next step is creating a list file for MongoDB in /etc/apt/sources.list.d/. Run the command below:

echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list

By now MongoDB is yet to release the packages for Ubuntu 18.04 (Bionic Beaver), but the Xenial Xerus should be working fine on your Ubuntu 18.04 LTS. In some case you might visit this URL before executing the above command.  In case a directory “bionic” appears on the web page, you should replace the term ‘xenial’ in the command above with the term ‘bionic’.

Step 3:
Update the repository

By now, you have the repository for your MongoDB 3.6. Next, you should issue an update command to allow Ubuntu to read packages from the newly created repository. To update the repository, run the following apt command:

sudo apt-get update

That’s it, the stage is set for MongoDB installation.

Step 4:
Install mongoDB

Now, type the following command to initiate the installation of MongoDB from the repository:

sudo apt-get install -y mongodb-org

The MongoDB apt installer will automatically create the mongod.service file for the systemd. However, if this is not created automatically you can craft it manually. The system unit file retains the information about sockets, services, devices, and other resources managed by the system. Create the file in /etc/systemd/system/ directory, use the following nano:

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

Then copy and paste the text below:

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

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


Press Ctrl + O to save the file and Ctrl + X to close it.  Next, update the systemd to incorporate the newly created service, then enable and induct the service.

# sudo systemctl daemon-reload
 # sudo systemctl enable mongod
 # sudo systemctl start mongod
Confirm the service is running using the following command:
# systemctl status mongod
If the service is running successfully, you will get the following command: 
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
     Docs: https://docs.mongodb.org/manual
 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.

You can also use a netstat command to confirm that MongoDB is running on port 27017. Use the following command:

netstat -plntu

You get the following output:

Part Two:
Configuring MongoDB username/password

By now, you have installed all the packages of MongoDB. Then next is configuring the username and password for your database server.

Step 1:
Open the mongo shell

Before setting up the username and password you must open the mongoDB shell for your database server. Type in:

# mongo

If you get the error: Failed global initialization: badvalue invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are correctly set. Then use the command below;

export LC_ALL=C

Step 2:
Change the database administrator

Once you are logged in the shell, type the command below to switch to the administrator database:

use admin

Step 3:
Create a root user

Now you can create an admin username, then create its password. Use the following command to achieve this:

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

From the command above it clear you have created a user called admin with password set as admin123. The user has the role/permission as rootuser whereas the database is admin.

Note: The password value can be substituted for your unique value e.g. you can set the password as MyPassword.

The command above will give the following output:

Exit the MongoDB shell by typing exit.

Step 4:
Enable mongoDB authentication

Locate and open the '/lib/systemd/system/mongod.service' using your nano editor:

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

On the ExecStart line, include an option argument line:

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

Press Ctrl+O to save and Ctrl+X to exit nano.

Next, update the system to incorporate a new version of the adjusted service file.

systemctl daemon-reload

Step 5:
Restart MongoDB

Now restart MongoDB to enable all the changes made to take effect. Use the following command:

# sudo systemctl restart mongod

Then run the command below to connect to the shell:

# mongo -u admin -p --authenticationDatabase admin

You will be requested to enter the password as set above. Once you are logged into the MongoDB shell verify that you are the authenticated user you created above. Run the following command:

> db.runCommand({connectionStatus : 1})

You will get the following output:

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

That is it!


You have successfully installed and configured MongoDB on Ubuntu 18.04. Enjoy the high availability, high performance, and the automatic scaling of MongoDB. Start up the MongoDB quickly and enjoy the convenience and fun of creating your applications.


Check out the top 3 Dedicated server hosting services:

Was this article helpful?