One of my projects is to automate a LEGO City, especially the train section. For that i need a place to save all the information. Initially choose a SQL database (MySQL), but as necessary to create custom tables for each case and create relations. But as i didn’t know what information was supplied by the City and i would need a communication protocol ( i’ll use MQTT. In the future i’ll write a tutorial how to use it) i choose a NoSQl database, MongoDB.
A NoSQL database doesn’t follow the Relational Model. It stores the informations on a “key-value” format, allow to save all type of information.
The first step was to install MongoDB on a Rasperry Pi 1 Model B running raspbian.
Installation process:
1 – Update the Raspbian
sudo apt-get update sudo apt-get upgrade
2 – Install all needed libraries needed by MongoDB
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
3 – Download, from Github, the MongoDB version for Raspbian. It will be downloaded to the user home folder
cd ~ ( the tilde represents the user home folder. This command can be executed in any part of the Operation System. It will always change the directory to the home use folder, in this case /home/pi) git clone https://github.com/skrabban/mongo-nonx86
4 – Enter the downloaded folder and start the MongoDB installation process
Note: The compilation process takes a long time. On the Raspberry Pi 1 Model B takes nine hours.
cd mongo-nonx86 sudo scons
5 – After compilation is needed to install the MongoDB on the system. As the previous step, this also takes a long time, about four hours
sudo scons --prefix=/opt/mongo install
6 – Create the user for MongoDB and give the needed permissions
sudo adduser --firstuid 100 --ingroup nogroup --shell /etc/false --disabled-password --gecos "" --no-create-home mongodb
7 – Create the needed folder for MongoDB
sudo mkdir /var/log/mongodb/ sudo chown mongodb:nogroup /var/log/mongodb/ sudo mkdir /var/lib/mongodb sudo chown mongodb:nogroup /var/lib/mongodb
8 – Move the startup script to the correct location
sudo cp debian/init.d /etc/init.d/mongod
9 – Move the configuration file to the /etc folder
sudo cp debian/mongodb.conf /etc/
10 – Create the links between folders
sudo ln -s /opt/mongo/bin/mongod /usr/bin/mongod
11 – Give the final permissions to the scripts
sudo chmod u+x /etc/init.d/mongod sudo update-rc.d mongod defaults
After this steps the MongoDB database is installed, but it is still necessary change a parameter on the configuration file to avoid the following error on the browser:
REST is not enabled. use --rest to turn on.
12 – Edit the configuration file
sudo nano /etc/init.d/mongod
13 – Find the following lines and add “–rest” at the end of the last one.
DAEMONUSER=${DAEMONUSER:-mongodb} DAEMON_OPTS=${DAEMON_OPTS:-"--dbpath $DATA --logpath $LOGFILE run"} DAEMON_OPTS="$DAEMON_OPTS --config $CONF"
14 – Save the file (Ctrl+X) and restart the service
sudo /etc/init.d/mongod restart
Verification:
To check if the service is working is used the following command:
Ps -aux|grep mongo ( this command lists all the running processes and only show those who have “mongo”)
If it is working a line similar with the next is displayed:
mongodb 15623 1.3 8.3 126028 37020 ? Sl 10:28 5:14 /usr/bin/mongod --dbpath /var/lib/mongodb --logpath /var/log/mongodb/mongodb.log run --config /etc/mongodb.conf --rest
Now it’s possible to use a basic web interface to use MongoDB. Simply access the following address:
http://<PI_IP>:28017
(the <PI_IP> parameter must be changed with the Raspberry Pi ip)
To have a full interface thera are some applications. I use “Robomongo” (http://robomongo.org)(only available for OSX).