MySQL Won’t Start Because of InnoDB Table Corruption (Repair and Recover Database)

Hardware failures and power outages are the usual cause of database crashes, corrupted tables, and bad data.

Normally, as MySQL starts it will check itself and attempt to automatically recover from the previous crash (by redoing incomplete transactions using ib_logfiles). But when MySQL encounters an abnormal issue or corrupt InnoDB data, it will refuse to start and will let the user decide what to do next.

InnoDB: Database was not shutdown normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files…

InnoDB: Error:…

Before attempting to repair the existing database: if you have a recent database backup (either a full SQL dump-file or a directory copy), you can rebuild the database to the last transaction recorded by using MySQL’s Binary Log (if the Binary Log’s “expire_logs_days” value is still within the backup date).

If the issue is that an InnoDB table has become corrupt, you have to attempt to get as much good data from it, and then manually reload it…

If the corrupt table is not important, you can skip all the steps, and just delete its IBD file (\Database\mysql-data\mysql\table-name.ibd).

1. First you have to make a backup of the entire \Database directory (as data and files are going to be changed and mistakes can happen easily).

“7zip” is a good compression utility, that will properly zip/unzip NTFS Junction Point / folder-links (some apps will not), which this folder contains.

2. Open file Config\Mysql\my.ini (in Notepad), and in section “[mysqld]” add in line:

innodb_force_recovery = 1

Save file. Attempt to start MySQL.

The value above (“1″) specifies which level of regular startup checks MySQL will skip over – so it can start. The value goes from 1 to 6, 4 + is considered dangerous, and the minimum value should be used to get MySQL to start…

This will also put the Database into a predominantly read-only mode (but you can still DROP tables).

3. If MySQL starts, mysqldump (export) the corrupt table, then DROP (remove) it…

Open the command-line (Systems tab, button: Command Line).

Export the specific table into an SQL file –

mysqldump -u root -p database.name table.name > database.table.sql

(* it will ask you for the root password, if it is not set then just leave the “-p” switch out)

The above exported table copy will only contain table rows/entries that MySQL could read (and it is possible that no data could be recovered from the table).

Then access the MySQL shell –

mysql -u root -p

(* it will ask you for the root password, if it is not set then just leave the “-p” switch out)

Drop (remove) the corrupt table which is preventing MySQL from starting –

use database.name;
drop table table.name;

Exit the MySQL shell –

exit;

4. Restart MySQL in normal recovery mode (undo the my.ini edit), and re-import the “recovered” table. This table will only contain non-corrupted rows/entries; it will likely have some data missing…

Remove from file my.ini, section “[mysqld]” –

innodb_force_recovery = 1

Save file. Start MySQL.

Open the command-line.

Import the table –

mysql database.name < database.table.sql

5. If more than 1 table is corrupted, check MySQL log and repeat the above process.

6. If the corrupt table issues are fixed, but MySQL still has some problems starting due to “binary log files errors”…

Move out the “mysql-bin.*” files that are in \Database\mysql-data\ (but not the ib_logfile*, nor ibdata* files).

7. Once MySQL is running, you can also perform a general check and repair of all databases –

mysqlcheck -u root -p --auto-repair --check --optimize --all-databases

3 thoughts on “MySQL Won’t Start Because of InnoDB Table Corruption (Repair and Recover Database)”

  1.  2016-08-19 16:10:56 5784 [Note] InnoDB: The log sequence numbers 65927719 and 65927719 in ibdata files do not match the log sequence number 66237592 in the ib_logfiles!
     2016-08-19 16:10:56 5784 [Note] InnoDB: Database was not shutdown normally!
     2016-08-19 16:10:56 5784 [Note] InnoDB: Starting crash recovery.
     2016-08-19 16:10:56 5784 [Note] InnoDB: Reading tablespace information from the .ibd files...
     2016-08-19 16:10:57 f30  InnoDB: Operating system error number 2 in a file operation.
     InnoDB: Error: could not open single-table tablespace file .\db_name\table_name

    If the underlining problem is not significant, and/or you are not too worried about possible loss of some data (depending on many factors this might not even result in any loss), you can usually just perform this to get things working again –

    [This could result in loss of data.]

    Delete (or move out) the ib_logfile* and the ibdata* files that are in MySQL’s data directory (\WampDeveloper\Database\mysql-data\).

    MySQL will recreate them automatically.

Leave a Reply to DG Cancel reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>