Saturday, January 10, 2015

Installing MongoDB on Ubuntu 14.04

jira-17742: MongoDB is not supported on Ubuntu 15.04.
Support will likely be re-added for Ubuntu 16.04.  See the troubleshooting section for help on getting MongoDB working on 15.04.

Installation Steps

Installation of MongoDB on Ubuntu 14.04 is simple.

The installation can typically be handled in four steps:
  1. Import the public key used by the package management system
    1. sudo apt-key adv --keyserver hkp:// --recv 7F0CEB10
      The Ubuntu Package Manager will use this key to verify the integrity of the packages being downloaded.
  2. Create a list file for MongoDB
    1. echo 'deb dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
  3. Reload local package database
    1. sudo apt-get update
  4. Install the MongoDB packages
    1. sudo apt-get install mongodb-org
      Installs the latest (stable) MongoDB version all the necessary packages on the local system

Installation Output

Successful installation output for me looks like:
 craig@U14BASE01:~$ sudo apt-key adv --keyserver hkp:// --recv 7F0CEB10  
 [sudo] password for craig:   
 Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --homedir /tmp/tmp.ZcGplsOCKP --no-auto-check-trustdb --trust-model always --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver hkp:// --recv 7F0CEB10  
 gpg: requesting key 7F0CEB10 from hkp server  
 gpg: key 7F0CEB10: public key "Richard Kreuter <>" imported  
 gpg: Total number processed: 1  
 gpg:        imported: 1 (RSA: 1)  
 craig@U14BASE01:~$ echo 'deb dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list  
 deb dist 10gen  
 craig@U14BASE01:~$ sudo apt-get update  
 Ign dist InRelease  
 Ign trusty InRelease                   
 Get:1 dist Release.gpg [490 B]         
 Ign trusty-security InRelease              
 Get:2 dist Release [2,042 B]          
 Hit trusty Release.gpg                  
 Get:3 trusty-security Release.gpg [933 B]        
 Get:4 trusty-security Release [62.0 kB]      
 Hit trusty Release                    
 Get:5 dist/10gen amd64 Packages [25.4 kB]   
 Ign trusty InRelease                 
 Hit trusty/main Sources                  
 Hit trusty/main amd64 Packages              
 Get:6 dist/10gen i386 Packages [25.6 kB]    
 Ign trusty-updates InRelease             
 Hit trusty/main i386 Packages               
 Get:7 trusty-security/main Sources [63.1 kB]      
 Ign trusty-backports InRelease            
 Get:8 trusty-security/restricted Sources [2,061 B]   
 Get:9 trusty-security/universe Sources [17.4 kB]    
 Hit trusty Release.gpg                
 Get:10 trusty-security/multiverse Sources [716 B]    
 Get:11 trusty-security/main amd64 Packages [196 kB]   
 Get:12 trusty-updates Release.gpg [933 B]       
 Ign trusty/main Translation-en_US             
 Get:13 trusty-backports Release.gpg [933 B]      
 Ign trusty/main Translation-en              
 Ign dist/10gen Translation-en_US        
 Ign dist/10gen Translation-en         
 Get:14 trusty-security/restricted amd64 Packages [8,875 B]  
 Get:15 trusty-security/universe amd64 Packages [84.2 kB]  
 Hit trusty Release      
 Get:16 trusty-security/multiverse amd64 Packages [1,166 B]  
 Get:17 trusty-security/main i386 Packages [187 kB]  
 Get:18 trusty-updates Release [62.0 kB]  
 Get:19 trusty-security/restricted i386 Packages [8,846 B]  
 Get:20 trusty-security/universe i386 Packages [84.2 kB]  
 Get:21 trusty-security/multiverse i386 Packages [1,412 B]  
 Get:22 trusty-security/main Translation-en [99.0 kB]  
 Hit trusty-security/multiverse Translation-en      
 Hit trusty-security/restricted Translation-en   
 Get:23 trusty-backports Release [62.0 kB]  
 Get:24 trusty-security/universe Translation-en [46.1 kB]  
 Hit trusty/main Sources                
 Hit trusty/restricted Sources             
 Hit trusty/universe Sources              
 Hit trusty/multiverse Sources             
 Hit trusty/main amd64 Packages            
 Hit trusty/restricted amd64 Packages         
 Hit trusty/universe amd64 Packages          
 Hit trusty/multiverse amd64 Packages         
 Hit trusty/main i386 Packages             
 Hit trusty/restricted i386 Packages          
 Hit trusty/universe i386 Packages           
 Hit trusty/multiverse i386 Packages          
 Hit trusty/main Translation-en            
 Hit trusty/multiverse Translation-en         
 Hit trusty/restricted Translation-en         
 Hit trusty/universe Translation-en          
 Get:25 trusty-updates/main Sources [155 kB]      
 Get:26 trusty-updates/restricted Sources [2,061 B]  
 Get:27 trusty-updates/universe Sources [97.6 kB]   
 Get:28 trusty-updates/multiverse Sources [3,550 B]  
 Get:29 trusty-updates/main amd64 Packages [398 kB]  
 Get:30 trusty-updates/restricted amd64 Packages [8,875 B]  
 Get:31 trusty-updates/universe amd64 Packages [239 kB]  
 Get:32 trusty-updates/multiverse amd64 Packages [9,373 B]  
 Get:33 trusty-updates/main i386 Packages [391 kB]   
 Get:34 trusty-updates/restricted i386 Packages [8,846 B]  
 Get:35 trusty-updates/universe i386 Packages [240 kB]  
 Get:36 trusty-updates/multiverse i386 Packages [9,567 B]  
 Get:37 trusty-updates/main Translation-en [188 kB]  
 Hit trusty-updates/multiverse Translation-en     
 Hit trusty-updates/restricted Translation-en     
 Get:38 trusty-updates/universe Translation-en [123 kB]  
 Get:39 trusty-backports/main Sources [4,451 B]    
 Get:40 trusty-backports/restricted Sources [28 B]   
 Get:41 trusty-backports/universe Sources [19.6 kB]  
 Get:42 trusty-backports/multiverse Sources [1,898 B]  
 Get:43 trusty-backports/main amd64 Packages [5,165 B]  
 Get:44 trusty-backports/restricted amd64 Packages [28 B]  
 Get:45 trusty-backports/universe amd64 Packages [23.9 kB]  
 Get:46 trusty-backports/multiverse amd64 Packages [1,245 B]  
 Get:47 trusty-backports/main i386 Packages [5,176 B]  
 Get:48 trusty-backports/restricted i386 Packages [28 B]  
 Get:49 trusty-backports/universe i386 Packages [24.0 kB]  
 Get:50 trusty-backports/multiverse i386 Packages [1,249 B]  
 Hit trusty-backports/main Translation-en       
 Hit trusty-backports/multiverse Translation-en    
 Hit trusty-backports/restricted Translation-en    
 Get:51 trusty-backports/universe Translation-en [21.8 kB]  
 Ign trusty/main Translation-en_US           
 Ign trusty/multiverse Translation-en_US        
 Ign trusty/restricted Translation-en_US        
 Ign trusty/universe Translation-en_US         
 Fetched 3,024 kB in 37s (80.1 kB/s)                        
 Reading package lists... Done  
 craig@U14BASE01:~$ sudo apt-get install mongodb-org  
 Reading package lists... Done  
 Building dependency tree      
 Reading state information... Done  
 The following extra packages will be installed:  
  mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools  
 The following NEW packages will be installed:  
  mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell  
 0 upgraded, 5 newly installed, 0 to remove and 261 not upgraded.  
 Need to get 115 MB of archives.  
 After this operation, 291 MB of additional disk space will be used.  
 Do you want to continue? [Y/n] Y  
 Get:1 dist/10gen mongodb-org-shell amd64 2.6.7 [4,457 kB]  
 Get:2 dist/10gen mongodb-org-server amd64 2.6.7 [9,484 kB]  
 Get:3 dist/10gen mongodb-org-mongos amd64 2.6.7 [7,176 kB]  
 Get:4 dist/10gen mongodb-org-tools amd64 2.6.7 [94.0 MB]  
 Get:5 dist/10gen mongodb-org amd64 2.6.7 [3,656 B]  
 Fetched 115 MB in 32s (3,592 kB/s)                         
 Selecting previously unselected package mongodb-org-shell.  
 (Reading database ... 168329 files and directories currently installed.)  
 Preparing to unpack .../mongodb-org-shell_2.6.7_amd64.deb ...  
 Unpacking mongodb-org-shell (2.6.7) ...  
 Selecting previously unselected package mongodb-org-server.  
 Preparing to unpack .../mongodb-org-server_2.6.7_amd64.deb ...  
 Unpacking mongodb-org-server (2.6.7) ...  
 Selecting previously unselected package mongodb-org-mongos.  
 Preparing to unpack .../mongodb-org-mongos_2.6.7_amd64.deb ...  
 Unpacking mongodb-org-mongos (2.6.7) ...  
 Selecting previously unselected package mongodb-org-tools.  
 Preparing to unpack .../mongodb-org-tools_2.6.7_amd64.deb ...  
 Unpacking mongodb-org-tools (2.6.7) ...  
 Selecting previously unselected package mongodb-org.  
 Preparing to unpack .../mongodb-org_2.6.7_amd64.deb ...  
 Unpacking mongodb-org (2.6.7) ...  
 Processing triggers for man-db ( ...  
 Processing triggers for ureadahead (0.100.0-16) ...  
 ureadahead will be reprofiled on next reboot  
 Setting up mongodb-org-shell (2.6.7) ...  
 Setting up mongodb-org-server (2.6.7) ...  
 Adding system user `mongodb' (UID 117) ...  
 Adding new user `mongodb' (UID 117) with group `nogroup' ...  
 Not creating home directory `/home/mongodb'.  
 Adding group `mongodb' (GID 126) ...  
 Adding user `mongodb' to group `mongodb' ...  
 Adding user mongodb to group mongodb  
 mongod start/running, process 3182  
 Setting up mongodb-org-mongos (2.6.7) ...  
 Setting up mongodb-org-tools (2.6.7) ...  
 Processing triggers for ureadahead (0.100.0-16) ...  
 Setting up mongodb-org (2.6.7) ...  

Installation Verification

  1. Verify the Service is Running
    1. ps -ef | grep mongo
    2. Verifies that the MongoDB instance is running:
    3.  craig@U14BASE01:~$ ps -ef | grep mongo  
       mongodb  3474   1 2 20:49 ?    00:00:11 /usr/bin/mongod --config /etc/mongod.conf  
       craig   3727 2395 0 20:55 pts/11  00:00:00 grep --color=auto mongo   

  2. Check the Log File
    1. Check the log file for the line "waiting for connections on port 27017":
    2. cat /var/log/mongodb/mongod.log
    3.  2015-01-10T21:16:22.453-0800 ***** SERVER RESTARTED *****  
       2015-01-10T21:16:22.460-0800 [initandlisten] MongoDB starting : pid=3838 port=27017 dbpath=/var/lib/mongodb 64-bit host=U14BASE01  
       2015-01-10T21:16:22.460-0800 [initandlisten] db version v2.6.6  
       2015-01-10T21:16:22.460-0800 [initandlisten] git version: 608e8bc319627693b04cc7da29ecc300a5f45a1f  
       2015-01-10T21:16:22.460-0800 [initandlisten] build info: Linux 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49  
       2015-01-10T21:16:22.460-0800 [initandlisten] allocator: tcmalloc  
       2015-01-10T21:16:22.461-0800 [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "" }, storage: { dbPath: "/var/lib/mongodb" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }  
       2015-01-10T21:16:22.478-0800 [initandlisten] journal dir=/var/lib/mongodb/journal  
       2015-01-10T21:16:22.479-0800 [initandlisten] recover : no journal files present, no recovery needed  
       2015-01-10T21:16:22.495-0800 [initandlisten] waiting for connections on port 27017  
       2015-01-10T21:17:22.499-0800 [clientcursormon] mem (MB) res:37 virt:341  
       2015-01-10T21:17:22.499-0800 [clientcursormon] mapped (incl journal view):160  
       2015-01-10T21:17:22.499-0800 [clientcursormon] connections:0  
       2015-01-10T21:22:22.548-0800 [clientcursormon] mem (MB) res:37 virt:341  
       2015-01-10T21:22:22.548-0800 [clientcursormon] mapped (incl journal view):160  
       2015-01-10T21:22:22.548-0800 [clientcursormon] connections:0  

  3. Check the Version
    1. mongo --version
    2. Checks the installed MongoDB version:
    3.  craig@U14BASE01:~$ mongo --version  
       MongoDB shell version: 2.6.6  

Basic MongoDB Commands

  1. Starting the service
    1. sudo service mongod start
    2.  craig@U14BASE01:~$ sudo service mongod start  
       mongod start/running, process 3807   

  2. Stopping the service
    1. sudo service mongod stop
    2.  craig@U14BASE01:~$ sudo service mongod stop  
       mongod stop/waiting   

  3. Restarting the service
    1. sudo service mongod restart
    2.  craig@U14BASE01:~$ sudo service mongod restart  
       mongod stop/waiting  
       mongod start/running, process 3838  

Installation Customization

The MongoDB configuration file is typically located here
A couple of key items for immediate configuration are

1. Storing Data and Logging

The data is stored in this location (by default):
and the logs are written to this path (by default):

2. Enabling MongoDB for remote access

Open the Configuration File
sudo gedit /etc/mongod.conf
and comment out the "bind_ip" line.

Restart the MongoDB service using this command:
sudo service mongod restart

My configuration file looks like this:

and using this approach I was able to successfully connect to a MongoDB database on Ubuntu from a RoboMongo installation on Windows 8:

Writing and Retrieving a Simple Document

Another way to test the MongoDB installation is by the insertion and retrieval of a simple JSON document.

On Ubuntu, the Mongo command is installed on the PATH, so I can simply type
$ mongo
from the terminal.

I'm going to use a sample database included with the installation, called "mydb":
 $ mongo  
 MongoDB shell version: 2.6.6  
 connecting to: test  
 > use mydb  
 switched to db mydb  
 >{ a : "Hello, World!" })  
 WriteResult({ "nInserted" : 1 })  
 > db.test.find()  
 { "_id" : ObjectId("54b55931d9249e3df3a97750"), "a" : "Hello, World!" }  

In the terminal output above, I demonstrate the successful saving and retrieving of a document.

The log file (located at /var/log/mongodb/mongod.log) contains a record of this transaction:
 2015-01-13T09:43:13.670-0800 [conn1] insert mydb.test query: { a: "Hello, World!", _id: ObjectId('54b55931d9249e3df3a97750') } ninserted:1 keyUpdates:0 numYields:0 locks(micros) w:164624 164ms  
 2015-01-13T09:43:13.670-0800 [conn1] command mydb.$cmd command: insert { insert: "test", documents: [ { a: "Hello, World!", _id: ObjectId('54b55931d9249e3df  

Installation Script

The latest version of this script is on Github:
sudo apt-key adv --keyserver hkp:// --recv 7F0CEB10
echo 'deb dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update
sudo apt-get install mongodb-org -y
cat /etc/mongod.conf | sed s/bind_ip/#bind_ip/ > ~/mongod.conf
sudo mv ~/mongod.conf /etc/mongod.conf 
sudo mkdir -p /data/db
sudo service mongod restart


Running MongoDB via Docker is extremely simple:
$ sudo docker run --name $NAME -p $PORT:27017 -v $DATA:/data/db mongo

so for example:
$ sudo docker run --name mongodb-p 17017:27017 -v /home/craig/dat:/data/db mongo


  1. Ubuntu 15.04
    [AskUbuntu] MongoDB 2.6 does not start


  1. [GitHub] An installation script for MongoDB (for Debian/Ubuntu)
  2. How To Install MongoDB on Ubuntu 12.04
    1. A reliable set of instructions for installing MongoDB on an older distribution of Ubuntu.
  3. How to Install MongoDB on Ubuntu 14.04
    1. An essential source for my first time installing MongoDB on Ubuntu 14.04.
  4. [] Official Documentation


  1. I like the article on the topic due to this reason it is seen that other readers are showing their interest by commenting on it.

    MongoDB Training Centers in Chenai

  2. Not Able to Troubleshoot MongoDB Installation Issue? Contact to DB Installation & Configuration Support
    Sometime when users install MongoDB they found installation issue when they run the command in CMD “Connection failed”. Actually this error occurs only when you cannot find the directory like “C:\data\db” and also check which version of Windows you are using. By this way you can easily solve your problem but if not then contact to Cognegic’s DB Installation Support or DB Configuration Support for quick and reliable solution.
    For More Info:
    Contact Number: 1-800-450-8670
    Email Address-
    Company’s Address- 507 Copper Square Drive Bethel Connecticut (USA) 06801

  3. Unable to Install the MongoDB on Ubuntu 16.10? Contact to DB Installation Support
    In the event that you need effectively establishment of MongoDB on Ubuntu 16.10 then you are at right stage. Indeed! We at Cognegic give DB Configuration Support or legitimate Database Management Services for those clients whose are not ready to introduce the MongoDB on Ubuntu 16.10. Our engineers are mastery in establishment and design of any database like: MySQL, MS SQL Server, Cassandra et cetera.
    For More Info:
    Contact Number: 1-800-450-8670
    Email Address-
    Company’s Address- 507 Copper Square Drive Bethel Connecticut (USA) 06801