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 :
Installing MongoDB
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
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
Installing MongoDB From the Stable Repository
This section will describe how-to install MongoDB from the stable repository. First of all, let’s add the MongoDB stable repository :
$ echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/stable multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-stable.list
Reload the local package database so apt can use the MongoDB repository.
$ sudo apt-get update
You can see the list of available Versions of MongoDB on the stable repository by running the command below:
$ sudo apt-cache show mongodb-org | grep Version
Version: 3.2.0
Version: 3.2.0~rc6~6~gfbb8433
Version: 3.2.0~rc5~13~g45d9477
Version: 3.2.0~rc3~121~g3b3ef42
Version: 3.2.0~rc3~110~g327660f
Version: 3.2.0~rc3~75~g5b3257d
Version: 3.2.0~rc3~64~gc76b4ff
Version: 3.2.0~rc3~57~g078ca78
Version: 3.2.0~rc3~36~g5d22dc0
Version: 3.2.0~rc3~31~g0d12c96
Version: 3.2.0~rc2~211~gbd58ea2
Version: 3.2.0~rc2~209~ga17839c
Version: 3.2.0~rc2~204~g4d9c15d
Version: 3.2.0~rc2~174~gd7511a9
Version: 3.2.0~rc2~143~gdeaf4af
Version: 3.2.0~rc2~108~g80dfa9f
Version: 3.2.0~rc2~72~ga2ab950
Version: 3.2.0~rc2~60~g6543f0d
Version: 3.2.0~rc2~52~g36d484d
Version: 3.2.0~rc2~35~gd70512a
Version: 3.2.0~rc1~79~g42d0577
Version: 3.0.7
Version: 3.0.6
Version: 3.0.5
Version: 3.0.4
Version: 3.0.3
Version: 3.0.2
Version: 3.0.1
Version: 3.0.0
Now we are ready to install MongoDB. To install the latest stable release of MongoDB you can use the command below.
$ sudo apt-get install mongodb-org
If you want to install a specific version of MongoDB you need to a specify version for all packages and not only mongodb-org
version.
If you only specify mongodb-org version it will install the latest version of each MongoDB package.
To install version 3.2 of MongoDB you can run the command below :
$ sudo apt-get install -y mongodb-org=3.2.0 mongodb-org-server=3.2.0 mongodb-org-shell=3.2.0 mongodb-org-mongos=3.2.0 mongodb-org-tools=3.2.0
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:
$ sudo apt-cache show mongodb-org | grep Version
Version: 3.2.0
Version: 3.2.0~rc6~6~gfbb8433
Version: 3.2.0~rc5~13~g45d9477
Version: 3.2.0~rc3~121~g3b3ef42
Version: 3.2.0~rc3~110~g327660f
Version: 3.2.0~rc3~75~g5b3257d
Version: 3.2.0~rc3~64~gc76b4ff
Version: 3.2.0~rc3~57~g078ca78
Version: 3.2.0~rc3~36~g5d22dc0
Version: 3.2.0~rc3~31~g0d12c96
Version: 3.2.0~rc2~211~gbd58ea2
Version: 3.2.0~rc2~209~ga17839c
Version: 3.2.0~rc2~204~g4d9c15d
Version: 3.2.0~rc2~174~gd7511a9
Version: 3.2.0~rc2~143~gdeaf4af
Version: 3.2.0~rc2~108~g80dfa9f
Version: 3.2.0~rc2~72~ga2ab950
Version: 3.2.0~rc2~60~g6543f0d
Version: 3.2.0~rc2~52~g36d484d
Version: 3.2.0~rc2~35~gd70512a
Version: 3.2.0~rc1~79~g42d0577
Now we are ready to install MongoDB. To install the latest stable release of MongoDB you can use command below.
$ sudo apt-get install mongodb-org
Pinning the MongoDB Version
You might want to lock or ‘pin’ the MongoDB version installed on your server. After installation you can run the command below to pin your MongoDB version :
$ echo "mongodb-org hold" | sudo dpkg --set-selections
$ echo "mongodb-org-server hold" | sudo dpkg --set-selections
$ echo "mongodb-org-shell hold" | sudo dpkg --set-selections
$ echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
$ echo "mongodb-org-tools hold" | sudo dpkg --set-selections
Checking the MongoDB Service
To check the MongoDB service you can run command below :
$ sudo service mongod status
mongod start/running, process 14089
The output from the command above shows that mongod is running. We can also check where mongod
listens by running the command below :
$ sudo netstat -naptu | grep LISTEN | grep mongo
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 14089/mongod
We can see above that mongod
is listening on port 27017, this is the default port, on 127.0.0.1
or localhost
.
Configuring MongoDB
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 :
bindIp: 127.0.0.1
change it to
bindIp: 0.0.0.0
then restart the mongod service
$ sudo service mongod restart
Let’s check the mongod process port listening with the command below
$ sudo netstat -naptu | grep LISTEN | grep mongo
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 26320/mongod
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 :
$ mongo
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 :
LC_ALL="en_US.utf8"
Now if you run mongo
shell again you will get a MongoDB shell :
$ mongo
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.
Let’s download the data sample first
$ wget -c https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/dataset.json
And now import it
$ mongoimport --host localhost --port 27017 --db hostpresto --collection restaurants --drop --file dataset.json
2015-12-13T04:55:13.792-0500 connected to: localhost:27017
2015-12-13T04:55:13.793-0500 dropping: hostpresto.restaurants
2015-12-13T04:55:15.803-0500 imported 25359 documents
Querying MongoDB
After importing the sample data let’s query our MongoDB database. We’ll only cover a basic query of the data. First let’s open the MongoDB Shell:
$ mongo
Use the database that we created on the previous section, hostpresto
and query all entries on database :
> use hostpresto
> db.restaurants.find()
To find a restaurant with name “The Movable Feast” we can run command below :
> db.restaurants.find( {"name":"The Movable Feast"})
{ "_id" : ObjectId("566d4082fdd9cac12f32d65d"), "address" : { "building" : "284", "coord" : [ -73.9829239, 40.6580753 ], "street" : "Prospect Park West", "zipcode" : "11215" }, "borough" : "Brooklyn", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-11-19T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2013-11-14T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2012-12-05T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-05-17T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "The Movable Feast", "restaurant_id" : "40361606" }
Uninstall 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.
$ sudo rm -r /var/log/mongodb
$ sudo rm -r /var/lib/mongodb
Summary
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!