Search

Quick Solutions to Repair Corrupted Tables in MySQL

post-title

MySQL is a popular open-source RDBMS. It is an essential part of the LAMP stack used in lots of websites today. Though MySQL is generally used to store and manage structured data. As it is used regularly, there is high risk of getting corrupt MySQL Tables.

MySQL tables become corrupted due to several reasons, like hardware failures, improper shutdowns, software bugs, etc. Regardless of the reasons, the issue can be fixed by using some effective solutions.

In this optimized post, you will be got to know about repair corrupt MySQL table.

What Causes Table Corruption in MySQL?

  • 1. Crashing MySQL service after reboot
  • 2. Due to Bad memory
  • 3. Unexpectedly shutting off the machine because of a power failure
  • 4. Hardware failure
  • 5. Abruptly turn OFF the system while updating the SQL.
  • 6. Software glitches in the operating system or SQL code.

Now, let’s move on troubleshoot this issue with the possible approaches.

How to Repair Corrupt MySQL Tables?

Important Note- It’s highly recommended to create a valid backup of your important database before following the below steps.

Step-1: First, stop MySQL service

sudo systemctl stop mysql

Once you have stopped the SQL service, now create a backup of all your current files into the new directory. You can use Ubuntu’s default data directory /var/lib/mysql/ for copying the system content:

cp -r /var/lib/mysql /var/lib/mysql_bkp

Step-2: Determine the MySQL Engine Type

The earlier versions of MySQL uses MyISAM as a default storage engine, which is prone to db table corruption. However, to check MySQL engine, you have to enter the below query in a MySQL console:

mysql> SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES where TABLE_SCHEMA = 'database_name';

Please Note: Replace the 'database_name' with your database name.

This will show an output like:

+--------------------+-------------+-----------+---
| Table_Name     |  Engine   |  Version |    ...
+--------------------+-------------+-----------+---
| customers      |  MyISAM   |    10    |    ...
+--------------------+-------------+-----------+---
| staff          |  InnoDB   |    10    |    ...
+--------------------+-------------+-----------+---

Step-3: Check & Repair MyISAM Tables

Firstly, locate the corrupted table. Well, if the db table is using MyISAM storage engine, then you can use CHECK TABLE statement & check the table status:

mysql> CHECK TABLE table_name; 

After checking the table status, if there’s any error identified on the MyISAM table, then you can use the below command to solve the error:

mysql> REPAIR TABLE table_name;

Well, if the error fixing process was successful, you’ll get the error-free output like this:

Output:

+--------------------------+--------+----------+----------+

| Table | Op | Msg_type | Msg_text |

+--------------------------+--------+----------+----------+

| database_name.table_name | repair | status  | OK  |

+--------------------------+--------+----------+----------+

At last, restart MySQL service by using the below command in order to check if you can easily gain access to server:

sudo systemctl restart mysql

Step-4: Leverage the Effective “innodb_force_recovery” Option

In case, if the server crashed, then you should use the best option “force_recovery” in the InnoDB. Enabling this option helps to restore the MySQL databases. However, you can do so by modifying “mysqld.cnf” file in a nano text editor:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Simply add the below line beneath [mysqld] section:

innodb_force_recovery=1

Now, save and then close the file just after restarting MySQL service. 

Step-5: Now, Create mysqldump File

At this time, if you can access the corrupt table, then use mysqldump option & put all dumps in a new file. After that, you can name the file as per your choice and also ensure to add .sql extension at the end of the name.

Here I am naming a file as the demo.sql:

mysqldump database_name table_name > demo.sql

Now, use a DROP TABLE query in order to delete a table from database. To avoid needing to open MySQL prompt again, use the below syntax:

mysql -u user -p --execute="DROP TABLE database_name.table_name"

Next, use the dump file “demo.sql” that you have recently prepared, restore the db table:

mysql -u user -p --database=database_name < demo.sql

So, you have repaired your MySQL corrupted table successfully.

Recommended Way for Repairing Corrupt MySQL Table

If you want an instant way to repair a corrupt table in MySQL database, it is recommended to use an advanced software- Stellar Repair for MySQL. This MySQL repair utility can fix MyISAM & InnoDB database tables without downtime. Also, it saves much time and effort as compared to manual solutions.

Furthermore, this feature-loaded program can restore all inaccessible MySQL database objects, such as tables, triggers, views, primary keys, relations, forms, a more in their original format.

The best part about this MySQL database recovery software is- it is 100% safe & secure and is compatible with both Windows and Linux machines.

Complete Steps to Fix MySQL DB Table using Stellar Repair for MySQL Tool

Step 1: Download, install, and open Stellar Repair for MySQL program on Windows/Linux system.

Step 2: Next, the program will display Select Data Folder dialog-box. Choose the version of the database that you’re currently using, click on the browse option to select the database file that you need to repair >> click OK.

Step 3: Now, in the Select Database window, you have to select the db that you need to repair.

Step 4: Then, to repair the corrupt MySQL database, you have to click the Repair button.

Step 5: When the repairing process gets over, you will get a Repair Complete pop up on your screen. Then, click OK.

Step 6: After that, the program will show the preview of the repaired database tables & other recoverable stuffs. Then, go to File menu and click on Save.

Step 7: At this time, a Save Database dialog-box will appear on your screen. From Save As options, you have to select MySQL, enter the necessary details in order to Connect to the Server >> click Save button.

Step 8: Finally, the “Save Complete” box will appear when the process gets over.

All the chosen MySQL database tables will be repaired & saved at your desired location.

Conclusion

Once you go through this blog, you got complete overview of repair corrupt MySQL tables.

In this article, I have mentioned both the manual and paid solutions to fix this issue. However, the manual technique to repair corrupt multiple MySQL database tables requires much time & effort.

To save your precious time, effort, & resources, you can use the third-party Stellar Repair for MySQL tool. It is one of the best options that can fix corruption in both MyISAM and InnoDB tables effortlessly.

Thanks for reading…!