Raspberry Pi MongoDB Installation – The working guide!

In this tutorial I will go through how to install MongoDb on your Raspberry Pi and also give a few tips on typical issues and fixes.
MongoDb is one of the most popular “NoSQL” databases out there. NoSQL releases you from the constraints of a typical relational database. They essentially allow you to store data in “key – value” stores which means you do not really have a typical schema as you would see in most other databases. They are often all open source and are horizontally scalable. MongoDB is said to be a document-oriented database which is designed for ease of development and scaling.

MongoDB

So why am I not considering MYSQL and the likes? Well I already run MySql on my Pi and I have to say the performance has been perfectly fine. This blog runs solely of my Pi and the wordpress data all sits on MySql. I have heard a lot of arguments against using it on the Pi due to resource usage etc, however it has run perfectly fine for me with little overhead. However with all of that being said you should never constrain yourself and I am currently working on a number of projects that require me to hold lots of random data together in a database. I want to be able to work quickly and not spend time having to set up table after table and check my relations, field types etc. I just want to store data and access it simply. This is where MongoDB comes in and really works well.

Before kicking off with the installation instructions I will add a few links for further reading below so you can get a bit more information about MongoDb, its usages, its good and bad points and also why you should consider NoSQL.

Further Reading:
What is noSql?
Advantages & Disadvantages of NoSql
When NoSQL makes sense to use

Lets get on with the installation. Firstly get yourself logged in to a terminal window on your Pi, either from the desktop or SSH straight in. As always we will run the following two commands:

sudo apt-get update
sudo apt-get upgrade

Next we will dive in to grabbing a fair few libraries. If you have followed any of my previous blogs you will probably have a few of these already but essentially these will allow us to use github, build tools, python implementation of mongo client and various file/system extensions.

sudo apt-get install build-essential libboost-filesystem-dev libboost-program-options-dev libboost-system-dev libboost-thread-dev scons libboost-all-dev python-pymongo git

Just hit yes to any of the other libraries it wants to pull in once you use the above command. We then move on to grabbing a version of MongoDb that has been edited to work on the Pi. If you try and install Mongo most other ways you get compile errors and that comes back to the same issues we have seen with the likes of Mono, at the moment quite a few products out there do not support the Pi architecture. But that is not a problem for us today as Skrabban has hosted an edited version for us.

cd ~
git clone https://github.com/skrabban/mongo-nonx86

This will clone the repository in to the mongo-nonx86 directory.

cd mongo-nonx86

We are now sat in an uncompiled version of MongoDb for the Pi, we will have to compile this ourselves on the Pi. If you are using one of the model A Pi’s this is mostly likely going to be a problem for you as the amount of RAM can be too low and max out. I will also point out that compiling takes up SD Card space so make sure you over 500mb free.

The following code should be executed within our mongo-nonx86 directory. It will execute “Scons” which is a software construction program written in python. WARNING: The below code will take hours to run on your Pi, try letting it run over night.

sudo scons

This above code will build the repository for us. When I first ran this after a few hours I hit an error during compiling. I expect it was because I ran “scons” without sudo. However even with the terminating error I just ran “sudo scons” once more and everything built correctly.

We can now run the installation code.

sudo scons --prefix=/opt/mongo install

Once again the above code can take a few hours to complete, for me it took about 4 hours in total. You should now have an installation of mongo on your Pi. We now need to do some general housekeeping with folders and permissions then finally an init.d script to be able to kick off mongodb easily.

Permissions for users and groups:

sudo adduser --firstuid 100 --ingroup nogroup --shell /etc/false --disabled-password --gecos "" --no-create-home mongodb

A folder for our log files to go:

sudo mkdir /var/log/mongodb/

Permissions for log file:

sudo chown mongodb:nogroup /var/log/mongodb/

A folder for our state data:

sudo mkdir /var/lib/mongodb

Permissions for the folder:

sudo chown mongodb:nogroup /var/lib/mongodb

Moving our init.d script to etc:

sudo cp debian/init.d /etc/init.d/mongod

Moving our config file to etc:

sudo cp debian/mongodb.conf /etc/

Linking folders up:

sudo ln -s /opt/mongo/bin/mongod /usr/bin/mongod

The above code as said does a lot of housekeeping but you may notice that when trying to run the init.d script you hit some errors. We need to finalise permissions and make sure the init.d script is executable so complete the installation with the final statements.

sudo chmod u+x /etc/init.d/mongod
sudo update-rc.d mongod defaults

You may get some warning when executing the above statement however these can be ignored.

We can now start MongoDB using the following command:

sudo /etc/init.d/mongod start

To connect to the instance you can then type:

mongo

You should then see connected to test:

pi@raspberrypi ~ $ mongo
MongoDB shell version: 2.1.1-pre-
connecting to: test
>

So now we have a mongo instance running on our server at http://localhost:27017 however mongo also has a very basic interface you can access at http://localhost:28017 just to give basic information.

If you click a link from that page and are hit with:

REST is not enabled.  use --rest to turn on.

Then we simply need to edit the init.d script and add in –rest as a parameter. The code below will show you how to do this.

sudo nano /etc/init.d/mongod

Scroll down to:

DAEMONUSER=${DAEMONUSER:-mongodb}
DAEMON_OPTS=${DAEMON_OPTS:-"--dbpath $DATA --logpath $LOGFILE run"}
DAEMON_OPTS="$DAEMON_OPTS --config $CONF"

And add in –rest to give:

DAEMONUSER=${DAEMONUSER:-mongodb}
DAEMON_OPTS=${DAEMON_OPTS:-"--dbpath $DATA --logpath $LOGFILE run"}
DAEMON_OPTS="$DAEMON_OPTS --config $CONF --rest"

Hit Ctrl+X, Y and then restart mongodb using:

sudo /etc/init.d/mongod restart

You will then have all of the options available to you through the mongo UI webpage. Well, for now thats about it. In later tutorials I will go through creation of collections, saving data, reading data and some of the uses you can put MongoDB to. If you have any problems/comments please leave them in the comments section below and I’ll get back to you with answers in no time.

Enjoy!

You may also like...

  • Pingback: Install MongoDB on your #RaspberryPi | Raspberry PiPod

  • https://plus.google.com/112099437248258927068 Stephen Emo

    It does work on a 256 MB PI you just need to increase the swap space.

    • MrMobberley

      Stephen can you post the code to do this correctly and I’ll add to the post with your reference. Thanks

  • Pingback: Raspberry Pi MongoDB Installation - The working...

  • https://plus.google.com/+JuanManuelFluxà Juan Manuel Fluxà

    Thank you for sharing this, it works like a charm.

  • fabianp

    It says “bash: mongo: command not found”! I followed all the steps..

    • https://plus.google.com/+JuanManuelFluxà Juan Manuel Fluxà

      I think there’s a missing step, make a symlink for /opt/mongo/bin/mongo

      `sudo ln -s /opt/mongo/bin/mongo /usr/bin/mongo`

      • MrMobberley

        Thanks for the catch if this works for people I’ll add in to the tutorial

      • fabianp

        This does seem to work, but for some reason I can’t start my mongodb anymore.. it says failed all the time!

      • MrMobberley

        Just taken a look at this again but I cant work out why we need to do the symlink as per your suggestion =[ the scons prefix (sudo scons –prefix=/opt/mongo install) should have pushed the files to the /opt/mongod folders which we then symlink to later on in the tutorial.

  • dchild

    Thanks, it worked for me! BTW, in order to get the mongo executable I had to do this:

    sudo ln -s /opt/mongo/bin/mongo /usr/bin/mongo

    in case anyone else has the same issue. Thanks again!

  • dchild

    sorry didn’t see Juan posting the same thing :)

  • fabianp

    Okay! I got it working fully now! I seemed to have corrupted data of my database. After a –repair on mongod it simply started again! All is running. Thanks a lot!

  • Piman

    Can I now delete the mongo-nonx86 directory? It is taking up like 2GB of space…

  • Cole

    When executing `sudo cp debian/init.d /etc/init.d/mongod`, I get the following error:
    cp: cannot stat `debian/init.d’: No such file or directory

    Any ideas? Thanks in advance, COle

    • http://mynewtec.blogspot.com Saumya Suhagiya

      execute command in mongo-nonx86 directory

  • Andy

    Please be advised that the mongodb version is quite old (2.1.1) and don’t have all functionality as current versions.

  • Siddu

    It worked like a charm. But the version of mongodb seems to be old from the Github fork mentioned above. Is there a latest version available? Is there a possibility to simply upgrade or should I go through the complete process of compiling, installing from the beginning?

  • http://gravatar.com/blackili akismet-b523130c1f2f2dcfb8972c82870a6d0b

    symlinking worked for me, thank you!

  • http://gravatar.com/ericlorenzobenjamin ericbenjaminjr

    Great tutorial. Thanks for this..

  • Pingback: Tutorials | Pearltrees

  • Pingback: Raspberry Pi MongoDB Installation | RasPi Stuff...

  • http://gravatar.com/mariomka mariomka

    Hi, great tutorial but I have a problem.

    Mongo compiles well but mongo shell dont runs:

    $ sudo /etc/init.d/mongod start
    [ ok ] Starting database: mongodb.

    $ cat /var/log/mongodb/mongodb.log
    Mon Jan 27 13:12:53 [initandlisten] MongoDB starting : pid=22084 port=27017 dbpath=/var/lib/mongodb 32-bit host=raspBob
    Mon Jan 27 13:12:53 [initandlisten]
    Mon Jan 27 13:12:53 [initandlisten] ** NOTE: This is a development version (2.1.1-pre-) of MongoDB.
    Mon Jan 27 13:12:53 [initandlisten] ** Not recommended for production.
    Mon Jan 27 13:12:53 [initandlisten]
    Mon Jan 27 13:12:53 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
    Mon Jan 27 13:12:53 [initandlisten] ** see http://blog.mongodb.org/post/137788967/32-bit-limitations
    Mon Jan 27 13:12:53 [initandlisten] ** with –journal, the limit is lower
    Mon Jan 27 13:12:53 [initandlisten]
    Mon Jan 27 13:12:53 [initandlisten] db version v2.1.1-pre-, pdfile version 4.5
    Mon Jan 27 13:12:53 [initandlisten] git version: 47fbbdceb21fc2b791d22db7f01792500647daa9
    Mon Jan 27 13:12:53 [initandlisten] build info: Linux raspBob 3.10.25+ #622 PREEMPT Fri Jan 3 18:41:00 GMT 2014 armv6l BOOST_LIB_VERSION=1_49
    Mon Jan 27 13:12:53 [initandlisten] options: { command: [ "run" ], config: “/etc/mongodb.conf”, dbpath: “/var/lib/mongodb”, logappend: “true”, logpath: “/var/log/mongodb/mongodb.log”, rest: true }
    Mon Jan 27 13:12:53 [initandlisten] waiting for connections on port 27017
    Mon Jan 27 13:12:53 [websvr] admin web console waiting for connections on port 28017

    $ mongo
    MongoDB shell version: 2.1.1-pre-
    connecting to: test
    Mon Jan 27 13:15:21 Error: couldn’t connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
    exception: connect failed

    $ sudo netstat -anp | grep 27017
    tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 22084/mongod
    unix 2 [ ACC ] STREAM LISTENING 195687 22084/mongod /tmp/mongodb-27017.sock

    Admin web works well.

    Any ideas?

  • http://gravatar.com/astein58 Alexandre Stein

    Hi

    I’m maybe wrong, but:
    Does it make sense to distribute compiled files instead everyone has to compile it.
    Which take ages…

    Is it possible?

    • MrMobberley

      At the moment I don’t have the time but if you could provide that and host somewhere I’m happy to add the link

  • David

    Works fine, thanks a lot for the clear guidelines!

  • Tamas Jozsa

    I followed you guide and it all works well to a point where mongo console dies of Segmentation fault.

    Here is the sequence to reproduce:

    1. make sure mongod is running

    2. run mongo shell with mongo command

    3. execute show dbs from within mongo shell

    After a few seconds the following error is shown:

    > show dbs

    Tue Feb 11 19:41:29 mongo got signal 11 (Segmentation fault), stack trace:

    Tue Feb 11 19:41:29 0x22a06c 0x8e288 0xb6ca6e40

    mongo(_ZN5mongo15printStackTraceERSo+0×18) [0x22a06c]

    mongo(_Z12quitAbruptlyi+0×220) [0x8e288]

    /lib/arm-linux-gnueabihf/libc.so.6(__default_sa_restorer_v2+0) [0xb6ca6e40]

    Do you have an idea?

  • Pingback: Raspberry Pi MongoDB Installation

  • Pingback: Fixing my first problem with MongoDB. » The dodgy domain of Digital Darragh

  • Sebastiaan

    At 1.7GB of files and zero space left on my Pi, it stopped building after nearly 10 hours. And that’s after I tried more than 5 times to run it (they all quit early). So what’s my game plan now? Any other ways to get MongoDB on my Raspberry Pi?

  • Alex stein

    I made it and a saved the compiled version.
    Do you think it could do it?

  • Pingback: Fish Tank Monitor: Setting up the Pi | Tarcio Saraiva

  • akhyar

    -bash: mongo: command not found. how do I configure the path?

    • szahran

      I have same problem did you find a solution

      • Raul Ernesto Martinez

        Try sudo ln -s /opt/mongo/bin/* /usr/bin/*

    • http://www.sebastienb.com sebastienb

      did you find a solution ? having the same problem

      • nkulig

        I also am having the same issue. Any luck?

  • Raul Ernesto Martinez

    great article, it was very helpful :)

  • Cristian

    Great article! Is it possible to do this process in a raspberry pi and then copy and paste any file or directory to another raspberry pi mongodb function without the need to compile?
    Thanks!

    • Alex stein

      I already take about that on this page.
      I have an old version fully compiled here: https://mega.co.nz/#F!LxpWzR7I!wnE1RKQJ_Ba1O-BXh5fp3Q

      I hope it will work for you.

      • Cristian

        Thanks! Where do you have to copy these files? I guess after copying files must follow the installation? (sudo scons –prefix=/opt/mongo install)
        Thanks again

        • Alex stein

          It doesn’t matter.

          I use Supervisor to run my Mongod instance and here is me supervisor conf file.It’s dead simple, just look the “command” line.

          [program:mongod]
          command=/media/usb_4g/mongodb/bin/mongod –journal –dbpath media/usb_4g/mongodb/data
          stdout_logfile=/media/usb_4g/mongodb/out.log
          stderr_logfile=/media/usb_4g/mongodb/err.log
          autostart=true
          autorestart=true
          startsecs=10
          stopwaitsecs=600

          PS: Supervisor is great :-)

          • Cristian

            thanks! I’ll try supervisor! But you need to install the binary somehow? because the binaries are not executable for the supervisor …

          • Alex stein

            Download the mongod file.
            Add the execution permission with chmod +x
            Run the file with the appropriate options.

          • Cristian

            done! With “./mongo” works, but is there a way to generate the file “mongod” for export to /etc/init.d? mongod that the file you download is a binary and I think it serves…

  • http://giannigar.wordpress.com Giovanni Gargiulo

    Perfect tutorial. Worked at the fist shot. Thanks a mil!

%d bloggers like this: