This article describes how to repair MySQL tables and databases. As a database's tables grow, errors may occur from time to time. When they do, MySQL includes several tools that you can use to check and repair database tables. To do this, follow the procedures below in the order in which they appear.
Before you attempt to repair any database, you should back it up first. To back up all of the files from all of your databases, follow these steps:
service mysqld stop
For Debian and Ubuntu, type:
service mysql stop
Type the following command:
cp -rfv /var/lib/mysql /var/lib/mysql$(date +%s)
Restart the MySQL server using the appropriate command for your Linux distribution:
service mysqld start
For Debian and Ubuntu, type:
service mysql start
After you back up your databases, you are ready to start troubleshooting. The mysqlcheck program enables you to check and repair databases while MySQL is running. This feature is useful when you want to work on a database without stopping the entire MySQL service.
Additionally, mysqlcheck works on tables that use the MyISAM or InnoDB database engines.
To use mysqlcheck, follow these steps:
cd /var/lib/mysql
Type the following command, replacing database with the name of the database that you want to check:
mysqlcheck database
The previous command checks all of the tables in the specified database. Alternatively, to check a specific table in a database, type the following command. Replace database with the name of the database, and replace table with the name of the table that you want to check:
mysqlcheck database table
Mysqlcheck checks the specified database and tables. If a table passes the check, mysqlcheck displays OK for the table. However, if mysqlcheck reports an error for a table, type the following command to try to repair it. Replace database with the database name, and table with the table name:
mysqlcheck -r database table
If running mysqlcheck does not fix the problem, the next step is to run diagnostics specific to the engine used by the database table or tables. Follow the appropriate procedure below for your table's database storage engine.
If you are using the MyISAM storage engine for a table, you can run the myisamchk program to repair it. To do this, follow these steps:
service mysqld stop
For Debian and Ubuntu, type:
service mysql stop
Type the following command:
cd /var/lib/mysql
Type the following command, replacing table with the name of the table that you want to check:
myisamchk table
To check all of the tables in a database, type the following command:
myisamchk *.MYI
If the previous command does not work, you can try deleting temporary files that may be preventing myisamchk from running correctly. To do this, change back to the /var/lib/mysql directory, and then type the following command:
ls */*.TMD
If there are any .TMD files listed, type the following command to delete them:
rm */*.TMD
Then try to run myisamchk again.
To try to repair a table, type the following command, replacing table with the name of the table that you want to repair:
myisamchk --recover table
Restart the MySQL server using the appropriate command for your Linux distribution:
service mysqld start
For Debian and Ubuntu, type:
service mysql start
If you are using the InnoDB storage engine for a database table, you can run the InnoDB recovery process. To do this, follow these steps:
Add the following line to the [mysqld] section:
innodb_force_recovery=4
Save the changes to the my.cnf file, and then restart the MySQL server using the appropriate command for your Linux distribution:
service mysqld restart
For Debian and Ubuntu, type:
service mysql restart
Type the following command to export all of the databases to the databases.sql file:
mysqldump --all-databases --add-drop-database --add-drop-table --routines > databases.sql
Start the mysql program, and then try to drop the affected database or databases using the DROP DATABASE command.
Stop the MySQL server using the appropriate command for your Linux distribution:
service mysqld stop
For Debian and Ubuntu, type:
service mysql stop
If you were unable to drop a database in step 6, type the following commands to delete it manually. Replace database with the name of the database that you want to delete:
cd /var/lib/mysql
rm -rf database
Use your preferred text editor to open the my.cnf file on your server, and then comment out the following line in the [mysqld] section as shown:
#innodb_force_recovery=4
Save the changes to the my.cnf file, and then start the MySQL server using the appropriate command for your Linux distribution:
service mysqld start
For Debian and Ubuntu, type:
service mysql start
Type the following command to restore the databases from the backup file you created in step 5:
mysql < databases.sql
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.