Data in MySQL tables is stored and managed in rows and columns. The server uses two storage engines – InnoDB and MyISAM. Tables created under these engines are respectively referred to as InnoDB or MyISAM tables.
MyISAM tables can get corrupted due to numerous reasons, such as an unexpected system shutdown, software bugs and more. This storage engine is more prone to corruption after an abnormal server shutdown as it doesn’t provide advance features such as crash recovery. In contrast, InnoDB storage engine provides a crash recovery mechanism.
Although InnoDB’s crash recovery option minimizes corruption after unexpected shutdowns, tables can still become damaged and cause errors. In this article, we will learn tested ways to fix corrupted tables in MySQL.
What causes Corruption in Tables in MySQL?
Corruption in MySQL tables can occur due to one of the following reasons:
- Sudden power failure stops MySQL Server when writing to the database table
- Bugs in MySQL code or operating system
- Hardware failure
- The system, hosting the MySQL Server, is shut down unexpectedly
- MySQL service crashes after reboot
How to Check and Detect Corruption in MySQL Tables?
You can first check the MySQL Server Error Log, which records server startups, shutdowns, and page corruption details. If your server is not running, then go to the MySQL data directory to open the error log manually. Else, you check the log using Workbench under the section labelled “Server Logs”.
If the server is running, you can also run the CHECK TABLE command directly via Query Editor. It helps you to verify the integrity of the indexes and data pages in table. However, if the server refuses to start due to severe damage, you can use the innochecksum utility—an offline command-line tool. It helps to scan the tablespace files even when the server is stopped.
MySQL Tables are Corrupted. Now What?
If you confirm that there is corruption in the tables, then you can restore your backup (dump file). If you have created the dump file, you may need the “mysql client utility” to restore it. If a physical backup of the MySQL data directory is available, you can restore the corrupted tables directly from it.
Steps to restore tables via the Data Directory:
- Stop the MySQL server service:
net stop mysql
- Locate the MySQL data directory (datadir) containing the database files.
- Copy the backed-up database folder along with the files such as ibdata1 and redo logs into the active data directory.
- Ensure the database folder contains the required files based on the storage engine:
- .frm, .MYD, and .MYI for MyISAM
- .frm and .ibd files for InnoDB (MySQL 5.7 & earlier)
- .ibd tablespace files for InnoDB (MySQL 8.0+)
- Start the MySQL Server:
net start mysql
- Open MySQL and verify that the database is visible:
SHOW DATABASES;
- To restore the dump file, use the MySQL client via the CMD window. Here’s how:
mysql -u root -p your_database < backup.sql
You can also use “PhpMyAdmin” tool to restore the backup (dump) file. But it has certain downsides like
- It has file-size limitations.
- It can cause time-out problems when uploading large-sized database files.
How to Fix Corrupted Tables in MySQL Server if Backup is not Available?
If the backup is not available, then follow these repair methods to fix corrupted tables in MySQL.
Method 1 – Use Dump and Reload Method
If InnoDB tables are corrupted or you’re encountering errors such as Error 1030, InnoDB: Checksum mismatch in data file, then you can use the dump and reload method. It helps in repairing the tables, including indexes. If corruption in table has caused the MySQL database to crash, then first go to the MySQL configuration file and then enable the innodb_force_recovery option. For this, enter the following statements under [mysqld] section:
mysqld]
Innodb_force_recovery=1
The default value of innodb_force_recovery is 0. However, you can change its value to ‘1’ to start the InnoDB engine and dump the tables. You can set level 1-6 to control startup behavior and allow the server to start even when corrupt tables exist. However, dumping tables with “innodb_force_recovery value” of 4 or higher can lead to data loss. So, it is a good practice if you create the backup file before proceeding.
Enabling the innodb_force_recovery allows you to start the MySQL Server to access the corrupt table. Then, run the following command to dump the table data:
mysqldump -u user -p database_name table_name > single_dbtable_dump.sql
Next, export all the databases to the dump.sql file by executing the command below:
mysqldump –all-databases –add-drop-database –add-drop-table > dump.sql
Now, restart the MySQL Server and then use the DROP TABLE command to drop the table:
DROP TABLE table_name;
After performing these tasks, put a comment in the line to disable the InnoDB recovery mode.
#innodb_force_recovery=…
Also, don’t forget to save the applied changes to the configuration file and restart the MySQL Server.
Method 2 – Run myisamchk Command
You can fix MyISAM table corruption using the myisamchk command. It can easily helps you resolve issues in the MYD data file or the index file (.MYI) of the table. However, before running it, create database’s backup.Next, stop the MySQL Server and run the command as given below:
myisamchk –recover /path/to/database_dir/TABLE.MYI
After this, start the server.
Method 3 – Use the REPAIR TABLE Command
This command only supports repairing of your MyISAM tables and also require SELECT and INSERT privileges for the table on which you are executing the command. Here’s how to use it:
REPAIR TABLE your table name;
Method 4 – Use a Professional MySQL Repair Tool
You can take help of Online MySQL Repair service to repair corrupted MySQL databases directly in your browser. It allows you to repair database file of up to 500 MB for free. You can even preview recoverable database objects before downloading the repaired file. For larger MySQL database files (up to 5 GB), you can upgrade to the Premium version.
You can even use an advanced MySQL repair tool, like Stellar Repair for MySQL to repair MySQL database tables. It supports repairing of both InnoDB and MyISAM tables. You do not need to run any commands to repair the tables using this tool. It allows you to repair multiple corrupt tables at once or even the entire database of any size or in any state. The tool can recover all the database objects, including tables, foreign keys, and primary keys with 100 % integrity. furthermore, it supports selective recovery and is compatible with both Windows and Linux operating systems.
Conclusion
You can use various methods to restore and repair corrupted tables in MySQL. In this post, you saw how to restore damaged or corrupted MySQL tables from backup as well as how to fix them if backup is unavailable or unrecoverable. You can use the most preffered way ‘dump and reload’ to rebuild InnoDB tables or the myisamchk command to repair MyISAM tables. In case you want a single tool that can repair both InnoDB and MyISAM tables, you can try the free online MySQL repair service. It helps you easily fix tables without installing the MySQL Server.
While the free online service is excellent for fixing the corrupt database files, it does have file size limitation. To repair corrupt MySQL tables without any file size restrictions, use advanced MySQL repair software like Stellar Repair for MySQL. It allows you to safely repair multiple large-sized databases directly on your local system.