How to install MongoDB on a Raspberry Pi…Really!

/Update 25 Sept – some handy links/
http://raspberrypi.stackexchange.com/questions/4883/how-can-i-build-mongodb
http://raspberrypi.stackexchange.com/questions/4883/how-can-i-build-mongodb/4914#4914

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 127.0.1.1 raspberrypi’ ) with the hostname you chose above ie ‘yellow’

$ sudo /etc/init.d/hostname.sh 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.http://ts1.mm.bing.net/th?id=I.4650776073995288&pid=15.1&W=160&H=160

$ 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/id_rsa.pub.
>>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:

http://ts4.mm.bing.net/th?id=I.4520067346075075&pid=15.1&W=113&H=160You’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

>>network={
ssid=”BTHub3-WFF6″
key_mgmt=WPA-PSK
psk=”nnanananna”
}
* 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 http://www.raspberrypi.org
ping: unknown host http://www.raspberrypi.org

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://github.com/RickP/mongopi.git

Build it (this took about four hours!):

cd mongopi
scons

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)
http://www.javaplex.com/blog/wp-content/uploads/2011/07/MongoDB.png

$ 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!

http://ts1.mm.bing.net/th?id=I.4937349179376252&pid=15.1&W=160&H=156Got a 32MB SD card from Maplin from 19.99. Re-ran the install. Looks promising. Cheapies can be found here
/rootfs
/dev/root
- 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 192.168.1.254 and (2) IP address, which is 192.168.1.86

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 192.168.1.86 and also uses DHCP then the use of addresses greater than 192.168.1.200 is unlikely to clash with the DHCP server, so i will start the five Raspberry Pi’s from this point.

My network is 192.168.1.0 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

——————————————————
address   192.168.1.200
# 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)
netmask   255.255.255.0
# Highlighted in step 2 Subnet Mask
network   192.168.1.0
# The first 3 PARTS of the gateway plus a 0 ending
broadcast 192.168.1.255
# The first 3 PARTS of the gateway plus a 255 ending
gateway   192.168.1.1
# 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

http://andyfelong.com/2013/02/raspberry-pi-meets-mongodb/

About these ads

19 thoughts on “How to install MongoDB on a Raspberry Pi…Really!

  1. A little comment:
    On 256MB Raspi the original 100 MB swap file is not enough to compiling.
    200 MB was enough.

    Good post ! Thx Stuart.

    • What error did you see when it failed?

      How do you increase the swap size?

      Trying to compile it now on my 256MB pi – I’ve overclocked it to speed it up!

      Thanks

  2. Pingback: Distribution, Replica sets, Master-Slave « mongopi

  3. Pingback: ‘Cloning’ SD cards « mongopi

  4. You want to run mongos on the client side so apps can connect to localhost and exactly three config servers so multi-phase commit can have a quorum

  5. Nvm, I just realized it is a problem with the mongo (the client) on the arm distro. If i use mongo on my mac to connect to the mongod cluster on my pi, it will work.

  6. Nice article. ARM V6 (1) is 32 bits (see ARM V8 for 64b) so the MongoDB built is limited to 32b. Each PI should give you 2 Gb only ? For n shards = n*2 Gb, 6 PI give you 12 Gb…

  7. Thank you so much for posting this. I received an error 2.5 hours after running the install command (sudo scons –prefix=/opt/mongo install). Any idea why this would be? I’ll be searching for the answer and if I’ll return here to update if I can find it out for the benefit of anyone else who stumbles across this.

    {standard input}:37559: Warning: swp{b} use is deprecated for this architecture
    {standard input}:40499: Warning: partial line at end of file ignored
    {standard input}: Error: open CFI at the end of file; missing .cfi_endproc direc tive
    g++: internal compiler error: Killed (program cc1plus)
    Please submit a full bug report,
    with preprocessed source if appropriate.
    See for instructions.
    scons: *** [build/linux2/normal/mongo/db/json.o] Error 4
    scons: building terminated because of errors.

  8. I simply wish to inform you that it means a whole lot for someone for taking time to publish this article and
    share about what they know. Is there anyway you can put
    me on an email alert list so I know of your upcoming updates?

  9. Hello,
    It would be nice to write a blogpost about, what exactly you changed to make the mongodb code build on the Raspberry Pi.
    I noticed you got stuck on a version way off current version (2.4.6)… and I’d love to use the latest and greatest version…
    I know it’s a fetish, but hey… I’m trying to learn too :-D.

    • Hi Michael,

      Thanks for your note. Likewise, I wish someone far more able and technically capable than me would port a more recent version onto GitHub. I’m not a developer, sadly.

      • Hi,

        I would be happy to do so, but tell me what you did until now to make the older versions compile ?
        I tried first to download the latest and greatest version and then followed your instructions, but that didn’t work :-D.
        I guess he looks for some libraries that are not ARM compatible… but that’s just a guess in the wild.

        Michael

  10. if you’re still playing this this, I recommend use of the –smallfiles flag, if you aren’t already. Mongo preallocs a lot.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s