Part of being a Linux System Administrator is to check that databases and tables are working properly specially if your dealing with e-commerce site.  In this how to i will discuss how it is easy to check and repair MySQL tables using myisamchk.

In doing this procedure you must login as root and Mysql Server is down.  If this is a e-commerce site you also need to shutdown apache server, this is because myisamchk makes changes to the table file itself, so you don’t want other users accessing or otherwise manipulating the file during the repair operation. So the sequence should be

  • backup the tables that you want to repair.

cp /var/lib/mysql/database/table.myi /var/lib/mysql/database/table-bak.myi

  • Shutdown apache server then mysql server.

/etc/init.d/httpd stop

/etc/init.d/mysql stop

Now type the following in the terminal.

  • myisamchk /var/lib/mysql/database/table.myi

Note: use the complete path not just the table name and include the file extension.

If your check reveals errors, or if you see cryptic error messages about table handlers or corrupt indexes, then you need to try repairing the table.

  • myisamchk –recover /var/lib/mysql/database/table.myi

The –recover option reconstructs the MySQL table index file after weeding the corrupted table of invalid or previously deleted records that could be causing a problem.

If –recover option fails, revert to the original table file and try the –safe-recover option. This is slower, because MySQL needs to scan through the records in the data file one by one and them restore the index, but it can sometimes work better than a regular repair operation.

  • myisamchk –safe-recover /var/lib/mysql/database/table.myi

After you successfully checked and recovered your MySQL tables you can now start your servers using the following sequence.

/etc/init.d/mysql start

/etc/init.d/httpd start

If you still have no luck and tables are still corrupted you can check this site the MySQL Manual.

Like this blog? Why not buy me a cup of coffee?