Quick answer: For a corrupted MyISAM table, use CHECK TABLE to confirm and REPAIR TABLE (or mysqlcheck) to fix it. For InnoDB, corruption is rarer and repaired by restoring from backup or, as a last resort, starting in InnoDB recovery mode to export the data. Always back up the current files before attempting any repair.
CHECK TABLE your_table;
Errors like "table is marked as crashed" confirm corruption. Application errors such as "table doesn't exist" or unexplained query failures are also signs.
REPAIR TABLE your_table; -- or from the shell, across a database: mysqlcheck --repair your_database
This resolves most MyISAM corruption from an unclean shutdown.
InnoDB does not use REPAIR TABLE. The safe fix is restoring from a recent backup. If none exists, add innodb_force_recovery (starting at level 1) to the config to bring the database up read-only, export the data with mysqldump, then rebuild. Increase the level cautiously only if needed, and remove it afterwards.
What causes database corruption?
Usually an unclean shutdown, a full disk, or underlying storage problems. Reliable backups and free disk space are the best prevention.
Should I back up before repairing?
Always - copy the raw data files first so a failed repair does not make things worse.
On a SoftSys managed Linux VPS our team handles database recovery and keeps backups, so corruption is fixed with minimal data loss.