MongoDB (from Humongous) is an open source document based database that provides high performance, high availability and automatic scaling.
Several key features of MongoDB include :
High performance data persistence, in particular support for embedded data models that reduces I/O activity on database.
High Availability with replication facility that provides automatic failover and data redundancy.
Automatic Scaling by providing automatic sharding which distributes data accross a cluster of machines. The replica sets can provide eventually consistent reads with low-latency high troughput.
As an additional introduction about MongoDB you can watch "What is MongoDB" Video below :
MongoDB only provides packages for the 64-bit Long Term Support (LTS) version of Ubuntu. Currently the packages available for Ubuntu 12.04 (Precise Pangolin) and Ubuntu 14.04 (Trusty Tahr). In this tutorial we'll learn how-to install MongoDB on Ubuntu 14.04.
Before we start the MongoDB installation, you need to know that MongoDB repository contains the following packages :
* mongodb-org. This is a metapackage that will install all packages below.
* mongodb-org-server. This package will install MongoDB server (mongod daemon), configurations and init scripts.
* mongodb-org-mongos. This package contains mongos daemon.
* mongodb-org-shell. This package contains mongo shell.
* mongodb-org-tools. This package contains the following MongoDB tools: mongoimport, bsondump, mongodump, mongoexport, mongofiles, mongooplog, mongoperf, mongorestore, mongostat, and mongotop.
There are two ways to install MongoDB from the repository. The first one is using the stable release repository, the second is to use a specific version repository.
Using the first method you can get the latest stable version of MongoDB but using the second method you can make sure your version of MongoDB will never get upgraded to a new version, which is important for Porduction system.
In this tutorial we'll learn installing MongoDB using both methods, using the stable repository channel and using a specific version. Let's install MongoDB.
First of all let's add a MongoDB Public key used by package management
Installing MongoDB From a Specific Version Repository
If you already followed the section above you don't need to follow this section. This section will describe how-to install MongoDB from a specific version repository. We will use the repository for version 3.2 in this tutorial. First of all, let's add MongoDB 3.2 repository.
$ echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
Reload the local package database so apt can use the MongoDB repository.
$ sudo apt-get update
You can see list of available versions from the MongoDB 3.2 repository by running the command below:
We can see above that mongod is listening on port 27017, this is the default port, on 127.0.0.1 or localhost.
MongoDB's configuration file is located at /etc/mongod.conf. From the previous section we learned that mongod only listens on localhost. In this section we'll configure MongoDB to listen to all network interfaces.
Please be warned that this will make your system less secure since your MongoDB installation is exposed to the network. Change this setting carefully.
Open /etc/mongod.conf. Find line below :
change it to
then restart the mongod service
$ sudo service mongod restart
Let's check the mongod process port listening with the command below
We can see above that MongoDB now listens to all interfaces so you can access MongoDB from another server, like your application server for example.
Using Mongo Shell
Your MongoDB installation is ready. Let's use the MongoDB installation using mongo shell. You can use the command below to use mongo shell :
If you are using a VPS, virtual machine or cloud server you might get the error message below.
Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly.
The error message above is because one of the LC_* variables or LC_ALL variable is not set correctly. To fix this error, you can run this command :
$ export LC_ALL="en_US.utf8"
That is English US with utf8. If you use another language change the value accordingly.
To make this change permanent, open /etc/environment and add the line below :
Now if you run mongo shell again you will get a MongoDB shell :
MongoDB shell version: 3.2.0
connecting to: test
Server has startup warnings:
2015-12-12T20:40:45.117-0500 I CONTROL [initandlisten]
2015-12-12T20:40:45.117-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-12-12T20:40:45.117-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-12-12T20:40:45.118-0500 I CONTROL [initandlisten]
2015-12-12T20:40:45.118-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-12-12T20:40:45.118-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-12-12T20:40:45.118-0500 I CONTROL [initandlisten]
Press CTRL+C or type exit to exit from MongoDB shell.
MongoDB shell above shows a warning related to Transparent Huge Pages (THP). We can ignore this warning for now. You can read about Disable Transparent Hugh Pages (THP) on MongoDB documentation.
Import Data to MongoDB
We will learn how-to import data to MongoDB using a data sample provided by MongoDB.
Just in case for some reason you need to uninstall MongoDB, you can follow the guide in this section.
First, let's stop mongod service
$ sudo service mongod stop
Remove all mongod packages
$ sudo apt-get purge mongodb-org*
If you already have data on MongoDB you might want to skip this step. But if you don't have any data on MongoDB and you want to cleanly uninstall MongoDB you need to remove MongoDB log and data directory.
In this tutorial we learned how-to install MongoDB on Ubuntu 14.04 (Trusty Tahr) from the MongoDB repository, we saw that we can use the latest version of MongoDB instead of using an old version provided by the Ubuntu repository.
We also learned how-to configure the MongoDB port, using MongoDB Shell and importing data to MongoDB. Have Fun with MongoDB!
Looking for the right web hosting solution for your website can be an intimidating task. Options nowadays are more diversified than ever, and each year brings new developments in the web hosting market. If you have no clue of what to look out for, you can find yourself overwhelmed with...
Content marketing continues to be one of the most valuable tools for today’s online businesses. With content, you can improve your chances of reaching your target audience, boost your search engine standing, and even unlock new opportunities for sales. The more content you produce, the more you can strengthen your...
User Experience (UX) is one of the most crucial factors to consider in web design. As the number of websites and applications in the world today continues to accelerate, businesses are under more pressure than ever to impress customers straight away. If a user visits your website and finds slow-loading...