Setting Up MongoDB on Linux

Russell Bateman
12 May 2011
last update:

Table of Contents

Fresher, working packages
Formal installation of latest packages
Trouble installing Debian (Ubuntu) packages
Setting up to use MondgoDB
MongoDB was running, but won't start
Learning to use MondgoDB
Using Java with MondgoDB
Useful links


MongoDB (from "humongous") is a scalable, high-performance, open source, no-schema, document-oriented database written in C++ which appeared in February, 2009.

I had to set MongoDB up on a server for my team at work. We're using it for a project just underway, so I'd never actually used it when I started this article.

A year later, I rewrote the first bit on installation.

Never, never, never install MongoDB from Ubuntu repositories. The version of Mongo installable using Ubuntu Software Center is unusable. It can't be connected to from JDBC, Django, etc. This is similar to installing Eclipse from the Ubuntu Software Center: it produces nothing usable.

If you find you installed MongoDB this way and wish to remove it, do this:

russ@tuonela:~> dpkg -l | grep mongodb rc mongodb-server 1:1.4.4-2ubuntu2 An object/document-oriented database (server package) russ@tuonela:~> sudo dpkg --purge mongodb-server [sudo] password for russ: (Reading database ... 273127 files and directories currently installed.) Removing mongodb-server ... Purging configuration files for mongodb-server ... dpkg: warning: while removing mongodb-server, directory '/var/lib/mongodb' not empty so not removed. dpkg: warning: while removing mongodb-server, directory '/var/log/mongodb' not empty so not removed. Processing triggers for ureadahead ... ureadahead will be reprofiled on next reboot

Then simply erased the following subdirectories:

Fresher, working packages: the right way

This page tells it as well or better than I can:

However, if you can't reach the Ubuntu keyserver in the first instruction, you are probably going through a proxy and must account for that. Re-try that instruction after doing this:

root@tuonela:~> export http_proxy="" root@tuonela:~> export https_proxy=""

So far, this always does it for me. If you're just back here in this tutorial to get that link, the following obviates the need to go there (short, command summary):

$ export http_proxy="" $ export https_proxy="" $ apt-key adv --keyserver --recv 7F0CEB10 $ vim /etc/apt/sources.list.d/10gen.list # add this line to this (probably new) file: # deb dist 10gen $ apt-get update $ apt-get install mongodb-10gen $ ps -ef | grep [m]ongo # see Jack run as a service! mongodb 19465 1 0 23:39 ? 00:00:00 /usr/bin/mongod --config /etc/mongodb.conf

Use tarball for a private installation

If you'd like the latest without installing it as a service on your system, go download it from MongoDB downloads as a simple tarball.

In this way, you can get a private copy of MongoDB to run without installing it on your host (or so I think; I haven't tried not installing it). There is a bin subdirectory.

I have a separate, runnable copy on my Linux development host that I used in formal MongoDB training. MongoDB was already running as a service before I did that since the web applications I develop rely on it.

Setting up to use MongoDB

This pertains mostly to running a private copy.

To get started (this is all at the "learning" link below), you'll need to create and place for Mongo databases to live on your platform. For Unix and Linux this is as below. You need privileges too. You can give these to everyone if you like.

root@tuonela:~# mkdir -p data/db root@tuonela:~# chmod -R a+rw data root@tuonela:~# ll data total 1 drwxrwxrwx 3 root root 72 2011-06-28 10:47 . drwxr-xr-x 25 root root 728 2011-06-28 10:47 .. drwxrwxrwx 2 root root 48 2011-06-28 10:47 db

To ensure that all is set up correctly, browse to http://localhost:28017/. There you'll find an administrative page with the title, mongod hostname with a great deal of information on it including the contents of the log (from /var/log/mongodb.log). You'll need to refresh to see new additions. For example, if you were running the Mongo shell, exited, then launched it again, then refresh the admin page, you'll see two more lines at the end of the log:

11:09:33 [conn4] end connection 11:09:38 [initandlisten] connection accepted from #5

Used to work, but can't get it to start now?

When mongodb is not shut down cleanly, there's a file, /var/lib/mongodb/mongod.lock, that's present. The recommendation is to remove this file and try again. You might also need to run mongodb --repair. See inside /var/log/mongodb/mongodb.log if there isn't a message to this effect.

Learning to use MondgoDB

Check out the MongDB website for a tutorial to get started. You'll use Mongo's shell at first. It's important to understand the concept. There are also high-powered GUI environments with which to access MongoDB. Later, you'll consume Mongo from your application.

Using Java with MongoDB

You can also find the Java driver JAR and documentation for same on the downloads page.

To get Javadoc is somewhat difficult. The authors won't go to the trouble for you, so you'll have to go get the source for the version you want, probably at:
and download it.

Explode the download in a subdirectory somewhere, delve down in to find the src subdirectory and, via Eclipse Build Path, attach it to the MongoDB JAR.

Useful links