How to Restore My SQL Database from Corruption?

Restore MySQL Database

MySQL provides different ways to analyze and also repair databases. It is essential to know that errors can arise for various reasons. However, it is important to know that database corruption can halt online operations, and data becomes unusable or unreadable. It’s critical to Repair MySQL Database on the server, as failure to do so properly can lead to data loss. You may also face some other issues such as server access problem or shut down due to corrupt MySQL server. 

Reasons for Causing Database Corruption in MySQL Server

Let’s look at some reasons for Database Corruption in MySQL Server.

  • Unexpected improper shutdowns or power outages will impact the databases
  • MySQL server software contains bugs
  • The process of MySQL is killed during the process of writing to the disk
  • Server hardware failure
  • Trying to move the database files in the MySQL data directory manually 
  • Unfortunately, the usage of third-party software to handle the Database crashed

These are some of the reasons you should know. Apart from these, db corruption mainly depends on using InnoDB or MyISAM storage engines. For information, MyISAM tables are vulnerable to corruption.

How do you handle it when MySQL Database gets Corrupted?

If the MySQL database is mainly corrupted, you must restore from the last known better backup copy. If you don’t have the backup, performing recovery options can fix MySQL database corruption at any time.

On the other hand, proceeding with the recovery process could be a good option if a backup is available, as bringing the Database back online will take less time than expected.

How to resolve MySQL Database Corruption

To resolve the issue, let’s check out the following steps below.

Step 1: Database Backup

Before repairing the corrupted Database, you must back up your data files immediately.

Though the Database is already corrupt, the backup will mainly help reduce the risk or prevent further damage. The recovery operation may cause it.

You need to stop the MySQL server to back up all the database files. You also need to create a backup copy of the respective datadir.

Step 2: In recovery mode, bring back the Database

When InnoDB is the default storage engine of MySQL Server, the database corruption, re-creating, dropping and dumping or few corrupt tables will work. In this case, you must view the check table statement to check the corrupt tables.

If the database corruption is high, you will face issues when starting the MySQL server. During this time, forcing InnoDB recovery could help you start up MySQL.

When using the innodb_force_recovery option, check out the steps below.

Now, open the MySQL configuration file and add the code to the [mysqld] section before restarting the server.

Well, you need to know that setting the value of the innodb_force_recovery option to 4 or above may increase the chances of data corruption.

[mysqld]

Innodb_force_recovery=1

All you need to do is save and close the MySQL configuration file and then move ahead to restart the MySQL service.

When you start MySQL, you need to dump all the MySQL databases to respective single files by handling the following command:

Mysqldump –u [user] –p[password] –all-databases > all_databases.sql

Now, you need to disable InnoDB recovery mode. To make it possible, comment on the below line in the section [mysqld].

#innodb_force_recovery=. . .

After that, you should save the changes to the configuration file and move ahead to start the MySQL server.

Now, restore the databases from the backup file that was created.

mysql> source all_databases.sql

Step 3: Using myisamchk repair MyISAM

To repair MyISAM tables, you must run myisamchk by following the steps below.

You need to stop the MySQL server

Important to use the following command for checking out the corrupt tables

myisamchk TABLE

By approaching the corrupt Database and would like to check out the tables, follow the below command:

myisamchk *.MYI

Now, it is essential to repair a corrupt table by executing the respective command below.

myisamchk –recover TABLE

Now, you need to start the MySQL server

What if it does not work?

If the troubleshooting steps fail to sort out the issue, proceed with Stellar Repair for MySQL software. This software helps to repair the corrupt Database and restore objects. It will also be helpful in repairing the MySQL database on Linux and Windows systems.

Essential reasons why you should access Stellar Repair for MySQL software

  • Solve MySQL db tables that utilize MyISAM (.frm, .myd, .myi) and InnoDB (.frm, .ibdata, idb) database engines.
  • The respective batch mainly repairs MySQL databases in multiple numbers in a single process.
  • It is possible to recover all the database objects like triggers, views, data types, table properties, tables and keys.
  • Availability of preview to check out recoverable database objects.
  • It allows saving the repaired Database in various file formats like XLS, HTML, CSV and MySQL.

Essential steps to fix corrupt MySQL Database using Stellar Repair

Requirements:

  • The ‘ibdata’ file location must be the same as the MySQL database.
  • You need to have that the destination drive has free space to store recovered MySQL databases.
  • MySQL should be installed on the system.

Step 1: You must launch Stellar Repair for MySQL software by completing the download and installation process.

Step 2: You must choose the MySQL version by entering the Select Data Folder.

Step 3: A Select Database window will appear. Select the Database you would like to repair.

Step 4: Now, tap Repair to start the repairing process.

Step 5: Click OK when the Repair Complete dialog box appears.

Step 6: The software will mainly display the repaired database preview and recoverable components.

Step 7: You must tap Save on the File menu to save the repaired databases.

Step 8: By approaching the Save Database window, you can choose MySQL file format and tap Save.

Step 9: If the ‘Saving process completed’ message displays, tap OK.

Finally, the repaired Database will be saved in the respective selected location.

Conclusion

When facing MySQL database corruption, immediate backup to secure your data is vital despite the data being compromised. Built-in tools like myisamchk for MyISAM and innodb_force_recovery for InnoDB are essential to diagnose and fix the issues. If you have the backups, then that should be a high priority to restore data integrity. If you face severe problems, approaching professional recovery software like Stellar Repair for MySQL will help you access the problematic recoveries. So, maintain a backup strategy regularly to avoid further risks. 

Post a Comment

0 Comments