Home > Mongo DB > Install MongoDB on Ubuntu

Install MongoDB on Ubuntu

Mongo DB

This tutorial helps to install MongoDB on Ubuntu systems. The tutorial uses .deb packages to install.

The MongoDB package repository contains five packages:

mongodb-org
This package is a metapackage that will automatically install the four component packages listed below.

mongodb-org-server
This package contains the mongod daemon and associated configuration and init scripts.

mongodb-org-mongos
This package contains the mongos daemon.

mongodb-org-shell
This package contains the mongo shell.

mongodb-org-tools
This package contains the following MongoDB tools: mongoimport bsondump, mongodump, mongoexport, mongofiles, mongoimport, mongooplog, mongoperf, mongorestore, mongostat, and mongotop.

Install MongoDB

Import the public key used by the package management system.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

Create a list file for MongoDB.

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

Reload local package database

sudo apt-get update
Install the MongoDB packages.
sudo apt-get install mongodb-org
apt-get install 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
Pin a specific version of MongoDB.
To prevent unintended upgrades, pin the package. To pin the version of MongoDB at the currently installed version, issue the following command sequence:
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

Run MongoDB

The MongoDB instance stores its data files in /var/lib/mongodb, its log files in /var/log/mongodb, and runs using the mongodb user account. 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.

Start MongoDB

sudo /etc/init.d/mongod start
Verify MongoDB
/var/log/mongodb/mongod.log.
Stop MongoDB
sudo /etc/init.d/mongod stop
Restart MongoDB
sudo /etc/init.d/mongod restart

Getting Started with MongoDB

Connect to a mongod

mongo


From a system prompt, start mongo by issuing the mongo command, as follows: By default, mongo looks for a database server listening on port 27017 on the localhost interface. To connect to a server on a different port or interface, use the –port and –host options.

Select a Database

At any time, issue the following operation at the mongo to report the name of the current database:

>db 
From the mongo shell, display the list of databases, with the following operation:
>show dbs
Switch to a new database named mydb, with the following operation:
>use mydb
Confirm that your session has the mydb database as context, by checking the value of the db object, which returns the name of the current database, as follows:
>db