How to install MongoDB on a Raspberry Pi…Really!

/Update 25 Sept – some handy links/

My 4th Pi arrived today, with an SD card pre-loaded with the O/S. Gave me the opportunity to try a ‘fresh’ config, after making several botched attempts not fully knowing what to do previously.

Write the Raspian Operating System to the SD card

Copy the latest O/S from the R Pi website.
If you haven’t already, get the image writer software for Windows
Download the binaries, not the source)
Extract the zipped O/S archive. Find the image file from within the image writing software. Write to the card (takes 5 mins or so).

Modify initial config settings

On 1st boot, I changed the following default settings. I also repeated some of these below, manually. Following great advice from Chris Elsmore, and his blog

  • Expand rootfs – expand the root partition to fill the SD card
  • Overscan – enabled
  • memory_split – how much memory should the GPU have? I set mine to 32 of the 512MB as I won’t be running graphically intensive apps. I hope this frees up most of the resource to run MongoDB
  • ssh – enabled
  • boot_behaviour – start desktop on boot
  • update – could not resolve mirror sites etc (I assume config for eth0 connection needs to be done?)

Change Hostname:

I also changed the hostname of my Pi, each of the 5 are housed in a colured plastic case.

Change hostname to match case - keep it simple!

Change hostname to match case – keep it simple!

$ sudo nano /etc/hostname

- this opens up a texteditor containing 1 line. I changed 'raspberrypi' to 'yellow' “CTRL+O” then “Y” to quit) 

$ sudo nano /etc/hosts

– again, this opens up a texteditor. this time containing 8 lines. Replace the last line (mine had ‘IP raspberrypi’ ) with the hostname you chose above ie ‘yellow’

$ sudo /etc/init.d/ start (to enable the changes).

The prompt should now say pi@’newhostname’ in my case pi@yellow. A further check is to run the command
$ hostname

Enable SSH:

SSH lets you remote login to a Pi. I haven’t yet needed to do this as I have >2 HDMI ports on my monitor, so just flip between video inputs.

$ ssh-keygen (I hit enter for all three options to accept defaults and no passphrase)

$ sudo service ssh start (to start sshd)

$ sudo update-rc.d ssh defaults (to run the ssh server on startup by default)

>>Generating public/private rsa key pair
>>Enter file in which to save the key (/home/pi/.ssh/id_rsa)
>>Enter passphrase (empty for no passphrase):
>>Enter same passphrase again:
>>Your identification has been saved in /home/pi/.ssh/id_rsa
>>Your public key jas been saved in /home/pi/.ssh/
>>The key fingerprint is: [a long string!]

$ sudo service ssh start (to start sshd)
>>[ ok ] starting OpenBSD Secure Shell server: sshd

$ sudo update-rc.d ssh defaults (to run the ssh server on startup by default)
>update rc.d: using dependency based boot sequencing
>update rc.d: warning: default stop runlevel arguments (0 1 6) do not match ssh Default-Stop values (none)

Now to config an internet connection so I can try to install MongoDB on this ‘fresh’ card.

Config an internet connection:’ll need to get out on to the internet so that the Pi can see the various mirrors and repositories it used to pull down updates.

$ dmesg | grep ^usb
$ dmesg | grep ^wlan
$ dmesg | grep ^wireless
$ dmesg | grep ^firmware

One of these commands should indicate the manufacturer of your USB wireless adapter. Mine (from Maplin) was “Ralink

Search the library for the appropriate package
$ apt-cache search ralink

Install the package
$sudo apt-get install firmware-ralink

Now create a config file to specify what type of encryption your home router has, necessary IDs, passwords etc
$ sudo nano /etc/network/interfaces

Add the following three lines to the bottom of the file (ignoring the numbers)
1. auto wlan0
2. iface wlan0 inet dhcp
3. wpa-conf /etc/wpa.conf

CTRL+X to save

The last line makes ref to a config file, wpa.conf which needs to be created. The file will be used by wpasupplicant, designed to provide Linux with an easy way to connect to networks secured with by WPA (most home commercial broadband networks I guess).

$sudo nano /etc/wpa.conf

* This is specifically my home SSID. My password is a combination of 10 alphanumerics *

The Pi’s wireless network is now (supposedly !) updated & configured, and will begin the next time the Pi is restarted. To start the wireless network without rebooting

$ sudo ifup wlan0
To make sure it’s working

$ ping  -C  1
ping: unknown host

In my case, things weren’t working, so I flipped over to the desktop to use the wifi config utility. On doing so, I could see the home broadband network and it was then easy to connect.

$ startx
To start the windows desktop emulator
Run the Wifi config programme, click the scan button, connect to the relevant network, provide any required info.

Install MongoDB via GitHub

Because of the limitations/power of the Pi (it is £30 after all!), you need to get the version that has been specially created for the R Pi.

Source control/repositories

Install the requisite packages on the Pi

sudo apt-get install git-core build-essential scons libpcre++-dev xulrunner-dev libboost-dev libboost-program-options-dev libboost-thread-dev libboost-filesystem-dev

I could not get this to execute successfully as a single script, so instead install each individual component one-by-one

$sudo apt-get install git-core
$ sudo apt-get install libboost-filesystem-dev

This seemed to work better.

Check for any updates to these
$ sudo apt-get update

Pull the files from this fork on Github:

git clone git://

Build it (this took about four hours!):

cd mongopi

you’ll see lots of these lines as the build progresses for several hours…
{standard input}: nnnnn: Warning: swp{b} use is deprecated for this architecture

Install it (this took about 3 hours)

$ sudo scons --prefix=/opt/mongo install

This will install mongo in /opt/mongo. Takes several hours. To get other programs to see it, add this dir to your $PATH:

Check the filepath where mongo is executed
pwd = present working directory

$ PATH=$PATH:/opt/mongo/bin/
$ export PATH

While you’re waiting for everything to install, take a look at the primer/tutorial on MongoDB on CPAN

All looks good….

Now. I can at last fire up MongoDB on the Pi! Or, so I thought….

pi@clear ~/mongoppi $ mongod
 ERROR: dbpath (/data/db) does not exist
 Create this directory or give existing directory in --dbpath
 , terminating

So, I assume I need to create the directories
sudo mkdir -p /data/db
mkdir: cannot create directory ‘data’ : No space left on device

Check disk usage
$ df -h
Reveals that the SD card is completely full.

I run scons -c to clean up any temp files hanging around after the installation. This frees up just 573M. That doesn’t feel like sufficient room to get some data on, and I’m also suspicious the install has really worked, given it seems to have taken all the space. So, time to experiment with some larger cards! a 32MB SD card from Maplin from 19.99. Re-ran the install. Looks promising. Cheapies can be found here
– Now both have 27G (94%) of 30G spare. That should give me 125GB or so across the array of five R PIs. One GB equates to about 1000 thick books.

Scan for the 20 largest files, perhaps there are some temp install files that can be cleared up.

#du -a /var | sort -n -r | head -n 20

Create the necessary /data/db
mkdir -p /data/db
try mongo again

Returns error: [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/db/mongo.lcok errno13: permission denied Is a mongo instance already running?, terminating

Check to see if any mongod instances are running
$ps -ef | grep mongod
– Suggests none are.

Therefore I suspect it is some kind of permissions conflict as the /data/db directory was created as ‘root’.

Seems like I’m not the only one to experience this common problem
$ sudo chown $USER /data/db
Seems to do the trick!

Configuring network access between devices / creating a lan

Now that Mongo is installed and you have updated packages, configured the PIs etc. It’s time to connect them all into a network hub/LAN. My primary device is my laptop, running windows 7. This is connected over Wifi to a BT router. It is this information that needs to be propagated to the R Pis. Thanks to instructions on Simon The PiMan’s blog.

> ipconfig /all|more
This pipes ipconfig information out 1-page at a time and can be tabbed through by pressing the spacebar.

Page 2 has the information required. The two important bits of info we need for setting up a Raspberry Pi with a fixed IP address are (1) Default Gateway which is the router to access the internet, in my case it has an IP address of and (2) IP address, which is

As you can see, Default Gateway has the same IP address as for the DHCP server  – I believe this means local addresses will be allocated by the same router (although my knowledge of networking is low!)

As my I.P. address is and also uses DHCP then the use of addresses greater than is unlikely to clash with the DHCP server, so i will start the five Raspberry Pi’s from this point.

My network is as the home local network is usually the 1st 3 parts of the IP address plus a 0 (but this is dependant on the Subnet Mask, usually irrelevent for most home users – and i’m out of my depth now)

So to conclude – My 5 Pis needs the following items to be setup within my LAN.

1 has yet to arrive!
Hooking in the 5 R PIs into a home LAN

# The first 3 digits of the gateway plus a Unique number per machine up to 254,
# Starting at 200 as it gives me 54 potential Pi addresses (max is 254)
# Highlighted in step 2 Subnet Mask
# The first 3 PARTS of the gateway plus a 0 ending
# The first 3 PARTS of the gateway plus a 255 ending
# Highlighted in step 2 Default Gateway

[ ~memo: not sure where this info gets written to (yet) ]

NOTE:- Details of your ADSL router are specific to the router so you will need to look at the documentation for the router if you want more details, and will also need this *to enable a DMZ for your Pi to run from*.

Creating a LAN. Use colour coded ethernet cables. It all gets rather messy after a while!

Hopefully all the network stuff is more or less done now, which should enable me to hook everything up and get things ‘talking’. The fun hopefully starts when I can start using MongoDB to shard data across each Pi node!

Sharding with MongoDB

Sharding with MongoDB

Big thanks to Simon & Chris for their generous help!

Update 02 April 13
Just noticed a new link/site

How to see the PIs?

So, I have successfully (I think) flashed the Raspian O/S onto each of the 2 SD cards.
Note to self: Buy an SD card with it pre-installed next time!

Initially, just one of the PIs changed from a red to a green light. I assume the O/S hasn’t been successfully written onto the one with the red light (but how to tell?).

Even though all devices (excl the Dell which isn’t turned on) are connected into the network switch device (TP-link). The light for port(2) on the switch is out. Probably echoes the above.

I swapped the SD cards between the 2 PIs and now have 4 lights on the switch. And all the PIs are green. Odd.

So, looks like we have a cluster and the O/S flashing onto each card has been successful. An afternoon of my life i’d rather not have to repeat!

Successfully flashed the O/S onto the PIs

I do like green lights!

What next?!

So, I run Puppet and hope(!) it can see the ‘members’ or ‘nodes’. TBH, I don’t know how to use Puppet (yet).

But, it only sees the laptop :(

Doh! Was hoping Puppet could see everything connected into the network switch (laptop & 3 RPi)

I know I have to do some O/S stuff now

But cannot ‘see’  each PI. Maybe I need to buy a video cable and connect them to a monitor, but had hoped I could switch to them from the laptop? One for tomorrow!

Update: Until you have successfully got SSH enabled and figured out the IP [ip show addr] address for each raspberry Pi, i’d recommend a HDMI switch, which lets you easily toggle between devices, without having to plug/unplug HDMI cables from your monitor

Let’s you flip between 3 devices. I have laptop in HDMI port 1 and the 3 R Pis into the switch and into HDMI port 2