Skip to main content

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

FlagDescription
--dbuser=VALUEOverride DB user
--dbpass=VALUEOverride 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.

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 FixCannot Fix
Crashed MyISAM tablesSeverely corrupted InnoDB data files
Missing index files (.MYI)Deleted/overwritten database files
Incorrect row countsHardware-level disk corruption
Minor table inconsistenciesData 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

  1. Run wp db check first — only use wp db repair when there is a confirmed problem.
  2. Back up the database before repairing — wp db export backup_before_repair.sql.
  3. Follow with wp db optimize — repair can leave fragmentation that optimize cleans up.
  4. Restore from backup if repair fails — forcing a broken table will not recover lost data.
  5. Investigate root causes — repeated crashes suggest hardware or MySQL configuration issues.

Troubleshooting

ProblemCauseFix
Operation failed after repairTable severely corruptedRestore from backup
InnoDB warnings about redo logCrash recovery not completedAllow MySQL to complete crash recovery on restart
Access deniedUser lacks LOCK TABLESUse root or grant LOCK TABLES privilege
Repair report shows no issuesNo corruption was presentwp 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