MySQL Recovery Toolbox: Step-by-Step Guide to Fix Corruption and Restore Data
Overview
A focused, practical guide that walks a MySQL administrator through diagnosing corruption, choosing recovery methods, and restoring data with minimal downtime. Covers storage-engine specifics (InnoDB, MyISAM), common corruption symptoms, recovery tools and commands, and prevention practices.
When to use this guide
- Database errors (crashes, I/O failures)
- Corrupted tables or indexes
- Lost or inconsistent rows after a crash
- Failed replication due to corrupted relay/master data
- When backups are missing or partial
Quick checklist (first 10 minutes)
- Isolate: Stop application writes or put the DB in read-only mode.
- Snapshot: Make a file-level copy or LVM snapshot of the data directory and binary logs.
- Preserve logs: Secure MySQL error log and binary logs (binlog).
- Document: Record MySQL version, OS, storage engine, and exact error messages.
- Don’t run risky commands (DROP, REPAIR without backup) until snapshot confirmed.
Diagnosis
- Check MySQL error log for messages like “InnoDB: page corruption”, “Got error 145”, or “Table is marked as crashed”.
- Use SHOW ENGINE INNODB STATUS\G for InnoDB internals.
- For MyISAM, run CHECK TABLE table_name;
- For InnoDB, try SELECT COUNT(*) on suspicious tables (may fail).
Recovery techniques by engine
InnoDB
- Try a clean shutdown (SERVICE mysql stop) and restart; InnoDB crash recovery may auto-fix.
- If startup fails, start MySQL with:
- innodb_force_recovery=1,2,… up to 6 (incrementally). Use lowest value that allows export.
- Note: values ≥4 make InnoDB read-only; use only to dump data.
- Once MySQL starts, use mysqldump to export affected databases/tables:
- mysqldump –single-transaction –routines –triggers dbname > dump.sql
- If dump succeeds, recreate a fresh MySQL instance and import dump.
- If innodb_force_recovery cannot start server:
- Use Percona Data Recovery Toolkit (undrop-for-innodb tools), or
- Use page-level recovery: extract pages from ibdata/ibd with tools like innodb_ruby or undrop-for-innodb, then rebuild tablespace.
- If using file-per-table (innodb_file_per_table=ON) and .ibd exists, try:
- CREATE TABLE with same structure in fresh instance, DISCARD TABLESPACE, replace .ibd, then IMPORT TABLESPACE (requires matching metadata; use tools to rebuild .frm/.ibd if needed).
MyISAM
- Run myisamchk on table files (.MYI/.MYD) from stopped server or with the engine offline:
- myisamchk -r -q /path/to/table.MYI
- Use myisamchk –safe-recover for complex cases.
- If myisamchk fails, copy .MYD/.MYI to a safe place and attempt recovery on copies.
- Consider using SELECT … INTO OUTFILE from a running server if table can be accessed read-only.
Recovering from missing binary logs or point-in-time needs
- If binlogs exist: use mysqlbinlog to replay events up to desired timestamp.
- If binlogs missing: rely on latest full backup plus logical exports; consider forensic log reconstruction only as last resort.
Restoring when backups exist
- Restore latest full backup to a separate host.
- Apply incremental backups or binary logs for point-in-time recovery.
- Validate data integrity and application behavior before cutover.
Tools and commands (practical)
- mysqldump, mysqlpump
- Percona XtraBackup (hot physical backups for InnoDB)
- mydumper/myloader (fast logical backups)
- myisamchk (MyISAM repair)
- mysqlbinlog
- innodb_force_recovery (my.cnf or startup option)
- Percona Toolkit, innodb_ruby, undrop-for-innodb
Testing and validation
- Run CHECK TABLE and spot-check rows.
- Use checksum tools (pt-table-checksum) for replication consistency.
- Run application tests against restored data before making it live.
Prevention best practices
- Use Percona XtraBackup or regular logical backups.
- Enable binary logging with appropriate retention.
- Use replication with a delayed slave for quick recovery from accidental deletes.
- Monitor disk health and set up alerts on MySQL errors.
- Test restore procedures regularly and keep runbooks updated.
Example quick recovery flow (InnoDB)
- Stop writes; snapshot data dir and binlogs.
- Try normal restart.
- If fails, set innodb_force_recovery=1 and restart; increase incrementally until it starts.
- Dump databases with mysqldump.
- Build fresh server, import dumps, reconfigure replication if needed.
If you want, I can produce a one-page printable runbook of these steps or a tailored recovery checklist for your MySQL version and environment.
Leave a Reply