MySQL
database error
table crash
repair failed
troubleshooting

MySQL table is marked as crashed and last automatic? repair failed

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The MySQL error "table is marked as crashed and last automatic repair failed" usually points to corruption in a MyISAM table, not a general SQL syntax problem. The immediate goal is to identify the affected storage engine, preserve data safely, and choose the correct repair path instead of running repair commands blindly.

First Confirm the Storage Engine

This error is most strongly associated with MyISAM. That matters because REPAIR TABLE and myisamchk are MyISAM-oriented tools. If the table is InnoDB, the remediation path is different.

Check the engine first:

sql
SHOW TABLE STATUS LIKE 'orders';

or:

sql
SELECT TABLE_NAME, ENGINE
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'appdb' AND TABLE_NAME = 'orders';

If the table is MyISAM, repair tools may help. If it is InnoDB, stop and investigate the underlying corruption or recovery path differently.

Check the Table Before Repairing

Before changing anything, run an explicit table check:

sql
CHECK TABLE orders;

This tells MySQL to inspect the table and report the condition. If the result confirms corruption, you can decide whether a SQL-level repair is appropriate.

At the same time, treat the situation as a data-risk event. If possible, take a filesystem-level copy or backup before attempting repair, especially in production. Repairs can fail partially or make a bad situation harder to reverse.

Try REPAIR TABLE for MyISAM

For a MyISAM table, the first repair attempt is usually:

sql
REPAIR TABLE orders;

If the damage is modest, that may restore the indexes or table structure enough for the table to become usable again.

You can also request the extended form when the basic repair fails:

sql
REPAIR TABLE orders EXTENDED;

Extended repair can be slower, but it is sometimes useful for more serious index corruption.

myisamchk for Offline Repair

If SQL-level repair is not enough, myisamchk is the traditional offline tool. This usually requires stopping MySQL or at least making sure the table is not being modified while the repair runs.

A typical flow looks like this:

bash
sudo systemctl stop mysql
myisamchk -r /var/lib/mysql/appdb/orders.MYI
sudo systemctl start mysql

For harder cases, you may see advice to use safer or more forceful options:

bash
myisamchk --safe-recover /var/lib/mysql/appdb/orders.MYI

or:

bash
myisamchk -r -f /var/lib/mysql/appdb/orders.MYI

Use these deliberately. Offline repair tools are powerful, but they operate directly on table files, so you want a backup before experimenting.

Why This Happens

Common causes include:

  • abrupt shutdown or power loss
  • disk or filesystem problems
  • hardware faults such as bad memory or storage
  • server crashes during writes
  • a table format that is simply more fragile than a transactional engine

That last point matters. MyISAM lacks the crash-recovery behavior that many teams expect from modern transactional storage engines. If this table matters operationally, the error is often a sign that the engine choice itself should be revisited.

Consider Migrating to InnoDB

If the application still depends on MyISAM for ordinary data tables, this kind of incident is a good reason to migrate if possible. InnoDB provides transaction support, row-level locking, and crash-recovery features that make these repair scenarios much less common.

A migration might look like this after the table is healthy again:

sql
ALTER TABLE orders ENGINE=InnoDB;

Do not run that in the middle of an active corruption event without understanding the state of the data first. But as a long-term fix, it is often the right direction.

Restore from Backup When Repair Fails

Sometimes repair attempts are not enough. If the table data file is badly damaged, restoring from a known-good backup may be faster and safer than repeated repair attempts.

That decision depends on:

  • how current the backup is
  • whether binary logs can replay missing changes
  • whether the damaged table is read-only, archival, or actively written

In serious cases, database recovery is a restoration problem, not a repair-command problem.

Common Pitfalls

The most common mistake is running REPAIR TABLE without first checking whether the table is actually MyISAM. Another is attempting myisamchk against live table files while MySQL is still actively using them, which can make corruption worse. Teams also sometimes keep retrying repairs without taking a backup or copy first, which is risky when the table is already damaged. A final issue is treating the immediate repair as the whole fix instead of asking why a crash-prone storage engine or unstable system condition caused the corruption in the first place.

Summary

  • This error usually points to corruption in a MyISAM table.
  • Confirm the storage engine before choosing a repair strategy.
  • Start with CHECK TABLE, then use REPAIR TABLE if appropriate.
  • Use myisamchk only with care and preferably offline.
  • If repair is unreliable, restore from backup and consider migrating important tables to InnoDB.

Course illustration
Course illustration

All Rights Reserved.