A production-ready system that detects database errors in real-time, fixes them one by one automatically, and displays everything live on a beautiful web dashboard.
Uses Docker + MongoDB 4.4 for maximum CPU compatibility including older processors that lack AVX instructions.
- Features
- Why Docker + MongoDB 4.4?
- Requirements
- Project Structure
- Installation
- Quick Start
- Usage
- What Gets Detected & Fixed
- Configuration
- Dashboard
- Docker Reference
- MongoDB Shell Reference
- Troubleshooting
- Contributing
- 🔍 Real-Time Detection — Scans database every 30 seconds
- 🔧 Automatic Fixing — Processes errors one by one, safely
- 💾 Backup Before Fix — Creates a real MongoDB backup before every change
- 📊 Live Dashboard — Beautiful animated web UI at
http://localhost:5000 - 🐳 Docker Ready — Works on any CPU via Docker + MongoDB 4.4
- 🛡️ Safe Modes — Dry-run and observe-only modes built in
- 📝 Full Logging — Every action logged to
logs/realtime_healing.log - 📱 Mobile Friendly — Dashboard works on phones and tablets
- ♻️ Restore Support — Restore from any backup at any time
MongoDB 5.0 and above require AVX instructions which are missing on many older CPUs. Running MongoDB 5.0+ on such CPUs causes an immediate crash:
Illegal instruction (core dumped)
This project uses MongoDB 4.4 via Docker which works on every CPU, every machine.
MongoDB 5.0+ → requires AVX → crashes on old CPUs
MongoDB 4.4 → no AVX needed → works everywhere ✅
- OS: Linux (Ubuntu 20.04+ recommended), macOS, or Windows with WSL2
- RAM: 4 GB minimum (8 GB recommended)
- Disk: 2 GB free space
- Docker: 20.10+
- Python: 3.8+
pymongo>=4.0
flask>=3.0
Install with:
pip install -r requirements.txtpymongo>=4.0.0
flask>=3.0.0
realtime-self-healing-mongodb/
│
├── realtime_self_healing_mongodb.py # Main application — run this
├── create_test_issues.py # Creates intentional DB errors for testing
├── verify_issues.py # Scans and reports current DB issues
│
├── requirements.txt # Python dependencies
├── .gitignore # Git ignore rules
├── LICENSE # MIT License
├── README.md # This file
│
└── logs/ # Auto-created on first run
└── realtime_healing.log # All system activity logs
git clone https://github.com/YOUR_USERNAME/realtime-self-healing-mongodb.git
cd realtime-self-healing-mongodbUbuntu/Debian:
sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo systemctl enable dockermacOS:
brew install --cask dockerFix Docker permissions (Linux):
sudo usermod -aG docker $USER
# Log out and log back in, or run:
newgrp dockerdocker run -d \
--name mongodb \
--restart always \
-p 27017:27017 \
-v mongodb-data:/data/db \
mongo:4.4Verify MongoDB is running:
docker ps
# Should show: mongo:4.4 Up X minutes 0.0.0.0:27017->27017/tcpTest the connection:
docker exec -it mongodb mongo --eval "db.adminCommand('ping')"
# Expected: { "ok" : 1 }
⚠️ Note: MongoDB 4.4 usesmongoshell, notmongosh. Always use:docker exec -it mongodb mongo
# Create virtual environment (recommended)
python3 -m venv env
source env/bin/activate # Linux/macOS
# or: env\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt# 1. Start MongoDB
docker start mongodb
# 2. Activate environment
source env/bin/activate
# 3. Create test errors in the database
python create_test_issues.py
# 4. Verify the errors exist
python verify_issues.py
# 5. Start the self-healing system
python realtime_self_healing_mongodb.py
# 6. Open dashboard in browser
# http://localhost:5000
# 7. After fixing — verify everything is clean
python verify_issues.pyCreates intentional errors in MongoDB for testing purposes. Run this to populate the database with test data.
python create_test_issues.pyCreates:
- Duplicate user records (same email multiple times)
- Orphaned orders (referencing non-existent users)
- Users with missing required fields (no email / no name)
- Orders with invalid status values
- 1000 products without indexes (triggers slow query detection)
Sample output:
✅ Connected to MongoDB: myapp
🗑️ Clearing existing data...
1️⃣ Creating DUPLICATE USERS... ✅ 6 users inserted
2️⃣ Creating ORPHANED ORDERS... ✅ 5 orders inserted
3️⃣ Creating MISSING FIELDS... ✅ 4 incomplete users
4️⃣ Creating INVALID STATUS... ✅ 4 bad orders
5️⃣ Creating SLOW QUERY DATA... ✅ 1000 products
Expected issues: ~13
Scans the database and reports all detected issues. Run before fixing to see what exists, and after fixing to confirm they are gone.
python verify_issues.pySample output — before fixing:
1️⃣ DUPLICATE RECORDS
⚠️ john@example.com — appears 3 times
2️⃣ ORPHANED DOCUMENTS
⚠️ ORD-002 — references nonexistent@example.com (user missing)
3️⃣ MISSING REQUIRED FIELDS
⚠️ Charlie Brown — no email field
4️⃣ INVALID DATA VALUES
⚠️ ORD-101 — status='xyz' (valid: pending/processing/completed/cancelled)
5️⃣ MISSING INDEXES
⚠️ products — no index on 'category' (1000 documents)
📊 TOTAL ISSUES FOUND: 13
Sample output — after fixing:
1️⃣ DUPLICATE RECORDS ✅ No duplicates found
2️⃣ ORPHANED DOCUMENTS ✅ No orphaned documents found
3️⃣ MISSING REQUIRED FIELDS ✅ No missing fields found
4️⃣ INVALID DATA VALUES ✅ No invalid data found
5️⃣ MISSING INDEXES ✅ No critical missing indexes
📊 TOTAL ISSUES FOUND: 0
✅ Your database is CLEAN!
The main application. Starts the detector, fixer, and dashboard.
python realtime_self_healing_mongodb.pyWhat happens:
Thread 1 — DETECTOR (every 30 seconds)
└── Scans MongoDB for all 5 error types
└── Adds detected errors to a queue
└── Updates the live dashboard
Thread 2 — FIXER (continuous)
└── Takes one error from the queue
└── Creates a real MongoDB backup first
└── Applies the appropriate fix
└── Marks error as resolved
└── Takes the next error
Flask Server — DASHBOARD
└── Serves the live dashboard at http://localhost:5000
└── Auto-refreshes every 5 seconds
Press Ctrl+C to stop.
| Error Type | How Detected | Fix Applied | Backup Created |
|---|---|---|---|
| Duplicate Records | Aggregate $group + $match count > 1 |
Keeps first doc, deletes extras | ✅ Yes |
| Orphaned Documents | Cross-reference orders vs users | Archives to collection_orphaned |
✅ Yes |
| Missing Required Fields | $exists: false + null + "" checks |
Deletes invalid document | ✅ Yes |
| Invalid Data Values | $nin check against valid values list |
Sets field to default valid value | ✅ Yes |
| Slow Queries | MongoDB profiler, threshold 2000ms | Creates missing index | ❌ Not needed |
Before every destructive fix, a real MongoDB backup collection is created:
users_backup_20240217_103010 ← timestamp: YYYYMMDD_HHMMSS
orders_backup_20240217_103015
To restore from backup:
// Inside MongoDB shell
use myapp
db.users_backup_20240217_103010.find().forEach(doc => db.users.insertOne(doc))Edit the CONFIG dictionary at the top of realtime_self_healing_mongodb.py:
CONFIG = {
# MongoDB
'MONGODB_URI': 'mongodb://localhost:27017',
'DATABASE_NAME': 'myapp',
# Detection
'REALTIME_CHECK_INTERVAL': 30, # Seconds between scans
'CHECK_DUPLICATES': True,
'CHECK_ORPHANED_DOCS': True,
'CHECK_MISSING_FIELDS': True,
'CHECK_DATA_CONSISTENCY': True,
'CHECK_SLOW_QUERIES': True,
# Fixing
'AUTO_FIX_ENABLED': True, # Enable automatic fixing
'DRY_RUN': False, # False = make real changes
'BACKUP_BEFORE_FIX': True, # Always backup before fixing
'FIX_DELAY': 2, # Seconds between fixes
# Dashboard
'DASHBOARD_ENABLED': True,
'DASHBOARD_PORT': 5000,
'DASHBOARD_HOST': '0.0.0.0',
}| Mode | AUTO_FIX_ENABLED |
DRY_RUN |
Effect |
|---|---|---|---|
| Observe only | False |
True |
Detects and shows errors — no changes |
| Simulate | True |
True |
Processes queue, logs what would happen — no changes |
| Live fixing | True |
False |
Detects, backs up, and actually fixes ✅ |
'REALTIME_CHECK_INTERVAL': 120, # Check every 2 minutes
'FIX_DELAY': 5, # 5 seconds between fixesOpen http://localhost:5000 in any browser after starting the system.
| Card | Description |
|---|---|
| DB Status | Live connection indicator |
| Errors Detected | Total errors found |
| Errors Fixed | Total errors resolved |
| In Queue | Errors waiting to be fixed |
| Fixer Status | Idle / Fixing |
| Total Checks | Detection cycles completed |
- Currently Fixing — Shows the active fix with animated progress bar
- Errors Detected — Live list, colour-coded by severity (red / yellow / blue)
- Errors Fixed — Green list with fix timestamps
- Activity Log — Last 50 system events
hostname -I # Get your machine's IP addressThen open http://YOUR_IP:5000 from any device on the same network.
# Start container
docker start mongodb
# Stop container
docker stop mongodb
# Restart container
docker restart mongodb
# Check status
docker ps
# Check all containers (including stopped)
docker ps -a
# View logs
docker logs mongodb
docker logs -f mongodb # Live/follow logs
# Enter MongoDB shell (use 'mongo' for version 4.4)
docker exec -it mongodb mongo
# Run single command without entering shell
docker exec -it mongodb mongo --eval "db.adminCommand('ping')"
# Resource usage
docker stats mongodb
# Remove container (data is safe in the named volume)
docker stop mongodb && docker rm mongodb# Enter shell
docker exec -it mongodb mongo// Switch to database
use myapp
// List all collections (real + backups)
show collections
// Document counts
db.users.countDocuments()
db.orders.countDocuments()
db.products.countDocuments()
// Find duplicates
db.users.aggregate([
{ $group: { _id: "$email", count: { $sum: 1 } } },
{ $match: { count: { $gt: 1 } } }
])
// Find orphaned orders
db.orders.find({
user_email: { $nin: db.users.distinct("email") }
})
// Find invalid statuses
db.orders.find({
status: { $nin: ["pending","processing","completed","cancelled"] }
})
// View backup documents
db.users_backup_20240217_103010.find().pretty()
// Restore from backup
db.users_backup_20240217_103010.find().forEach(doc => db.users.insertOne(doc))
// Drop a backup collection
db.users_backup_20240217_103010.drop()
// Exit
exitCause: CPU lacks AVX instructions required by MongoDB 5.0+ Fix: Use MongoDB 4.4 via Docker (already handled by this project)
sudo usermod -aG docker $USER
newgrp docker # Apply without logout
# or rebootdocker ps -a # Check container exists
docker start mongodb # Start it
sleep 3 # Wait for startup
python realtime_self_healing_mongodb.pydocker run -d --name mongodb --restart always \
-p 27017:27017 -v mongodb-data:/data/db mongo:4.4MongoDB 4.4 uses mongo, not mongosh:
# Wrong
docker exec -it mongodb mongosh
# Correct ✅
docker exec -it mongodb mongopip install flask # Ensure Flask is installed
sudo lsof -i :5000 # Check port 5000 is free
# If busy, change port in CONFIG:
# 'DASHBOARD_PORT': 8080Ensure all three settings are enabled in CONFIG:
'AUTO_FIX_ENABLED': True,
'DRY_RUN': False,
'BACKUP_BEFORE_FIX': True,'REALTIME_CHECK_INTERVAL': 120, # Less frequent scans
'FIX_DELAY': 5, # More time between fixestail -f logs/realtime_healing.log
grep "ERROR" logs/realtime_healing.log
grep "Backup" logs/realtime_healing.log
grep "Fixed" logs/realtime_healing.loggit clone → install docker → run mongo:4.4 → pip install
│
▼
python create_test_issues.py ← creates ~13 errors
│
▼
python verify_issues.py ← confirms: 13 issues found
│
▼
python realtime_self_healing_mongodb.py
│
├── http://localhost:5000 ← watch live dashboard
│
▼
python verify_issues.py ← confirms: 0 issues found ✅
Contributions are welcome!
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Commit your changes:
git commit -m 'Add my feature' - Push to the branch:
git push origin feature/my-feature - Open a Pull Request
Ideas for contributions:
- Add email/Slack notifications
- Support for PostgreSQL or MySQL
- Machine learning for error prediction
- Historical trend charts on the dashboard
- Docker Compose setup
⭐ If this project helped you, please give it a star on GitHub!