There is a new version of this tutorial available for Ubuntu 22.04 (Jammy Jellyfish).

How to Install and Configure MongoDB on Ubuntu 16.04 LTS

MongoDB is a NoSQL database that offers a high performance, high availability, and automatic scaling enterprise database. MongoDB is a NoSQL database, so you can't use SQL (Structured Query Language) to insert and retrieve data, and it does not store data in tables like MySQL or Postgres. Data is stored in a "document" structure in JSON format (in MongoDB called BSON). MongoDB was first introduced in 2009 and is currently developed by the company MongoDB Inc.

"MongoDB only provides packages for 64-bit LTS (long-term support) Ubuntu releases. For example, 12.04 LTS (precise), 14.04 LTS (trusty), 16.04 LTS (xenial), and so on.mongodb site.

Prerequisites

  • Ubuntu Server 16.04 - 64 bit
  • Root privileges

What we will do in this tutorial:

  1. Install MongoDB
  2. Configure MongoDB
  3. Conclusion

Install MongoDB on Ubuntu 16.04

Step 1 - Importing the Public Key

GPG keys of the software distributor are required by the Ubuntu package manager apt (Advanced Package Tool) to ensure package consistency and authenticity. Run this command to import MongoDB keys to your server.

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

Step 2 - Create source list file MongoDB

Create a MongoDB list file in /etc/apt/sources.list.d/ with this command:

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

Step 3 - Update the repository

update the repository with the apt command:

sudo apt-get update

Step 4 - Install MongoDB

Now you can install MongoDB by typing this command:

sudo apt-get install -y mongodb-org

We have to create a new MongoDB systemd service file in the '/lib/systemd/system' directory. Go to that directory and create the new MongoDB service file 'mongod.service' with vim editor.

cd /lib/systemd/system/
vim mongod.service

Paste script below:

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual

[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target

Save the file and exit.

Now update the systemd service with the command below:

systemctl daemon-reload

Start MongoDB and add it as a service to be started at boot time:

systemctl start mongod
systemctl enable mongod

Now check that MongoDB has been started on port 27017 with the netstat command.

netstat -plntu

Check if MongoDB is running.

Configure MongoDB username and password

When the MongoDB packages are installed you can configure username and password for the database server:

Step 1 - Open mongo shell

Before you set up a username and password for MongoDB, you need to open the MongoDB shell on your server. You can login by typing:

mongo

If you get error Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly, try the command:

export LC_ALL=C
mongo

Step 2 - Switch to the database admin

Once you`re in the MongoDB shell, switch to the database named admin:

use admin

Step 3 - Create the root user

Create the root user with this command :

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

Desc: Create user admin with password admin123 and have the permission/role as root and the database is admin.

Create a admin user in MongoDB

Now type exit to exit from MongoDB shell.

Step 4 - Enable mongodb authentication

Edit the mongodb service file '/lib/systemd/system/mongod.service' with your editor.

vim /lib/systemd/system/mongod.service

On the 'ExecStart' line 9, add the new option '--auth'.

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

Save the service file and exit vim.

Reload the systemd service:

systemd daemon-reload

Step 5 - Restart MongoDB and try to connect

Now restart MongoDB and connect with the user created.

sudo service mongod restart

and connect to the MongoDB shell with this command:

mongo -u admin -p admin123 --authenticationDatabase admin

and you will see the output like this:

Test MongoDB Authentication.

Conclusion

A well-known NoSQL database that offers high performance, high availability, and automatic scaling is MongoDB. It is different from the RDBMS such as MySQL, PostgreSQL, and SQLite as it does not use SQL to set and retrieve data. MongoDB stores data in `documents` called BSON (binary representation of JSON with additional types of information). MongoDB is only available for 64-bit Long Term Support Ubuntu Release.

Share this page:

35 Comment(s)

Add comment

Please register in our forum first to comment.

Comments

By: Mr. Geek

Seems to be interesting for the researching tasks of our institute! What about the stability of this release?

By: End User

Thank you for the instructions, worked like a charm - note in step-4 to reload it should be "systemctl daemon-reload" rather than "systemd daemon-reload" 

By: Jessiree De Vera

Thank you!

By: Antonio

best guide!

By: lumazi

I ran through the install but I am expernecing an issue at step #3. I run the createUser command but I receive the following error:

Error: couldn't add user: not authorized on admin to execute command

I am connected to the admin database but cannot execute the command. I do not see the --auth flag in the mongod.service file so I don't think I'm connecting with auth enabled.

By: Peter Carter

If anyone has problems getting systemd daemon-reload to work, you can probably use systemctl daemon-reload. Worked for me, anyway.

By: Oliver

Could you also make a HowToForge on how to compile the c++ driver easily? Today this is a mess and a pain. Thanks

By: swapnaja

Thank You for Instructions This is work for me.

By: raju prasad

thanks for this tutorial.

By: Guy Holtzman

Thanks,

I am getting Excess arguments as output just before step 5.

I continued and it seems to work, but I do not know what is the affect of this error and how to solve it, any ideas?

Guy

By: BrucklynBoy

Chapter 'Configure MongoDB username and password', 'Step 4 - Enable mongodb authentication':

Reload the systemd service says 'systemd daemon-reload', but should instead be 'systemctl daemon-reload'

By: matt

thanks!

By: invalid user

another method if error in : mongo -u admin -p admin123 --authenticationDatabase admin

with : mongo -u "admin" -p "admin123" --authenticationDatabase admin   

works to me on linux MINT 18 (sarah)

By: selva

This is work for me. thank you.

By: Chien

Thank for share!

By: Jonathan

In step 4 I had to use systemctl daemon-reload vice systemd daemon-reload.

By: Motiur Rehman

Thank You very much for your support through this tutorial(manual),

Awsome implementation and installation without any error, great and great job.

May Allah give you more knowledge to help us like this.

Thank You

By: freeelectron

sudo apt-get install -y mongodb-org did not work for me I had to do sudo apt-get install -y mongodb

By: Manoj

instead of  'systemd daemon-reload'

please use:

systemctl daemon-reload

By: CircuitBender

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

should be :db.createUser(user:"admin", pwd:"admin123", role:"root", db:"admin")

By: Nagy

Great Job... had problems before with installing mongo but this worked well.

By: alireza ahmadi from iram

hi

tanks for this tutorial

good luck

By: Reda

At step 4 you should replace systemd daemon-reload with : systemctl daemon-reload

The rest is great

 

Thanks by the way you saved me a lot of time :)

By: MK Shah

Its a great help to me. Thanks a lot for providing such polish steps.

By: Tim Schwab

Yes, I was confused for a bit, and I'm sure others will be too. This needs to be fixed.

By: Rahil

Thank you so much... 

By: Jeff D.

Thank you so much for this fantastic tutorial! I have spent the better part of today trying to remove single files because of another tutorial by Digital Ocean - which was not correct and only made swiss cheese of my root permissions. This guide actually worked for me!! I am now finally nested deep in instances running mongo, so I thank you!

By: Ron Barak

The systemctl commands should be preceded with sudo.

By: puneet

Thank you..

By: Guest

Brilliant and clear

By: Gruncle Seb

I followed this and it made my admin user have no rights. I'd recommend just following the docs

By: Stuki

Excellent article, I've managed to get mongo running by following the steps. One wierd thing is that the mongod.service couldn't start and was inactive (dead) unless I set Type=forking under [Service] in /lib/systemd/system/mongod.service

By: spydermard

hi

tanks for this tutorial

good luck

By: perkele

There is a an error in the manual.

Step 4:

systemd daemon-reload

should be

systemctl daemon-reload

Otherwise great step by step guide. Thanks. May the force be with you!

By: Muhammad

Hi, Thank you for this usefull tutorial :)

bests for you