How To: Fix a corrupt MySQL database
December 24th, 2008
Occassionally one or more tables in the database becomes corrupt. Fixing it is usually simple. Always be sure to make periodic backups of your database, just in case these tools do not work.
To test your database for corruption, issue the following command. You can include a table name at the end to just check a specific table, if desired:
mysqlcheck -u {USERNAME} {DATABASE} [TABLE]
Example:
C:\mysql5.0.45\bin>mysqlcheck -u root bytesphere alerts bytesphere.alerts warning : 19 clients are using or haven't closed the table properly error : Found key at page 339968 that points to record outside datafile error : Corrupt
To fix it, make sure the system is shut down and not connected to it, then issue the following command including the -r switch to repair:
mysqlcheck -r -u {USERNAME} {DATABASE} [TABLE]
Example:
C:\mysql5.0.45\bin>mysqlcheck -r -u root bytesphere alerts
bytesphere.alerts OK
Here we see that the mysql tool has fixed the alerts table. Now you can start up your application and attend to business as usual.