wp db repair
Overview
Fix corrupted MySQL/MariaDB tables in the WordPress database. The first recovery tool to reach for when wp db check reports crashed or corrupted tables.
What It Does
wp db repair runs mysqlcheck --repair on all WordPress tables. It attempts to fix index inconsistencies, corrupt data files, and crashed tables — restoring them to a working state without data loss (when possible).
Syntax
wp db repair [--dbuser=<value>] [--dbpass=<value>]
Options & Flags
| Flag | Description |
|---|---|
--dbuser=VALUE | Override DB user |
--dbpass=VALUE | Override DB password |
Basic Usage
Repair all tables
wp db repair
Output on success:
Success: Database repaired.
Output when repair is needed:
wordpress.wp_posts OK
wordpress.wp_postmeta
warning : Number of rows changed from 2345 to 2344
status : OK
wordpress.wp_options
error : Incorrect information in file: './wordpress/wp_options.MYI'
error : Corrupt
status : Operation failed
Error: Database repair failed.
When repair fails
If wp db repair cannot fix a table, the data file may be too severely corrupted. In that case, restore from a backup with wp db import.
Recommended Workflow: Check → Repair → Optimize
Always use these three commands together:
# Step 1: Diagnose
wp db check
# Step 2: Fix
wp db repair
# Step 3: Optimize (clean up fragments after repair)
wp db optimize
# Step 4: Verify
wp db check && echo "Database is healthy ✅"
Real-World Scenarios
Scenario 1: Emergency recovery after sudden server crash
# Server was killed mid-write — some tables are crashed
wp db check
# Attempt repair
wp db repair
# Verify repair succeeded
wp db check
# If still failing — restore from backup
# wp db reset --yes && wp db import latest_backup.sql
Scenario 2: Automated incident response
#!/bin/bash
if ! wp db check --quiet 2>/dev/null; then
echo "DB check failed — attempting repair..."
wp db repair
if ! wp db check --quiet 2>/dev/null; then
echo "Repair failed — restoring from backup..."
BACKUP=$(ls -t /var/backups/wp/*.sql | head -1)
wp db reset --yes
wp db import "${BACKUP}"
else
wp db optimize
echo "DB repaired and optimized ✅"
fi
fi
Scenario 3: Post-hosting migration table issues
# Some tables may not have transferred correctly
wp db check
# Repair any issues
wp db repair
# Verify the site works properly
wp core is-installed && echo "Site is intact ✅"
What Repair Can and Cannot Fix
| Can Fix | Cannot Fix |
|---|---|
| Crashed MyISAM tables | Severely corrupted InnoDB data files |
Missing index files (.MYI) | Deleted/overwritten database files |
| Incorrect row counts | Hardware-level disk corruption |
| Minor table inconsistencies | Data loss from a crashed write |
InnoDB limitations
InnoDB tables rarely need REPAIR TABLE. If an InnoDB table is corrupt, it usually indicates a more serious issue. Restore from backup in that case.
Best Practices
- Run
wp db checkfirst — only usewp db repairwhen there is a confirmed problem. - Back up the database before repairing —
wp db export backup_before_repair.sql. - Follow with
wp db optimize— repair can leave fragmentation that optimize cleans up. - Restore from backup if repair fails — forcing a broken table will not recover lost data.
- Investigate root causes — repeated crashes suggest hardware or MySQL configuration issues.
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
Operation failed after repair | Table severely corrupted | Restore from backup |
| InnoDB warnings about redo log | Crash recovery not completed | Allow MySQL to complete crash recovery on restart |
Access denied | User lacks LOCK TABLES | Use root or grant LOCK TABLES privilege |
| Repair report shows no issues | No corruption was present | wp db check may have been a false positive |
Quick Reference
wp db check # Diagnose first
wp db repair # Fix issues
wp db optimize # Clean up after repair
wp db export backup.sql # Always back up before repair
Next Steps
wp db check— diagnose before repairing.wp db optimize— optimize after repair.wp db import— restore from backup if repair fails.