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://keyserver.ubuntu.com:80 --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 http://downloads-distro.mongodb.org/repo/ubuntu-upstart 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://keyserver.ubuntu.com:80 --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://keyserver.ubuntu.com:80 --recv 7F0CEB10  
 gpg: requesting key 7F0CEB10 from hkp server keyserver.ubuntu.com  
 gpg: key 7F0CEB10: public key "Richard Kreuter <richard@10gen.com>" imported  
 gpg: Total number processed: 1  
 gpg:        imported: 1 (RSA: 1)  
 craig@U14BASE01:~$ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list  
 deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen  
 craig@U14BASE01:~$ sudo apt-get update  
 Ign http://downloads-distro.mongodb.org dist InRelease  
 Ign http://extras.ubuntu.com trusty InRelease                   
 Get:1 http://downloads-distro.mongodb.org dist Release.gpg [490 B]         
 Ign http://security.ubuntu.com trusty-security InRelease              
 Get:2 http://downloads-distro.mongodb.org dist Release [2,042 B]          
 Hit http://extras.ubuntu.com trusty Release.gpg                  
 Get:3 http://security.ubuntu.com trusty-security Release.gpg [933 B]        
 Get:4 http://security.ubuntu.com trusty-security Release [62.0 kB]      
 Hit http://extras.ubuntu.com trusty Release                    
 Get:5 http://downloads-distro.mongodb.org dist/10gen amd64 Packages [25.4 kB]   
 Ign http://us.archive.ubuntu.com trusty InRelease                 
 Hit http://extras.ubuntu.com trusty/main Sources                  
 Hit http://extras.ubuntu.com trusty/main amd64 Packages              
 Get:6 http://downloads-distro.mongodb.org dist/10gen i386 Packages [25.6 kB]    
 Ign http://us.archive.ubuntu.com trusty-updates InRelease             
 Hit http://extras.ubuntu.com trusty/main i386 Packages               
 Get:7 http://security.ubuntu.com trusty-security/main Sources [63.1 kB]      
 Ign http://us.archive.ubuntu.com trusty-backports InRelease            
 Get:8 http://security.ubuntu.com trusty-security/restricted Sources [2,061 B]   
 Get:9 http://security.ubuntu.com trusty-security/universe Sources [17.4 kB]    
 Hit http://us.archive.ubuntu.com trusty Release.gpg                
 Get:10 http://security.ubuntu.com trusty-security/multiverse Sources [716 B]    
 Get:11 http://security.ubuntu.com trusty-security/main amd64 Packages [196 kB]   
 Get:12 http://us.archive.ubuntu.com trusty-updates Release.gpg [933 B]       
 Ign http://extras.ubuntu.com trusty/main Translation-en_US             
 Get:13 http://us.archive.ubuntu.com trusty-backports Release.gpg [933 B]      
 Ign http://extras.ubuntu.com trusty/main Translation-en              
 Ign http://downloads-distro.mongodb.org dist/10gen Translation-en_US        
 Ign http://downloads-distro.mongodb.org dist/10gen Translation-en         
 Get:14 http://security.ubuntu.com trusty-security/restricted amd64 Packages [8,875 B]  
 Get:15 http://security.ubuntu.com trusty-security/universe amd64 Packages [84.2 kB]  
 Hit http://us.archive.ubuntu.com trusty Release      
 Get:16 http://security.ubuntu.com trusty-security/multiverse amd64 Packages [1,166 B]  
 Get:17 http://security.ubuntu.com trusty-security/main i386 Packages [187 kB]  
 Get:18 http://us.archive.ubuntu.com trusty-updates Release [62.0 kB]  
 Get:19 http://security.ubuntu.com trusty-security/restricted i386 Packages [8,846 B]  
 Get:20 http://security.ubuntu.com trusty-security/universe i386 Packages [84.2 kB]  
 Get:21 http://security.ubuntu.com trusty-security/multiverse i386 Packages [1,412 B]  
 Get:22 http://security.ubuntu.com trusty-security/main Translation-en [99.0 kB]  
 Hit http://security.ubuntu.com trusty-security/multiverse Translation-en      
 Hit http://security.ubuntu.com trusty-security/restricted Translation-en   
 Get:23 http://us.archive.ubuntu.com trusty-backports Release [62.0 kB]  
 Get:24 http://security.ubuntu.com trusty-security/universe Translation-en [46.1 kB]  
 Hit http://us.archive.ubuntu.com trusty/main Sources                
 Hit http://us.archive.ubuntu.com trusty/restricted Sources             
 Hit http://us.archive.ubuntu.com trusty/universe Sources              
 Hit http://us.archive.ubuntu.com trusty/multiverse Sources             
 Hit http://us.archive.ubuntu.com trusty/main amd64 Packages            
 Hit http://us.archive.ubuntu.com trusty/restricted amd64 Packages         
 Hit http://us.archive.ubuntu.com trusty/universe amd64 Packages          
 Hit http://us.archive.ubuntu.com trusty/multiverse amd64 Packages         
 Hit http://us.archive.ubuntu.com trusty/main i386 Packages             
 Hit http://us.archive.ubuntu.com trusty/restricted i386 Packages          
 Hit http://us.archive.ubuntu.com trusty/universe i386 Packages           
 Hit http://us.archive.ubuntu.com trusty/multiverse i386 Packages          
 Hit http://us.archive.ubuntu.com trusty/main Translation-en            
 Hit http://us.archive.ubuntu.com trusty/multiverse Translation-en         
 Hit http://us.archive.ubuntu.com trusty/restricted Translation-en         
 Hit http://us.archive.ubuntu.com trusty/universe Translation-en          
 Get:25 http://us.archive.ubuntu.com trusty-updates/main Sources [155 kB]      
 Get:26 http://us.archive.ubuntu.com trusty-updates/restricted Sources [2,061 B]  
 Get:27 http://us.archive.ubuntu.com trusty-updates/universe Sources [97.6 kB]   
 Get:28 http://us.archive.ubuntu.com trusty-updates/multiverse Sources [3,550 B]  
 Get:29 http://us.archive.ubuntu.com trusty-updates/main amd64 Packages [398 kB]  
 Get:30 http://us.archive.ubuntu.com trusty-updates/restricted amd64 Packages [8,875 B]  
 Get:31 http://us.archive.ubuntu.com trusty-updates/universe amd64 Packages [239 kB]  
 Get:32 http://us.archive.ubuntu.com trusty-updates/multiverse amd64 Packages [9,373 B]  
 Get:33 http://us.archive.ubuntu.com trusty-updates/main i386 Packages [391 kB]   
 Get:34 http://us.archive.ubuntu.com trusty-updates/restricted i386 Packages [8,846 B]  
 Get:35 http://us.archive.ubuntu.com trusty-updates/universe i386 Packages [240 kB]  
 Get:36 http://us.archive.ubuntu.com trusty-updates/multiverse i386 Packages [9,567 B]  
 Get:37 http://us.archive.ubuntu.com trusty-updates/main Translation-en [188 kB]  
 Hit http://us.archive.ubuntu.com trusty-updates/multiverse Translation-en     
 Hit http://us.archive.ubuntu.com trusty-updates/restricted Translation-en     
 Get:38 http://us.archive.ubuntu.com trusty-updates/universe Translation-en [123 kB]  
 Get:39 http://us.archive.ubuntu.com trusty-backports/main Sources [4,451 B]    
 Get:40 http://us.archive.ubuntu.com trusty-backports/restricted Sources [28 B]   
 Get:41 http://us.archive.ubuntu.com trusty-backports/universe Sources [19.6 kB]  
 Get:42 http://us.archive.ubuntu.com trusty-backports/multiverse Sources [1,898 B]  
 Get:43 http://us.archive.ubuntu.com trusty-backports/main amd64 Packages [5,165 B]  
 Get:44 http://us.archive.ubuntu.com trusty-backports/restricted amd64 Packages [28 B]  
 Get:45 http://us.archive.ubuntu.com trusty-backports/universe amd64 Packages [23.9 kB]  
 Get:46 http://us.archive.ubuntu.com trusty-backports/multiverse amd64 Packages [1,245 B]  
 Get:47 http://us.archive.ubuntu.com trusty-backports/main i386 Packages [5,176 B]  
 Get:48 http://us.archive.ubuntu.com trusty-backports/restricted i386 Packages [28 B]  
 Get:49 http://us.archive.ubuntu.com trusty-backports/universe i386 Packages [24.0 kB]  
 Get:50 http://us.archive.ubuntu.com trusty-backports/multiverse i386 Packages [1,249 B]  
 Hit http://us.archive.ubuntu.com trusty-backports/main Translation-en       
 Hit http://us.archive.ubuntu.com trusty-backports/multiverse Translation-en    
 Hit http://us.archive.ubuntu.com trusty-backports/restricted Translation-en    
 Get:51 http://us.archive.ubuntu.com trusty-backports/universe Translation-en [21.8 kB]  
 Ign http://us.archive.ubuntu.com trusty/main Translation-en_US           
 Ign http://us.archive.ubuntu.com trusty/multiverse Translation-en_US        
 Ign http://us.archive.ubuntu.com trusty/restricted Translation-en_US        
 Ign http://us.archive.ubuntu.com 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  
  mongodb-org-tools  
 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 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ dist/10gen mongodb-org-shell amd64 2.6.7 [4,457 kB]  
 Get:2 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ dist/10gen mongodb-org-server amd64 2.6.7 [9,484 kB]  
 Get:3 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ dist/10gen mongodb-org-mongos amd64 2.6.7 [7,176 kB]  
 Get:4 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ dist/10gen mongodb-org-tools amd64 2.6.7 [94.0 MB]  
 Get:5 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ 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 (2.6.7.1-1) ...  
 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) ...  
 Done.  
 Adding user `mongodb' to group `mongodb' ...  
 Adding user mongodb to group mongodb  
 Done.  
 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) ...  
 craig@U14BASE01:~$   



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 build10.nj1.10gen.cc 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: "127.0.0.1" }, 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
/etc/mongodb.conf
A couple of key items for immediate configuration are


1. Storing Data and Logging


The data is stored in this location (by default):
dbpath=/var/lib/mongodb
and the logs are written to this path (by default):
logpath=/var/log/mongodb/mongod.log


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  
 > db.test.save({ 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://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart 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



Docker


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



Troubleshooting

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



References

  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. [MongoDB.org] Official Documentation

4 comments:

  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

    ReplyDelete
  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: https://cognegicsystems.com/
    Contact Number: 1-800-450-8670
    Email Address- info@cognegicsystems.com
    Company’s Address- 507 Copper Square Drive Bethel Connecticut (USA) 06801

    ReplyDelete
  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: https://cognegicsystems.com/
    Contact Number: 1-800-450-8670
    Email Address- info@cognegicsystems.com
    Company’s Address- 507 Copper Square Drive Bethel Connecticut (USA) 06801

    ReplyDelete