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...

52 Responses

  1. Stephen Emo says:

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

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

  3. fabianp says:

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

    • 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 says:

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

      • fabianp says:

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

      • MrMobberley says:

        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.

  4. dchild says:

    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!

  5. dchild says:

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

  6. fabianp says:

    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!

  7. Piman says:

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

  8. Cole says:

    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

  9. Andy says:

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

  10. Siddu says:

    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?

  11. Great tutorial. Thanks for this..

  12. mariomka says:

    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?

  13. 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?

  14. David says:

    Works fine, thanks a lot for the clear guidelines!

  15. Tamas Jozsa says:

    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?

  16. Sebastiaan says:

    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?

  17. Alex stein says:

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

  18. akhyar says:

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

  19. Raul Ernesto Martinez says:

    great article, it was very helpful :)

  20. Cristian says:

    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 says:

      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 says:

        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 says:

          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 says:

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

          • Alex stein says:

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

          • Cristian says:

            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…

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

  1. October 14, 2013

    […] Read it here […]

  2. October 29, 2013

    […] 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. …  […]

  3. January 24, 2014

    […] Raspberry Pi MongoDB Installation – The working guide! – RASPBERRY PI […]

  4. January 25, 2014

    […] 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. …  […]

  5. February 13, 2014

    […] Full instructions for install mongodb on a Pi can be found here […]

  6. February 17, 2014

    […] It isn’t available in the APT repository and compiling it on the Raspberry pi is a little different so I followed this very helpful guide. […]

  7. March 15, 2014

    […] I followed the instructions from this blog post. There’s no “hocus pocus” or complicated stuff but it just takes forever and I […]

  8. July 29, 2014

    […] via Raspberry Pi MongoDB Installation – The working guide! – RASPBERRY PI. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>