MongoDB on UK VPS: Complete Setup Guide for Production 2026
TL;DR: Deploy MongoDB on a UK VPS in under 30 minutes. This guide covers installation, production hardening, backups, monitoring, and migration — all optimized for UK-based infrastructure.
---
Why MongoDB on a UK VPS?
UK developers choose MongoDB for its flexible document model, horizontal scaling, and rich query language. But where you host it matters:
- Latency: A UK VPS means your database is physically close to your UK user base — critical for sub-10ms queries
- Data residency: Keep customer data within UK borders (UK GDPR, Data Protection Act 2018)
- Egress costs: Cloud MongoDB (Atlas) charges egress at $0.09–$0.12/GB. A UK VPS with unmetered bandwidth eliminates that surprise
- Control: Full root access to tune MongoDB for your workload
At Hostingowy, we see an increasing number of UK startups migrating their MongoDB workloads from Atlas to their own VPS — typically saving 50–70% on database costs while maintaining performance.
Prerequisites
- A UK VPS running Ubuntu 22.04 LTS (or Debian 12)
- 2GB+ RAM (4GB recommended for production)
- 20GB+ NVMe storage (adjust based on your data size)
- Root access via SSH
- A non-root sudo user set up
> Using Hostingowy? Deploy a VPS in under 60 seconds from our control panel. All plans come with NVMe storage, UK data centres, and unmetered bandwidth.
Step 1: Connect to Your VPS
ssh your-user@your-vps-ip
Update the system:
sudo apt update && sudo apt upgrade -y
Step 2: Install MongoDB 7.0
MongoDB 7.0 is the latest stable release as of 2026. Install it from the official repository:
# Import MongoDB GPG key
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
Add MongoDB repository (Ubuntu 22.04)
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Update and install
sudo apt update
sudo apt install -y mongodb-org
Step 3: Configure MongoDB for Production
Set Up Data and Log Directories
sudo mkdir -p /data/db /var/log/mongodb
sudo chown -R mongodb:mongodb /data/db /var/log/mongodb
Edit the MongoDB Configuration
Open the config file:
sudo nano /etc/mongod.conf
Here's a production-ready configuration:
# mongod.conf — Production configuration for UK VPS
storage:
dbPath: /data/db
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 1.5 # ~40% of available RAM
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
logRotate: reopen
net:
port: 27017
bindIp: 127.0.0.1 # Only localhost — secure by default
processManagement:
timeZoneInfo: /usr/share/zoneinfo/Europe/London
security:
authorization: enabled # Enable authentication
> Security note: Setting bindIp: 127.0.0.1 means MongoDB only accepts connections from the local machine. Your application connects over the local network. Never expose MongoDB directly to the internet.
Start MongoDB
sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod
Step 4: Create Admin User
Connect to MongoDB shell:
mongosh
Switch to the admin database and create a root user:
use admin
db.createUser({
user: "admin",
pwd: passwordPrompt(), // Enter a strong password
roles: [
{ role: "userAdminAnyDatabase", db: "admin" },
{ role: "readWriteAnyDatabase", db: "admin" }
]
})
Exit and test authentication:
mongosh -u admin -p --authenticationDatabase admin
Step 5: Create Application Database and User
use your_app_database
db.createUser({
user: "app_user",
pwd: passwordPrompt(),
roles: [
{ role: "readWrite", db: "your_app_database" }
]
})
Step 6: Configure Firewall (UFW)
MongoDB only needs local access, but ensure your firewall is active:
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status
Step 7: Set Up Backups
Automated Daily Backups with mongodump
Create a backup script:
sudo nano /usr/local/bin/mongodb-backup.sh
#!/bin/bash
MongoDB Backup Script — stores last 7 days of backups
BACKUP_DIR="/backups/mongodb"
DATE=$(date +%Y-%m-%d)
RETENTION_DAYS=7
mkdir -p "$BACKUP_DIR/$DATE"
mongodump \
--username admin \
--password "$MONGO_PASSWORD" \
--authenticationDatabase admin \
--out "$BACKUP_DIR/$DATE"
Compress
tar -czf "$BACKUP_DIR/mongodb-$DATE.tar.gz" -C "$BACKUP_DIR" "$DATE"
rm -rf "$BACKUP_DIR/$DATE"
Remove backups older than retention period
find "$BACKUP_DIR" -name "mongodb-*.tar.gz" -mtime +$RETENTION_DAYS -delete
echo "Backup completed: $BACKUP_DIR/mongodb-$DATE.tar.gz"
Make it executable and set up a cron job:
sudo chmod +x /usr/local/bin/mongodb-backup.sh
Add to crontab — runs daily at 2 AM
echo "0 2 * root /usr/local/bin/mongodb-backup.sh" | sudo tee /etc/cron.d/mongodb-backup
> Pro tip: For critical data, also send backups to a separate UK-based location or S3-compatible storage.
Step 8: Set Up Monitoring
Enable MongoDB Monitoring with mongostat
# Install monitoring tools
sudo apt install -y mongodb-mongosh
Monitor in real-time
mongostat --username admin --password --authenticationDatabase admin
Prometheus + MongoDB Exporter (Production Monitoring)
For full observability, install the MongoDB Prometheus exporter:
# Download the exporter
wget https://github.com/percona/mongodb_exporter/releases/latest/download/mongodb_exporter-linux-amd64.tar.gz
tar xzf mongodb_exporter-linux-amd64.tar.gz
sudo mv mongodb_exporter /usr/local/bin/
Create a monitoring user in MongoDB
mongosh -u admin -p --authenticationDatabase admin
use admin
db.createUser({
user: "monitor",
pwd: passwordPrompt(),
roles: [{ role: "clusterMonitor", db: "admin" }]
})
# Run the exporter
mongodb_exporter \
--mongodb.uri="mongodb://monitor:password@localhost:27017/admin" \
--web.listen-address=":9216"
Pair this with Prometheus and Grafana on your VPS for a full monitoring dashboard.
Step 9: Performance Tuning
WiredTiger Cache Size
The cache size is the single most important MongoDB performance setting. Set it to 40–50% of your available RAM:
# In /etc/mongod.conf
storage:
wiredTiger:
engineConfig:
cacheSizeGB: 2 # For a 4GB VPS
Index Optimization
Ensure your queries use indexes. Check for slow queries:
mongosh -u admin -p --authenticationDatabase admin
// Enable profiling for queries taking longer than 100ms
use your_app_database
db.setProfilingLevel(1, { slowms: 100 })
// Check slow queries
db.system.profile.find().sort({ $natural: -1 }).limit(10).pretty()
Connection Pooling
Configure your application's MongoDB driver for optimal connection pooling:
Node.js example:
const { MongoClient } = require('mongodb');
const client = new MongoClient('mongodb://app_user:password@localhost:27017/your_db', {
maxPoolSize: 25, // Adjust based on workload
minPoolSize: 5,
maxIdleTimeMS: 30000,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000
});
Step 10: Migrate from MongoDB Atlas to Your UK VPS
If you're currently on MongoDB Atlas, here's how to migrate:
1. Export from Atlas
# Dump all databases from Atlas
mongodump \
--uri="mongodb+srv://:@.mongodb.net" \
--out ./atlas-backup
2. Import to Your UK VPS
# Restore to your local MongoDB
mongorestore \
--username admin \
--password "$MONGO_PASSWORD" \
--authenticationDatabase admin \
./atlas-backup
3. Update Connection Strings
Update your application's MONGO_URI from the Atlas connection string to:
mongodb://app_user:password@localhost:27017/your_app_database
4. Validate and Cutover
- Run
mongoshto verify data integrity - Run your application's test suite
- Switch DNS if needed
Cost Comparison: UK VPS vs MongoDB Atlas
| Component | MongoDB Atlas (M10) | UK VPS (4GB, 2 vCPU) |
|---|---|---|
| Monthly cost | $57/mo (~£45) | ~£7/mo |
| Storage | 10GB NVMe | 50GB NVMe |
| Data transfer | 1GB/day free, then $0.09/GB | Unmetered |
| Data residency | Choose region (UK available) | UK data centre guaranteed |
| Management | Fully managed | Self-managed (but you have full control) |
| Annual cost | ~£540 | ~£84 |
Savings: ~85% annually by hosting MongoDB on your own UK VPS.
Security Checklist
- ✅ MongoDB bound to localhost only (127.0.0.1)
- ✅ Authentication enabled
- ✅ Firewall configured (UFW)
- ✅ Automatic backups configured
- ✅ TLS/SSL for any remote connections
- ✅ Regular security updates applied
- ✅ Monitoring enabled for anomaly detection
Troubleshooting Common Issues
MongoDB Won't Start
# Check logs
sudo journalctl -u mongod -n 50
Check permissions
sudo chown -R mongodb:mongodb /data/db
Check disk space
df -h
Connection Refused
# Check if MongoDB is running
sudo systemctl status mongod
Check bind IP
sudo grep bindIp /etc/mongod.conf
Check firewall
sudo ufw status
Slow Queries
- Check
system.profilefor slow operations - Ensure indexes exist on queried fields
- Increase WiredTiger cache size if swap is being used
Next Steps
- Set up replication with a secondary node for high availability
- Configure TLS/SSL for encrypted connections
- Implement monitoring alerts with Prometheus + Alertmanager
- Create automated backup verification (restore to a test environment weekly)
Related Resources
- Node.js Deployment on UK VPS
- Docker Hosting on UK VPS
- VPS Security Hardening in 10 Minutes
- UK VPS vs US Cloud: Cost Comparison
---
Deploy your MongoDB-backed application on a UK VPS with Hostingowy. First month free with code LAUNCH100. Get started →