A damaged MySQL database is usually fixable, but the order of operations matters more than the tools. The reliable path is to stop writes, make a byte-level backup, identify whether the damage sits in a single table, an index, the InnoDB redo/undo layer, or the filesystem, and only then choose the least destructive repair. Most “corruption” reports turn out to be one of three things: a crashed server with recoverable InnoDB pages, a MyISAM table that needs REPAIR TABLE, or a failing disk that will destroy any repair you attempt. Classify correctly and you keep your data; guess and you can turn a recoverable outage into permanent loss.
This guide covers MySQL 8.0 and 8.4 in the InnoDB-default era, noting where MyISAM and older 5.7 behavior differ. It assumes you have shell access to the server and, ideally, a recent logical or physical backup. If you don't have a backup, the first section still applies: create one before you change anything.
Why "damaged" needs a diagnosis before a repair
Corruption in MySQL means data or metadata that no longer matches its own internal structure. Error 145 (Table 'x' is marked as crashed and should be repaired), error 126 (Incorrect key file), error 1034, or InnoDB messages like Database page corruption on disk or a failed read all point at damage, but each has a different cause and a different safe fix.
Three failure classes dominate:
Physical/storage layer: bad sectors, a failing SSD, a truncated file, or a filesystem that returned zeroed pages. Repairing tables on a dying disk usually makes things worse, because repair rewrites pages over the same failing medium. Crash/consistency layer: power loss, kill -9 on mysqld, or an OS crash mid-transaction. InnoDB is designed to recover from this automatically via its redo log; you often need nothing more than a clean restart. Forced shutdown on MyISAM can leave tables crashed and needing explicit repair. Logical/user layer: dropped or altered objects, a bad DELETE, or a corrupted mysql system schema. These are not “corruption” in the storage sense and are handled with backup restore or mysql_upgrade-style steps, not REPAIR TABLE.
The critical rule: never run a repair on a database you cannot first back up. A repair that fails can leave a table in a worse state than it started. Copy the entire data directory first, even if the server is down.
Prerequisites: back up before you touch anything
You need four things before any repair: a stopping point for writes, a full copy of the data directory, free disk space of at least the database's size on a different device (so a disk failure can't take both copies), and the exact MySQL version (SELECT VERSION();) because repair behavior and InnoDB recovery settings changed across releases.
If the server is still running and healthy enough to accept a logical dump, take one:
--single-transaction gives a consistent snapshot for InnoDB tables without locking them. MyISAM tables are not covered consistently by that flag, so add --lock-tables if you have MyISAM and can tolerate the lock. If the server won't start at all, skip to the physical copy and copy the datadir with the server stopped.
For a stopped server, a byte-for-byte copy is safest:
Adjust /var/lib/mysql to your datadir from my.cnf. Success check: the copy exists, is roughly the same size as the original (du -sh both), and you can read files from it. If the copy itself errors, the disk is failing—stop and replace hardware before any repair.
Step 1: Confirm the server's own recovery runs first
InnoDB performs crash recovery on startup. Many "corrupt" databases need only this. Start MySQL cleanly and watch the error log:
Look for InnoDB: Database was not shut down normally, followed by InnoDB: Starting crash recovery and finally InnoDB: Database physically writes the file full: wait... and a ready-for-connections message. If you reach ready for connections with no [ERROR] lines mentioning corruption, you're done—run CHECK TABLE (Step 3) to confirm.
What to expect: crash recovery can take seconds or, on multi-hundred-gigabyte InnoDB instances, many minutes. Don't interrupt it. If startup aborts with InnoDB: Error: page ... log sequence number ... in the future or InnoDB: Cannot open datafile, the crash layer has failed and you escalate to Step 4.
A specific failure worth naming: a redo log truncated by a full disk or a mismatched innodb_log_file_size can prevent clean recovery. That's not user corruption and is fixed by restoring the datadir from backup, not by REPAIR TABLE.
Step 2: Check the storage underneath before blaming MySQL
A repair writes pages back. On a disk with bad sectors, that write may fail or silently return zeros, and you'll chase the same error after every repair. Rule this out first with SMART and kernel messages:
If you see pending sectors, reallocated sectors climbing, or EXT4-fs error ... remounting filesystem read-only, stop. Copy the datadir to healthy media and treat hardware failure as the root cause. Repairing software corruption on failing hardware is a losing proposition—you'll usually lose more data than you recover.
Step 3: Identify the damaged object with CHECK TABLE
With the server running, narrow the problem to a schema and table. CHECK TABLE works on both InnoDB and MyISAM and reports structural issues without modifying anything.
Interpretation matters. CHECK TABLE returning status: OK means MySQL's view of the table is consistent, not that the disk is. status: Corrupt on MyISAM is a green light for REPAIR TABLE. For InnoDB, CHECK TABLE rarely says Corrupt; page damage normally shows up as errors in the error log during queries or at startup, such as:
InnoDB: Database page corruption on disk or a failed read of page [page id] [ERROR] [MY-010334] ... Invalid (old?) table or database name (often schema/dictionary damage) [ERROR] [MY-011906] ... Corruption: ...
Grep the error log for [ERROR] and corrupt rather than relying on memory:
Also check the mysql system schema if authentication or privileges suddenly fail: CHECK TABLE mysql.user; (in 8.0, mysql.user is a view; check the underlying mysql.user base table via CHECK TABLE mysql.user). Grant-table damage typically requires restoring mysql from backup—REPAIR TABLE on InnoDB system tables is not a supported general fix.
Step 4: Repair by failure class, least destructive first
MyISAM tables: REPAIR TABLE and mysqlcheck
For MyISAM, REPAIR TABLE is the standard tool and is expected to work for crash damage.
REPAIR TABLE returns status: OK on success. If it returns status: Error, the table file may be physically truncated; then use myisamchk with the server stopped:
Start with -r (recover); -o (safe recover) is more conservative if -r fails but can drop rows it can't read. A table with a damaged unique index may report duplicates after repair—check with SELECT queries that touch indexed columns before assuming success.
InnoDB: do not routinely use REPAIR TABLE
REPAIR TABLE on InnoDB is accepted by the parser in 8.0 but generally does nothing meaningful; InnoDB's self-healing is the redo log and doublewrite buffer. If crash recovery in Step 1 succeeded, InnoDB is already consistent. If it failed, the escalation path is:
1: Restore from backup — the only reliable fix for InnoDB page-level corruption. Restore the datadir, then replay binary logs (mysqlbinlog) up to the point of failure if you need to close the gap. 2: innodb_force_recovery — when no backup exists and the server won't start, set innodb_force_recovery in my.cnf under [mysqld] and increase 1 → 6 until startup succeeds. At level 1–2, the server starts read-only-ish and lets you mysqldump data out; at higher levels it may skip more recovery, accepting more data loss. While set, INSERT/UPDATE/DELETE are blocked, and you should drop innodb_force_recovery to 0 after exporting. Use it only to rescue data, never as a permanent setting. 3: innodb_force_recovery=6 is a last resort and can mask corruption badly enough that you cannot tell what was lost; document exactly what level started and export immediately.
Sequence for the force-recovery path: set a low level, start, dump to a fresh server or fresh database, verify row counts against your expectations, then rebuild from that dump. Do not run the production workload on a force-recovery instance.
Dictionary and InnoDB metadata damage
If the error log shows InnoDB: Cannot open datafile or dictionary errors and the server won't start, you may need to reinitialize or restore. mysql_upgrade isn't a repair tool in the modern sense—don't reach for it expecting to heal pages. If you have a backup of ibdata1 and the mysql schema, restore those with the datadir.
When repair is the wrong move
Repair is the wrong tool when the disk is failing (Step 2), when the damage is in the mysql system schema (REPAIR TABLE doesn't fix grant corruption), or when the object is a view, trigger, or stored routine (those live in metadata; recreate them from SHOW CREATE output or your migration scripts). Know the stop condition: if mysqldump of a force-recovery instance fails partway, or if repeated repairs return the same error, further DIY work risks more loss—engage a MySQL specialist or your cloud provider's managed-database recovery support, and in the meantime preserve the failing datadir and backups intact.
Step 5: Verify the repair before returning to service
A table that returns status: OK from repair isn't necessarily correct. Verify three ways:
If you have a backup, compare row counts and a checksum. For a strong check, run CHECKSUM TABLE yourdb.yourtable on both the repaired and a restored copy if one exists. status: OK after EXTENDED means MySQL can read every page; it doesn't prove every row you expected survived. That's why the count comparison matters—REPAIR TABLE on MyISAM can silently drop unreadable rows.
Bring the application back gradually, monitor the error log for a few hours, and re-enable binary logging if you had disabled it during maintenance. Confirm replication caught up if the instance is a replica.
What causes it, and how to stop it recurring
The majority of "damaged MySQL" incidents come from a short list:
Unclean shutdowns — kill -9, OOM-killer terminations, power loss. InnoDB usually recovers, but repeated forced stops combined with fsync being off (innodb_flush_method, or a filesystem lying about fsync, common on some virtualized/copying storage) put pages at risk. Filesystem and storage issues — failing disks, full volumes truncating files, misconfigured RAID, or network storage that drops writes. Killing a server mid-write to MyISAM, which has no crash-safe recovery. Restoring a datadir from a running server (inconsistent copy) — a classic self-inflicted corruption.
Practical hardening: make sure innodb_flush_log_at_trx_commit=1 unless you accept data-loss trade-offs, use a real backup schedule (logical dumps plus periodic physical or snapshot backups tested by actual restores), monitor SMART and disk space, and prefer InnoDB everywhere you can. Test restores—an untested backup is a hope, not a plan.
A fair comparison of repair tools on one dimension—destructiveness—shows why order matters. CHECK TABLE is read-only. REPAIR TABLE rewrites and can drop unreadable MyISAM rows. innodb_force_recovery trades recovery completeness for startup and can silently skip pages. Backup restore replaces the data wholesale and is the only method that guarantees InnoDB page-level consistency, but it loses writes after the backup's point in time unless binary logs cover the gap. The "best" tool is the least destructive one that fits your failure class, which is why Step 3's diagnosis isn't optional.
Practical FAQ
Does REPAIR TABLE work on InnoDB?
Not as a real repair in modern MySQL. The statement is accepted but InnoDB's consistency comes from the redo log and doublewrite buffer, not from table repair. If InnoDB is corrupt, restore from backup or extract data with innodb_force_recovery; don't expect REPAIR TABLE to fix pages.
The server says "Table is marked as crashed" — which engine is it?
Check SHOW TABLE STATUS LIKE 'yourtable'; for the Engine column. If MyISAM, REPAIR TABLE is appropriate. If InnoDB, that message is unusual and likely reflects a crash on startup that InnoDB should resolve itself; verify the error log after a clean restart.
Will I lose data by repairing?
With MyISAM REPAIR TABLE, yes—it can drop rows it cannot read, so compare counts before and after. InnoDB repair mechanisms prioritize availability and can skip pages. The only way to know the true loss is a backup comparison.
How long does a fix take?
Diagnosis is minutes; CHECK TABLE EXTENDED on large InnoDB tables can take a long time; InnoDB crash recovery on a big instance can run for many minutes; a restore from a logical dump can take hours depending on size. Plan the maintenance window from a test restore of comparable size, not guesswork.
Can I fix it without downtime?
For a single MyISAM table, sometimes. For InnoDB page corruption, no—recovery or restore needs a controlled outage. On managed cloud MySQL, use the provider's automated backup and point-in-time restore rather than manual file copies.