There is a new revision of this tutorial available for Ubuntu 16.04 (Xenial Xerus).

How to Install and Configure MongoDB on Ubuntu 14.04

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. There are Ubuntu packages for MongoDB available for LTS releases only.


  • Ubuntu Server 14.04 - 64 bit
  • Root privileges

What we will do in this tutorial:

  1. Install MongoDB
  2. Configure MongoDB
  3. Conclusion

Install MongoDB in Ubuntu

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:// --recv 7F0CEB10

Step 2 - Create source list file MongoDB

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

echo "deb "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.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

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 mongodb shell on your server. You can login by typing:


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

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.

Set the admin password in MongoDB.

Now type exit to exit from MongoDB shell.

Step 4 - Edit MongoDB configuration file

Edit the MongoDB configuration file /etc/mongod.conf with an editor.

nano /etc/mongod.conf

Uncomment the line #auth = true by removing the # character, then save and exit.

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:

Login to MongoDB.


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:

4 Comment(s)

Add comment


From: ralphie

I followed your procedure above but except the 

 `Uncomment the line #auth = true by removing the # character, then save and exit.`

since I cannot find this line  "#auth = true"

but i have this error after i try to connect 

MongoDB shell version: 3.0.7

connecting to: test

2015-11-24T01:02:17.177+0800 W NETWORK  Failed to connect to, reason: errno:111 Connection refused

2015-11-24T01:02:17.179+0800 E QUERY    Error: couldn't connect to server (, connection attempt failed

    at connect (src/mongo/shell/mongo.js:179:14)

    at (connect):1:6 at src/mongo/shell/mongo.js:179

exception: connect failed


From: mongonoob

Starting a shell, attempting to connect to a non-running database (connection refused) Starting the database, seeing 0 connections in the logs Stopping the database (Ctrl-C or similar), starting a shell, fails to connect again because database has been shut down Starting the database, seeing 0 connections in logs repeat

From: Amit Gunjal

Thank you very very much!

From: sam


I installed clients tools on the application server and tried to connec to database mongodb on another server and I have all time connection refused and when I saw the mongodb errorlog there is no error. I think I am missing something. Could you provide me with the rigth mongo syntax to connect