There can be different reasons why one wants to repair a MySQL database. Database corruption maybe due to a lack of disk space or an unexpected server shutdown that caused the database to go bad. Queries that worked before suddenly stopped working and overall the database is not behaving anymore as expected. This guide shall not be a full database troubleshooting guide, but rather give you a hand on doing a MySQL repair on all databases on a cPanel/WHM server (or any other server running MySQL).
The situation: Your server crashed hard and when it is back up the MySQL side of the house is not working anymore. You do your normal troubleshooting. If all other troubleshooting and verifications are done, you come to the conclusion that you will need to run a repair as the next step. A full database restore from a (hopefully existing) good database backup would be your last choice.
Repairing one MySQL database is fairly easy, but how do you do this for an entire server holding20, 30, 50 or over 100 databases? You will need SSH access as the root user to the server running MySQL. So, log in as the root user and then do the following to repair your MySQL databases:
Please be advised that you should backup critical databases before running any of the commands below:
Check if you any need DB repair:
#myisamchk –check /var/lib/mysql/*/*.MYI
Then try a ‘safe-recover’ as the first step:
#myisamchk –safe-recover /var/lib/mysql/*/*.MYI
If the ‘safe-recover’ does not work, run a full recover:
#myisamchk –recover /var/lib/mysql/*/*.MYI
Then use the ‘force’ flag to get things back to normal:
#myisamchk –safe-recover –extend-check –force /var/lib/mysql/*/*.MYI
#myisamchk –recover –extend-check –force /var/lib/mysql/*/*.MYI
Use the mysqlcheck tool to repair your databases.
#mysqlcheck -–all-databases -r #repair
#mysqlcheck -–all-databases -a #analyze
#mysqlcheck -–all-databases -o #optimize
There you have it. By using one of these two options you should be able to repair and recover your databases. Please be advised that you should backup critical databases before running any of the commands above. If something goes wrong you can revert back and try again.