Open In App

How to Install MongoDB on Raspberry pi?

Last Updated : 01 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

This guide provides a comprehensive overview of installing and configuring MongoDB on the Raspberry Pi, a popular single-board computer known for its versatility and affordability. It covers essential prerequisites, installation steps for Ubuntu and MongoDB, securing MongoDB for enhanced data protection, and making MongoDB accessible across networks.

Install & Configure MongoDB on the Raspberry Pi

The Raspberry Pi is a series of compact single-board computers created by the Raspberry Pi Foundation, a UK-based charitable organization. Designed initially to promote the teaching of basic computer science in schools and developing countries. The Raspberry Pi has become popular for various applications due to its low cost, versatility, and extensive community support.

MongoDB is a popular, open-source, NoSQL database management system designed to handle large volumes of unstructured data. It differs from traditional relational databases in that it stores data in flexible, JSON-like documents rather than tables with rows and columns. This structure allows MongoDB to be more versatile and scalable, making it suitable for a wide range of applications, from small startups to large enterprise systems

Prerequisites

Before starting, make sure you have the following:

  • A Raspberry Pi (preferably a Pi 3 or later model for better performance)
  • A microSD card (16GB or larger)
  • A stable internet connection
  • An external power source for the Raspberry Pi
  • A monitor, keyboard, and mouse for the initial setup

Things Not To Do

To ensure a smooth installation and operation, avoid the following:

  • Using a Raspberry Pi model older than Pi as it may not provide sufficient performance for MongoDB
  • Installing MongoDB on a heavily used or unreliable microSD card, can lead to data corruption
  • Running MongoDB without a sufficient power supply can cause instability

Installing Ubuntu

  1. Download Ubuntu: Visit the official Ubuntu website and download the latest Ubuntu image for Raspberry Pi.
  2. Flash the SD card: Use software like Balena Etcher to flash the downloaded image onto your microSD card.
  3. Initial setup: Insert the microSD card into your Raspberry Pi, connect it to your monitor, keyboard, and mouse, and power it up. Follow the on-screen prompts to complete the Ubuntu setup process..

Install MongoDB

1. Update system package

sudo apt update
sudo apt upgrade -y

2. Install dependencies

Add the MongoDB repository and install it using the following commands

sudo apt install -y gnupg

3. Add the MongoDB repository

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=arm64,armhf ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

4. Install MongoDB

sudo apt update
sudo apt install -y mongodb-org

Run MongoDB

1. Start the MongoDB service

sudo systemctl start mongod

2. Enable MongoDB to start on boot

sudo systemctl enable mongod

3. Verify the installation

Check the MongoDB service status to ensure it is running correctly:

sudo systemctl status mongod

Securing MongoDB

1. Create an admin user

mongo
use admin
db.createUser({
user: "admin",
pwd: "password",
roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
})
exit

2. Enable authentication

Edit the MongoDB configuration file (/etc/mongod.conf) and enable authorization by adding the following line under the security section:

security:
authorization: "enabled"

3. Restart MongoDB to apply the changes

sudo systemctl restart mongod

Make MongoDB Available to Your Network

1. Allow remote connections

Edit the MongoDB configuration file (/etc/mongod.conf) and bind MongoDB to your network interface by modifying the bindIp setting:

net:
bindIp: 0.0.0.0

2. Restart MongoDB

sudo systemctl restart mongod

3. Configure firewall rules

If you have a firewall enabled, make sure to allow connections on MongoDB’s default port (27017):

sudo ufw allow 27017

Conclusion

Installing and configuring MongoDB on a Raspberry Pi can be a cost-effective way to learn and experiment with this powerful NoSQL database. By following this guide, you have set up a secure MongoDB instance that can be accessed remotely, making it suitable for various small-scale projects and educational purposes.



Next Article

Similar Reads