How to Recover Corrupt MySQL Database in Simple Steps

One of the essential tasks of DBAs is to keep the MySQL Database updated and running. However, there are scenarios when the database gets corrupted leading to inconsistencies and data inaccessibility. Also, it affects the overall performance of the organization. So, to overcome the corruption issue and recover the corrupt MySQL database, this blog states effective strategies. Also, find a MySQL Database Recovery Tool for swift handling of database recovery without losses.

Download Buy Now

What Causes MySQL Database Corruption?

The significant reasons for the corruption in MySQL database are mentioned here:

  • Sudden power failure or shutdown in MySQL results in a failed server, disturbing the data integrity.
  • Hardware and software failures due to faulty disks and application-level issues.
  • Wrong or incompatible data types and transactions to modify the database tables.
  • Incorrect or misplaced configuration of MySQL storage engine (InnoDB and MyISAM).
  • Low disk space to perform the important MySQL operations, like, read and write.
  • Executing incomplete, incorrect, or wrong SQL statements and deleting crucial files.

Thus, it becomes necessary to recover a corrupted MySQL database to avoid the harm and losses that may occur. Moving on, browse the next section to explore all the possible ways in which you can fix the MySQL database quickly.

Best Practices to Recover Corrupt MySQL Database

Some efficient troubleshooting solutions to repair and recover a corrupt MySQL database are mentioned here. It covers free manual methods and an expert-suggested professional tool in detail. Undergo these methods to find the best solution for you depending on the severity of your MySQL database corruption.

Traditional Solutions for MySQL Fix Database Corruption

Before proceeding to the given manual solutions ensure to take a backup of your MySQL database. Additionally, you must have sufficient disk space, administrator privileges, and access to the MySQL server.

Method 1: Repair MySQL Corrupted Database Using MySQLCheck Command

  1. Start a command line terminal on the system that hosts MySQL Server.
  2. Then, use the command mysqlcheck db_name to check for the database.
  3. Further, run the command mysqlcheck db_name table_name to check for a specific table in the database.
  4. To repair the specific corrupted table, run the command: mysqlcheck –r db_name table_name.
  5. Finally, to repair all the MySQL databases, run the mysqlcheck –repair –all-db command.

Method 2: Recover Corrupt MySQL Database from the Backup

To use this method, you must have created a regular backup of your database using the mysqldump utility. The steps are:

  1. Create an empty database with the command: mysql > create database_name.
  2. Next, restore the database by entering the command: mysql -u root -p database_name < backuppath.sql.
  3. Lastly, check the restored database tables by the command: mysql> use database_name; mysql > show tables.

Method 3: MySQL Fix Database Corruption by Rebuilding Database

This method is divided into two parts as per the InnoDB and MyISAM Storage Engines.

For InnoDB Storage Engine
  1. Type service.msc in the Run dialog box and click OK.
  2. In the service window, right-click on MySQL Service and press Restart Service.
  3. Further, to start the MySQL service, you need to use the innodb_force_recovery option. However, first, you need to find the option from the configuration file.
    • Search for my.cnf file on your system. (Generally, its location varies depending on your OS. Yet, the default path for it is /etc/mysql/my. conf.)
    • Once you find the configuration file, locate the mysqld section and input these commands:

      mysqld

      Innodb_force_recovery=1

      service mysql restart

    Note: The default value of force recovery is set to O. But to start the InnoDB engine and dump tables, we need to set it to 1. Additionally, dumping tables with values higher than 4 can cause losses. So be sure to keep a backup to recover a corrupt MySQL database.

  4. Now, to dump the table, run the command given below:
  5. mysqldump -u user -p db_name table_name > single_dbtable_dump.sql

  6. Afterward, run the following command to export all the databases to dump.sql:
  7. mysqldump –all-db –add-drop-db –add-drop-table > dump.sql

  8. Restart the SQL Server and input the DROP DATABASE command to drop the MySQL database.
  9. In case it doesn’t work and fails, run the command: cd /var/lib/mysql
  10. rm -rf database_name to drop manually.

  11. Furthermore, disable InnoDB by commenting on the #innodb_force_recovery=… line in mysqld.
  12. In the end, save all the changes in the my.conf file and restart the MySQL Server.
For MyISAM Storage Engine
  1. Firstly, close the MySQL Server and check for the corrupted MyISAM table with the command: myisamchk TABLENAME.
  2. To recover it, run the command, myisamchk –recover TABLENAME, and then, start MySQL Server.

Following the above-given methods, you have repaired the MySQL database successfully. However, these methods have some limitations.

Shortcomings of the Manual Method

  • The processes are difficult to perform and take a lot of time.
  • Users need high technical expertise to execute the commands.
  • There are chances of severe losses and incomplete recovery.

Automated Tool to Recover Corrupt MySQL Database

As manual methods are tricky and involve a lot of complex processes, opt for the SysInfo MySQL Database Recovery Tool. It enables to perform seamless data recovery without extra effort and saves time with minimum steps. It enables seamless data recovery without extra effort and saves time. The tool is proficient in restoring all tables, keys, properties, views, etc. from the database. It provides a simple interface and allows to preview the data before recovery. Moreover, it supports all MySQL Server and Windows OS versions without any hassle.

Using this specialized tool, recover MySQL databases from ibdata1, InnoDB, and MyISAM storage. The steps to recover a corrupt MySQL database are explained briefly here:

  1. Run the software on the PC and click Open from the top menu.
  2. Press (…) to add the MySQL database file to the tool. Then, click OK.
  3. If prompted to add, the ibdata1 file, click Yes and choose the file to add.
  4. Further, open it and preview all the database tables from the tree format.
  5. Afterward, press Save MySQL Data and choose to save as Database or Scripts.
  6. Input the other credentials accordingly. At last, press OK to complete the recovery.

Isn’t this a more straightforward way to fix the corrupted MySQL databases?

Conclusion

The troubleshooting methods discussed here can help recover corrupt MySQL databases with ease. However, the manual methods encompass risk and are not so easy to execute. If you cannot take a chance with your crucial data, use a professional tool as suggested above. It restores data with complete integrity and can handle multiple databases at once.

Frequently Asked Questions

Q. How to fix a corrupt MySQL database?

Ans- Follow the methods given below to fix corruption in the MySQL database:
1. Use mysqlcheck command
2. Recover from the backup
3. Use the InnoDB and MyISAM storage engines
4. Opt for the best professional tool, i.e., MySQL Database Recovery Software.

5/5 - (1 vote)

About The Author:

Simran Bhatia is a technical content writer engaged in writing clear, concise, and SEO-optimized content. With a background in computer science and a passion for writing, I thrive to deliver complex technical content in simple layman terms.

Related Post