Mongo DB on Raspberry Pi Zero W
The goal of this project is to setup Mongo DB on a Pi Zero. I am fairly new to the Pi Zero and this is mainly for documenting steps and lessons learned.
Initial Installation
Using the Raspberry Pi Imager v1.5:
Select Raspberry Pi OS Lite (32-bit)
Write to 32 GB micro SD card
N**ote: Ubuntu does not work for the Raspberry Pi Zero W
Initial Power-up
login: u:pi, p:raspberry
sudo raspi-config
to setup wifi and change default passwordreboot when prompted
Note: Within raspi-config, I set Localisation Options (5) -> WLAN Country (L4), System Options (1) -> Wireless LAN (S1) and Password (S3)
Install and Upgrade all components
sudo apt update
sudo apt upgrade
Install Mongo DB
sudo apt install mongodb
Note: From what I’ve read, Mongo DB appears to be deprecating the 32-version. Ubuntu has a path forward for the latest Mongo DB, but since Pi Zero is based on Raspberry Pi 1 (Arm6) and Ubuntu no longer supports it, we are limited to the version provided with Raspberry Pi OS
Create RAM Disk
For my purpose, I want to use a ram disk for Mongo DB.. At this time I don’t really want to retain any data… perhaps in the future
# create the folder
sudo mkdir /mnt/ramdisk
# create the ramdisk with 100 MB capacity
sudo mount -t tmpfs -o rw,size=100M tmpfs /mnt/ramdisk
# setup the ramdisk at boot
sudo nano /etc/fstab
# add the following line to the end
tmpfs /mnt/ramdisk tmpfs rw,size=100M 0 0
#Ctrl-O to Save
#Strl-X to Exit
Configure and Start Mongo DB
Now we need to point Mongo DB at the RAM Disk
sudo nano /etc/mongodb.conf
# change the dbpath
dbpath=/mnt/ramdisk
# Ctrl+O to save
# Ctrl+X to exit
sudo systemctl enable mongodb
sudo systemctl start mongodb
sudo systemctl status mongodb
# reboot
sudo reboot
# verify ramdisk
ls /mnt/ramdisk
# verify auto-start of mongo
sudo systemctl status mongodb
Note: The version available uses
mongodb
rather thanmongod
like is current.. I was tripped up by this when trying to start the service
Testing Mongo DB
To verify that it is all working, lets use the mongo
cli to add and query data
# query databases
db
# select the test database
use db
db.createCollection('projects');
# it seems that either single or double quotes are supported
db.projects.insert({id:'apple', name:'Apple'});
db.projects.insert({id:'banana', name:'Banana'});
# query newly created data
db.projects.find();
quit()
I connected via SSH to get the console output directly…
pi@raspberrypi:~ $ mongo
MongoDB shell version: 2.4.14
connecting to: test
Server has startup warnings:
Mon Jan 18 05:07:17.214 [initandlisten]
Mon Jan 18 05:07:17.214 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Mon Jan 18 05:07:17.215 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Mon Jan 18 05:07:17.215 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Mon Jan 18 05:07:17.215 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Mon Jan 18 05:07:17.216 [initandlisten]
> use test
switched to db test
> db.projects.find();
{ "_id" : ObjectId("60051c0dd548855d5de48b06"), "id" : "apple", "name" : "Apple" }
{ "_id" : ObjectId("60051c39d548855d5de48b07"), "id" : "banana", "name" : "Banana" }
>
…and we’re done! I am unsure of the present limitations of this version of Mongo DB, but it is intended for some local test applications only.