Installing MongoDB on Linux

MongoDB installation on Linux (Ubuntu 14.04 LTS here) consists of running the following commands:

  • sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 7F0CEB10   (Installing GPG Key)

Executing: gpg –ignore-time-conflict –no-options –no-default-keyring –homedir /tmp/tmp.KLub4f6ZtK –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: “Richard Kreuter <richard@10gen.com>” not changed
gpg: Total number processed: 1
gpg:              unchanged: 1

  • echo ‘deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen’ | sudo tee /etc/apt/sources.list.d/mongodb.list

rahulsaini@rahulsaini-UBUNTU:~$ 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
rahulsaini@rahulsaini-UBUNTU:~$

  • sudo apt-get update

This sudo apt-get update command will update the package lists

To install a particular MongoDB version (here it is 2.6.1) run the command below

  • sudo apt-get install -y mongodb-org=2.6.1 mongodb-org-server=2.6.1 mongodb-org-shell=2.6.1 mongodb-org-mongos=2.6.1 mongodb-org-tools=2.6.1

Output will be something like this:

rahulsaini@rahulsaini-UBUNTU:~$ sudo apt-get install -y mongodb-org=2.6.1 mongodb-org-server=2.6.1 mongodb-org-shell=2.6.1 mongodb-org-mongos=2.6.1 mongodb-org-tools=2.6.1
Reading package lists… Done
Building dependency tree
Reading state information… Done

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 9 not upgraded.
Need to get 113 MB of archives.
After this operation, 284 MB of additional disk space will be used.
Get:1 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ dist/10gen mongodb-org-shell i386 2.6.1 [4,389 kB]
Get:2 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ dist/10gen mongodb-org-server i386 2.6.1 [9,308 kB]
Get:3 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ dist/10gen mongodb-org-mongos i386 2.6.1 [7,045 kB]
Get:4 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ dist/10gen mongodb-org-tools i386 2.6.1 [92.3 MB]
Get:5 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ dist/10gen mongodb-org i386 2.6.1 [3,652 B]
Fetched 113 MB in 4min 52s (386 kB/s)
Selecting previously unselected package mongodb-org-shell.
(Reading database … 243751 files and directories currently installed.)
Preparing to unpack …/mongodb-org-shell_2.6.1_i386.deb …
Unpacking mongodb-org-shell (2.6.1) …
Selecting previously unselected package mongodb-org-server.
Preparing to unpack …/mongodb-org-server_2.6.1_i386.deb …
Unpacking mongodb-org-server (2.6.1) …
Selecting previously unselected package mongodb-org-mongos.
Preparing to unpack …/mongodb-org-mongos_2.6.1_i386.deb …
Unpacking mongodb-org-mongos (2.6.1) …
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack …/mongodb-org-tools_2.6.1_i386.deb …
Unpacking mongodb-org-tools (2.6.1) …
Selecting previously unselected package mongodb-org.
Preparing to unpack …/mongodb-org_2.6.1_i386.deb …
Unpacking mongodb-org (2.6.1) …
Processing triggers for man-db (2.6.7.1-1ubuntu1) …
Processing triggers for ureadahead (0.100.0-16) …
Setting up mongodb-org-shell (2.6.1) …
Setting up mongodb-org-server (2.6.1) …
mongod start/running, process 12163
Setting up mongodb-org-mongos (2.6.1) …
Setting up mongodb-org-tools (2.6.1) …
Processing triggers for ureadahead (0.100.0-16) …
Setting up mongodb-org (2.6.1) …

Once MongoDB has been installed via Ubuntu APT, it will be updated frequently whenever newer versions arrive. In order to stop this, the following commands need to be run

  • 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
  • Running MongoDB on Ubuntu (Linux)
    ======================================================

    The MongoDB instance stores its data files in /var/lib/mongodb and its log files in /var/log/mongodb by default, and runs using the mongodb user account. You can specify alternate log and data file directories in /etc/mongod.conf. See systemLog.path and storage.dbPath for additional information.

    If you change the user that runs the MongoDB process, you must modify the access control rights to the /var/lib/mongodb and /var/log/mongodb directories to give this user access to these directories.

    sudo service mongod start

    Verify that the mongod process has started successfully by checking the contents of the log file at /var/log/mongodb/mongod.log for a line reading

    [initandlisten] waiting for connections on port <port>

    where <port> is the port configured in /etc/mongod.conf, 27017 by default.

    grep “waiting for connections on port” /var/log/mongodb/mongod.log

    rahulsaini@rahulsaini-UBUNTU:~$ grep “waiting for connections on port” /var/log/mongodb/mongod.log
    2015-04-01T22:27:45.674+0530 [initandlisten] waiting for connections on port 27017
    rahulsaini@rahulsaini-UBUNTU:~$

    sudo service mongod stop

    Later, to stop MongoDB, press Control+C in the terminal where the mongod instance is running.

Enjoy !

Advertisements

#linux, #mongodb, #ubuntu