raj
Full Stack Developer

Command Palette

Search for a command to run...

Install MongoDB on Debian 12 VPS: Complete Guide to Self-Hosting for Cost Savings

Install MongoDB on Debian 12 VPS: Complete Guide to Self-Hosting for Cost Savings

Step-by-step guide to install MongoDB 7.0 on Debian 12 VPS with low memory configuration, remote access setup, authentication, and cost-saving tips.

DevOps ToolsCloud AutomationVPS

Published on: Friday, Jan 9, 2026


Table of contents
Why Self-Host MongoDB?
Prerequisites
Step 1: Install MongoDB 7.0 on Debian 12
Step 2: Configure MongoDB for Low Memory (Critical!)
Step 3: Create Log Directory (Easy to Miss!)
Step 4: Start MongoDB
Step 5: Enable Remote Access (The bindIp Trap)
Step 6: Set Up Authentication (Don't Skip This!)
Step 7: Your Connection String
Memory Usage Results
Production Notes for Larger VPS
FAQs - Issues I Encountered
Q: MongoDB won't start with "cacheSizeGB must be >= 0.25" error
Q: Getting "No such file or directory" for /var/log/mongodb/mongod.log
Q: Connection refused when connecting remotely
Q: SSH tunnel vs direct connection - which is better?
Q: How do I verify MongoDB is listening on the right IP?
Q: What's the minimum VPS spec for MongoDB?
Conclusion
  • Related posts
  • facebooktwitterlinkedinwhatsapp

    Looking to reduce your cloud database costs or gain more control over your data? Self-hosting MongoDB on a VPS is a compelling option. This guide walks you through installing MongoDB 7.0 on Debian 12, configuring it for low-memory environments, enabling remote access, and setting up authentication—complete with solutions to common issues you might encounter.

    Why Self-Host MongoDB?

    Managed database services like MongoDB Atlas are convenient for getting started, but self-hosting gives you full control and can significantly reduce costs—especially for development, testing, or smaller production workloads. A budget VPS can handle MongoDB surprisingly well with the right configuration.

    Prerequisites

    • A Debian 12 (Bookworm) VPS with SSH access
    • Root or sudo privileges
    • At least 512MB RAM (we'll configure for low memory)
    • Basic terminal knowledge

    Step 1: Install MongoDB 7.0 on Debian 12

    First, let's add the official MongoDB repository and install the database server. SSH into your VPS and run:

    Step 2: Configure MongoDB for Low Memory (Critical!)

    This is a common roadblock. By default, MongoDB's WiredTiger engine uses 50% of available RAM for cache. On a low-memory VPS, that's problematic. Edit the configuration file:

    Add or modify the storage section. Important: The minimum cacheSizeGB is 0.25 (256MB). I initially tried 0.1 and MongoDB refused to start!

    Step 3: Create Log Directory (Easy to Miss!)

    Another issue I encountered: MongoDB wouldn't start because the log directory didn't exist. Fix it:

    Step 4: Start MongoDB

    You should see "active (running)" in green. If not, check the logs:

    Step 5: Enable Remote Access (The bindIp Trap)

    By default, MongoDB only listens on localhost (127.0.0.1). When I tried connecting from my development machine, I got:

    The fix: Update bindIp in /etc/mongod.conf:

    Then restart MongoDB and open the firewall:

    Step 6: Set Up Authentication (Don't Skip This!)

    With the port open to the internet, you MUST enable authentication. First, create an admin user:

    Now enable authentication in mongod.conf:

    Step 7: Your Connection String

    You can now connect from anywhere using this connection string format:

    Replace YOUR_VPS_IP with your server's public IP address. Test with mongosh:

    Memory Usage Results

    Here's typical resource usage on a 512MB RAM VPS after setup:

    ResourceUsage
    MongoDB Memory~71MB
    Available RAM~279MB
    Swap Used~74MB
    Total Documents2729
    Database Size15MB

    The VPS handles development workloads without any issues. For production with larger datasets, you'll want at least 2-4GB RAM.

    Production Notes for Larger VPS

    When moving to production with a beefier VPS, consider these additional hardening steps:

    • Enable TLS/SSL - Encrypt all connections with certificates
    • IP Whitelisting - Restrict bindIp to known application servers only
    • Automated Backups - Set up daily mongodump with cron jobs
    • Increase Cache Size - Set cacheSizeGB to 50% of available RAM
    • Disable Transparent Huge Pages - MongoDB recommends this for performance
    • Role-Based Users - Create separate users for app, backup, and admin tasks

    FAQs - Issues I Encountered

    Q: MongoDB won't start with "cacheSizeGB must be >= 0.25" error

    A: The WiredTiger storage engine requires a minimum cache of 256MB (0.25GB). You cannot set cacheSizeGB lower than this value, even on low-memory systems. If your VPS has less than 512MB RAM, MongoDB will still work but may use swap.

    Q: Getting "No such file or directory" for /var/log/mongodb/mongod.log

    A: The log directory doesn't exist by default. Create it with: sudo mkdir -p /var/log/mongodb && sudo chown mongodb:mongodb /var/log/mongodb

    Q: Connection refused when connecting remotely

    A: Check two things: 1) Is bindIp set to 0.0.0.0 in mongod.conf? The default 127.0.0.1 only allows local connections. 2) Is your firewall allowing port 27017? Use: sudo ufw allow 27017/tcp

    Q: SSH tunnel vs direct connection - which is better?

    A: SSH tunnels are more secure (MongoDB port stays closed) but require maintaining the tunnel. Direct connection with authentication + TLS is simpler for production. For development testing, direct connection with strong passwords is acceptable.

    Q: How do I verify MongoDB is listening on the right IP?

    A: Run: ss -tlnp | grep 27017. You should see 0.0.0.0:27017 for remote access, not 127.0.0.1:27017.

    Q: What's the minimum VPS spec for MongoDB?

    A: For testing/development: 512MB RAM works fine with small datasets (<50MB). For production: 2GB+ RAM minimum, 4GB+ recommended for datasets over 1GB.

    Conclusion

    Self-hosting MongoDB on a budget VPS is absolutely viable for development and testing workloads. The key learnings: always configure cacheSizeGB for low-memory systems, remember to change bindIp for remote access, and never skip authentication. For roughly $5-10/month, you get full control over your database—a fraction of managed service costs.

    Stay Updated

    Get the latest updates and insights directly to your inbox.

    Related posts
    How to Install WordPress on a VPS with Ubuntu, PHP 8.3, MariaDB, and Caddy

    How to Install WordPress on a VPS with Ubuntu, PHP 8.3, MariaDB, and Caddy

    First Things to Do After Buying a VPS: A Complete Setup Guide

    First Things to Do After Buying a VPS: A Complete Setup Guide

    Migrate MongoDB Atlas to Self-Hosted VPS: Complete Data Migration Guide

    Migrate MongoDB Atlas to Self-Hosted VPS: Complete Data Migration Guide

    The Ultimate Guide to Choosing and Securing Your VPS in 2025

    The Ultimate Guide to Choosing and Securing Your VPS in 2025

    EmailLinkedinGithub