Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

MySQL says a table is marked as crashed and must be repaired

ErrorTable 'wp_options' is marked as crashed and should be repaired

Short answer

This error means a MyISAM table was flagged as damaged. Back up the table files, run CHECK TABLE, then REPAIR TABLE. If that fails, stop the server and run myisamchk. Moving the table to InnoDB removes the MyISAM weakness behind it.

  • Applies to MySQL 8.0, MySQL 8.4 LTS
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out which storage engine the table uses. Repair only exists for MyISAM, ARCHIVE and CSV tables.

    SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES
    WHERE TABLE_SCHEMA = 'mydb' AND TABLE_NAME = 'wp_options';
  2. 2Match what you see, then jump to its fix

    1. 1ENGINE is MyISAM and CHECK TABLE reports the table as crashed. perror 145 gives 'Table was marked as crashed and should be repaired'.A MyISAM table was left damaged and flaggedFix
    2. 2The REPAIR TABLE result is not status OK, or the error returns straight away.REPAIR TABLE did not end with OKFix
    3. 3REPAIR TABLE or myisamchk -r refuses because the .MYI file is missing or its header is corrupt.The index file header is lost and a normal repair cannot read itFix
    4. 4The same table crashes again after each unclean shutdown.The table is still on MyISAM, which has no crash recoveryFix

What does the error look like?

  • Table 'wp_options' is marked as crashed and should be repaired
  • Table was marked as crashed and should be repaired
  • Table is crashed and last repair failed

Cause 1 of 4

A MyISAM table was left damaged and flagged

1Check

Back up the table's .MYD and .MYI files first, because a repair can lose data. Then ask the server to check the table.

CHECK TABLE wp_options;

2Fix

Run REPAIR TABLE. By default, for MyISAM this behaves like myisamchk --recover. The last row of the result should show Msg_type status and Msg_text OK.

REPAIR TABLE wp_options;

3Verify

Check the table again and read it.

CHECK TABLE wp_options;
SELECT COUNT(*) FROM wp_options;

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 4

REPAIR TABLE did not end with OK

1Check

The manual says to try the slower safe recovery when a MyISAM repair does not return OK. The server must be stopped before you run myisamchk on its files, and mysqladmin shutdown returns before the server has fully stopped.

sudo systemctl stop mysql
sudo systemctl status mysql

2Fix

With the server stopped and the files backed up, run myisamchk from the database directory: first the quick index repair, then the full recovery, then safe recovery.

cd /var/lib/mysql/mydb
myisamchk -r -q wp_options.MYI
myisamchk -r wp_options.MYI
myisamchk --safe-recover wp_options.MYI

3Verify

Start the server and run CHECK TABLE again.

sudo systemctl start mysql
mysql -e "CHECK TABLE mydb.wp_options"

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 4

The index file header is lost and a normal repair cannot read it

1Check

The manual limits REPAIR TABLE ... USE_FRM to this case: the .MYI file is missing or its header is corrupted.

ls -l /var/lib/mysql/mydb/wp_options.*

2Fix

Use USE_FRM as a last resort. It loses the current AUTO_INCREMENT value and does not reclaim space from deleted rows, and it must not be used on compressed tables.

REPAIR TABLE wp_options USE_FRM;

3Verify

Check the table and set the AUTO_INCREMENT value again if the table had one.

CHECK TABLE wp_options;
SHOW CREATE TABLE wp_options;

Confirmed by dev.mysql.com

Did this fix it?

Cause 4 of 4

The table is still on MyISAM, which has no crash recovery

1Check

REPAIR TABLE only supports MyISAM, ARCHIVE and CSV; it does not cover InnoDB. A table that is still MyISAM keeps the same weakness.

SELECT TABLE_NAME FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'mydb' AND ENGINE = 'MyISAM';

2Fix

Rebuild the table as InnoDB. ALTER TABLE with an ENGINE clause rebuilds the table.

ALTER TABLE wp_options ENGINE = InnoDB;

3Verify

The engine column now says InnoDB.

SELECT ENGINE FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'mydb' AND TABLE_NAME = 'wp_options';

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Convert MyISAM tables to InnoDB so the server can recover them after a crash.
  • If the server exits during a repair, run REPAIR TABLE again straight after the restart, before anything else touches the table.
  • Keep a recent dump so that a failed repair costs you one restore.
  • After a repair on a replicated source, repair or rebuild the replicas too, because the manual says the repair changes are not propagated.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.