How to Fix MySQL Server Database Corruption?

Summary: This blog will outline potential reasons behind MySQL Server Database Corruption issue and steps to fix the issue. These steps include backing up the database (db) before performing recovery operations, using ‘innodb_force_recovery’, etc. The blog will also explain how MySQL repair software can help when you are unable to repair and restore the database.

Database corruption in MySQL Server, if not handled properly or resolved in time, can lead to major downtime and data loss.

Unfortunately, you might not even notice that your MySQL server has turned corrupt, until the server accesses a corrupt table or the server is shutdown.

What Causes Database Corruption in MySQL Server?

Following are some of the common reasons behind MySQL Server Database Corruption:

Besides these reasons, db corruption also depends on whether you’re using MyISAM or InnoDB storage engine. That?s because MyISAM tables are more susceptible to corruption.

What to do When MySQL Database Gets Corrupt?

Usually, when MySQL database corrupted, it is recommended that you restore it from the last known good backup copy. But if you don?t have the backup, performing recovery operations can help fix MySQL database corruption.

Even if backup is available, attempting recovery can be a better option as it takes less time in getting back the db online.

Troubleshooting Steps to Resolve MySQL Database Corruption

Try resolving database corruption by following steps in the sequence listed below:

NOTE: These steps will work only if the MySQL database server is running. But, if MySQL service stops running (has crashed) and you cannot log into the MySQL command-line client, the only option left is to restore the database from a good known backup. If there is no backup, using a specialized MySQL repair software may help.

Step 1 ? Backup Your Database

Before attempting to repair the corrupt database, make sure to back up your database files first.

Although the db is already corrupt, backing it up will help minimize the risk of further damage, which may be caused due to a recovery operation.

To back up all of the db files, follow these steps:

Step 2 ? Bring Database Back in Recovery Mode

NOTE: If MySQL Server uses MyISAM as the default storage engine, skip to Step 3 to know the process of repairing the corrupt db.

If InnoDB is MySQL Server?s default storage engine, in some cases of database corruption, merely dumping, dropping and re-creating one or a few corrupt tables will work for you. For this, use the CHECK TABLE statement to check the tables that are corrupt.

Read this: How to Repair MySQL Database Table Without Downtime?

If database corruption is severe, you will likely face trouble while starting your MySQL Server. In such a case, forcing InnoDB recovery may help start up MySQL.

To use ?innodb_force_recovery? option, follow these steps:

NOTE: The innodb_force_recovery is set to ?0? by default. However, to start InnoDB and dump MySQL tables, you will need to set the value to ?1? and increase the value incrementally (from 1 to 6).

But, keep in mind, setting the value of innodb_force_recovery option to 4 or greater increases the chances of data corruption.

[mysqld]
innodb_force_recovery=1
mysqldump -u [user] ?p[password] --all-databases > all_databases.sql

NOTE: The dump file in this example is called all_databases.sql. But, you can change it to whatever you want.

#innodb_force_recovery=...
 mysql> source all_databases.sql

Read about: Best Ways to Repair InnoDB Table Corruption in MySQL.

Step 3 ? Repair MyISAM Tables using myisamchk

Run the myisamchk command to repair MyISAM tables by following these steps:

NOTE: The myisamchk command does not work for the tables that use InnoDB engine.

myisamchk TABLE
myisamchk *.MYI
myisamchk ?recover TABLE

Refer to this link, for detailed information on ?How to repair MyISAM table by using myisamchk??

What if Nothing Works?

If the troubleshooting steps fail to resolve the issue, use Stellar Repair for MySQL software to repair the corrupt database and restore all its objects. The software can repair MySQL database on Windows and Linux systems.

Some Reasons Why You Should Use Stellar Repair for MySQL Software

Steps to Fix Corrupt MySQL Database with Stellar Repair for MySQL Software

Prerequisites:

Step 1: Download, install and launch Stellar Repair for MySQL software.

Step 2: In Select Data Folder window, select the MySQL version you are using.

Figure 1- Select Data Folder Window

Step 3: The Select Database window is displayed. Select the database you want to repair.

Figure 2- Select Database Window

Step 4: Click Repair to begin the repairing process.

Step 5: When Repair Complete dialog box appears, click OK.

Step 6: The software displays a preview of the repaired database and its recoverable components.

Step 7: Click Save on File menu to save the repaired database.

Figure 4 – File Menu

Step 8: In Save Database window, choose MySQL file format, and then click Save.

Step 9: When the ‘Saving process completed‘ message appears, click OK.

Figure 6 – Save Complete Message Box

The repaired database will be saved in the selected location.

End Note

When it comes to resolving database corruption in MySQL server, executing the troubleshooting steps discussed in this post may help you fix the issue.

But, if you are not an expert at handling database corruption or have a large size db (with gigabytes or terabytes of data), you may fail to restore the corrupt db with repair and recovery options. Also, recovery options such as innodb_force_recovery set to a value of 4 or greater may result in data corruption.

A better alternative is to use a purpose-built MySQL repair software to repair a corrupt database easily, in the original format.

Related Post