keepcalculator

06 Numbers and Powers

Repair a Corrupted WordPress Database Safely

Card No. 06·72352 · Updated · Standard formula, shown below

Database corruption in WordPress can cause errors like “Error establishing a database connection,” blank pages, or failed logins. This guide walks you through safe, step-by-step methods to repair a corrupted database using built-in WordPress tools, phpMyAdmin, and WP-CLI. You’ll need access to your hosting control panel or FTP/SFTP, and a recent backup of your database.

What You’ll Need

  • Access to your hosting control panel (cPanel, Plesk, or similar)
  • phpMyAdmin or another database management tool
  • FTP/SFTP credentials or file manager in your hosting panel
  • A recent backup of your WordPress database (critical for safety)
  • Optional: WP-CLI access if your host supports it

Step-by-Step

  1. Backup your database. Before any repair, export a fresh copy of your database. In phpMyAdmin, select your database, click “Export,” choose “Quick” method, and download the SQL file. Store it securely. If you can’t access phpMyAdmin, use a plugin like UpdraftPlus (if your site is still partially working) or ask your host to provide a backup.
  2. Identify the corruption. Common signs include: “Error establishing a database connection,” “One or more database tables are unavailable,” or “The database is corrupt.” Check your WordPress debug.log (in /wp-content/) if enabled, or view the site’s error logs in your hosting panel.
  3. Enable WordPress’s built-in repair mode. Add the following line to your wp-config.php file (just above the “That’s all, stop editing!” line):
    define('WP_ALLOW_REPAIR', true);
    Then visit https://yourdomain.com/wp-admin/maint/repair.php. You’ll see two options: “Repair Database” and “Repair and Optimize Database.” Start with “Repair Database.” This runs MySQL’s REPAIR TABLE command on all tables. After it finishes, remove the line from wp-config.php immediately – leaving it enabled is a security risk.
  4. Use phpMyAdmin to repair specific tables. If the built-in repair didn’t work, log into phpMyAdmin, select your WordPress database, and check the “Structure” tab. Look for tables marked “in use” or with an error. Select the problematic tables (checkboxes), then from the “With selected” dropdown choose “Repair table.” This runs REPAIR TABLE manually. Note: this only works for MyISAM tables; InnoDB tables use a different recovery process.
  5. For InnoDB tables, use a different approach. If your tables are InnoDB (the default in modern MySQL), the REPAIR command may not work. Instead, try:

    • In phpMyAdmin, select the database, go to “Operations” and click “Optimize table” (this can fix some issues).
    • If that fails, you may need to restore from backup or use mysqlcheck from the command line.
  6. Run mysqlcheck via SSH (if available). If you have SSH access, you can run:
    mysqlcheck -u username -p --auto-repair databasename
    Replace username and databasename with your actual credentials. This checks all tables and repairs any that are corrupted. For InnoDB, you can add --force to attempt recovery.
  7. Use WP-CLI (if installed). If your host supports WP-CLI, run:
    wp db repair
    This runs the same repair commands as the built-in tool but from the command line. It’s safe and often faster.
  8. Restore from backup if repairs fail. If none of the above works, you’ll need to restore your database from the backup you made in step 1. In phpMyAdmin, import the SQL file you downloaded. If your backup is large, you may need to increase the upload limits or use a tool like BigDump.
  9. Verify the site works. After any repair or restore, visit your site’s front end and wp-admin. Check that posts, pages, and settings are intact. If you used the built-in repair, remember to remove the WP_ALLOW_REPAIR constant from wp-config.php.

Common Mistakes

  • Not backing up first. Repair attempts can sometimes worsen corruption. Always have a fresh backup.
  • Leaving the repair constant enabled. Forgetting to remove define('WP_ALLOW_REPAIR', true); exposes your site to unauthorized repair attempts. Remove it immediately after use.
  • Repairing InnoDB tables with REPAIR. The REPAIR command only works on MyISAM tables. For InnoDB, use OPTIMIZE or restore from backup.
  • Using the wrong database name. Double-check that you’re working on the correct database – accidentally repairing the wrong one can cause data loss.
  • Ignoring error messages. If a repair returns an error, don’t ignore it. It may indicate a deeper issue like disk full or insufficient permissions.

Tips

  • Schedule regular backups – use a plugin or host-level backup so you always have a recent restore point.
  • Keep WordPress, themes, and plugins updated to reduce the chance of corruption.
  • If you’re not comfortable with phpMyAdmin, ask your host’s support to run a repair for you – many hosts offer this as a free service.
  • After repairing, run a database optimization to clean up overhead and improve performance.
  • Consider switching to InnoDB if you’re on MyISAM – it’s more robust and less prone to corruption.

Troubleshooting

If the repair doesn’t go as planned:

  • “Error establishing a database connection” persists. Check your wp-config.php database credentials (DB_NAME, DB_USER, DB_PASSWORD, DB_HOST). Ensure the host is correct – sometimes it’s “localhost” but your host may use a different hostname.
  • “Table doesn’t exist” error. This often means the table was dropped or never created. Restore from backup or use a plugin like “WordPress Database Repair” to recreate missing tables.
  • Repair times out. If the repair process times out, try increasing PHP’s max execution time in php.ini (e.g., set max_execution_time = 300) or use WP-CLI which doesn’t have that limit.
  • Site still broken after restore. Clear your browser cache and any caching plugins. Also check that the database prefix in wp-config.php matches the tables in your backup.
  • Can’t access phpMyAdmin. Use the file manager in your hosting panel to edit wp-config.php and enable repair mode, or contact your host for assistance.

Frequently Asked Questions

What causes WordPress database corruption?

Common causes include abrupt server shutdowns, plugin conflicts, incomplete updates, or issues with the MySQL server itself. Sometimes it’s just bad luck.

Can I repair a corrupted database without phpMyAdmin?

Yes. You can enable WordPress’s built-in repair mode by adding the constant to wp-config.php, or use WP-CLI if available. Both methods don’t require phpMyAdmin.

Will repairing the database delete my content?

No. The repair process only fixes table structures and indexes; it doesn’t remove posts, pages, or settings. However, if you restore from a backup, any changes made after that backup will be lost.

How do I know if my database is corrupted?

Typical signs are error messages like “Error establishing a database connection,” “One or more database tables are unavailable,” or the site loading blank pages. Check your server error logs for MySQL errors.

Is it safe to use the built-in repair mode?

Yes, but only if you have a backup. The repair mode runs standard MySQL repair commands. It’s safe for MyISAM tables, but for InnoDB tables it may not fix the issue – you might need to restore from backup.

What should I do if the repair doesn’t work?

If all repair methods fail, the last resort is restoring from a backup. If you don’t have a backup, contact your hosting provider – they may have server-level backups or tools to recover the data.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →