DEV Community

Cover image for How to self-host MongoDB on a Raspberry Pi 4
The Smartbug
The Smartbug

Posted on • Originally published at thesmartbug.com

How to self-host MongoDB on a Raspberry Pi 4

MongoDB Server

MongoDB is an open-source, NoSQL database management system designed to handle large volumes of data and provide high performance, scalability, and flexibility. Introduced in 2009 by Dwight Merriman, Eliot Horowitz, and Kevin Ryan, the co-founders of the company 10gen (later renamed MongoDB, Inc.), MongoDB emerged as a solution to the limitations and challenges posed by traditional relational databases.

Motivation behind

The primary motivation behind MongoDB's creation was the need to manage and store the vast amounts of unstructured and semi-structured data generated by modern applications. Traditional relational databases, which use fixed schemas and tables, struggled to efficiently handle the diverse and evolving data structures typical of contemporary web applications, big data, and real-time analytics.

MongoDB uses a document-oriented model, storing data in flexible, JSON-like documents. This schema-less design allows developers to easily adapt to changing data requirements without costly schema migrations. Additionally, MongoDB's architecture supports horizontal scaling through sharding, enabling it to manage large-scale, distributed data across multiple servers seamlessly.

By providing a more dynamic and scalable approach to data management, MongoDB has become a popular choice for organizations looking to build applications that require real-time processing, complex querying, and rapid iteration.

Self-host MongoDB setup

To setup instance of the MongoDB server on a Raspi4, we can use the following docker compose file

version: '2'

services:
  mongodb:

    # For Development / Testing
    image: mongodb/mongodb-community-server:4.4.3-ubuntu2004

    # For Production Deployment
    # image: mongodb/mongodb-enterprise-server:4.4.3-ubuntu2004

    environment:
      MONGODB_INITDB_ROOT_USERNAME: <DB_USERNAME>
      MONGODB_INITDB_ROOT_PASSWORD: <DB_PASSWORD>
    restart: unless-stopped
    ports:
      - 27017:27017
    volumes:
      - mongodb_data:/data/db

volumes:
  mongodb_data:
    driver: local

Enter fullscreen mode Exit fullscreen mode

Note

It is extremely important to note that the latest versions of the docker images will NOT work on the Raspberry Pi 4. It is because the architecture of Raspi is arm64/v8 and there are many new additions in the architecute arm64/v8.2 that are absent on the Raspberry pi.
More details here and here.

Save the above docker-compose config in some file (say mongo-docker-compose.yaml), and run the following cmd

docker-compose -d mongo-docker-compose.yaml up
Enter fullscreen mode Exit fullscreen mode

The -d flag instructs the command to run in a detached mode, in background.
After testing many different Docker images, and configurations, finally the above docker-compose file has been prepared. This has been proven to work on raspberry pi 4.

Conclusion

You can use your own self-hosted instance of MongoDB without any restriction and allocate as much data as you like. It is an easy to use, and easy to setup Database. It only takes 5 minutes of your time if you follow this guide.

Feel free to post in the comments below if something is unclear or if you've any suggestion. I would be very glad to read your comments.

Also, any feedback is most welcome


Original Article published at The SmartBug

Top comments (0)