🔧 For MyISAM Tables

You can use the REPAIR TABLE statement or command-line tools:

Using SQL Command

REPAIR TABLE table_name;

  • Repairs the table if it’s corrupted.
  • Works only for MyISAM tables.

Using Command Line (mysqlcheck)

mysqlcheck -u root -p –repair db_name table_name

Or to repair all tables in a database:

mysqlcheck -u root -p –repair db_name

Using myisamchk (Requires Table to Be Offline)

myisamchk -r /var/lib/mysql/db_name/table_name.MYI

Use only when MySQL server is stopped.

Run with -o for a safer, slower repair:

myisamchk -o /var/lib/mysql/db_name/table_name.MYI

🔧 For InnoDB Tables

InnoDB is more self-healing, but manual intervention may be needed in severe cases.

Restart with Recovery Options (MySQL Configuration)

In my.cnf or my.ini:

[mysqld]
innodb_force_recovery = 1

  • Set value 1 to 6 depending on severity (higher is more aggressive).
  • Start MySQL, dump the database, and restore after removing this setting.

Dump and Reimport

Sometimes, simply exporting and reimporting the table/data works:

mysqldump -u root -p db_name table_name > dump.sql
mysql -u root -p db_name < dump.sql

⚠️ General Tips

  • Always back up the database before attempting any repair.
  • Check logs (/var/log/mysqld.log or error.log) for exact corruption errors.
  • Consider using CHECK TABLE first to diagnose:

CHECK TABLE table_name;

By admin