Documentation Home
MySQL 9.2 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 40.8Mb
PDF (A4) - 40.9Mb
Man Pages (TGZ) - 259.7Kb
Man Pages (Zip) - 366.9Kb
Info (Gzip) - 4.1Mb
Info (Zip) - 4.1Mb


17.18.2 InnoDB Recovery

This section describesInnoDB recovery. Topics include:

Point-in-Time Recovery

To recover anInnoDB database to the present from the time at which the physical backup was made, you must run MySQL server with binary logging enabled, even before taking the backup. To achieve point-in-time recovery after restoring a backup, you can apply changes from the binary log that occurred after the backup was made. SeeSection 9.5, “Point-in-Time (Incremental) Recovery”.

Recovery from Data Corruption or Disk Failure

If your database becomes corrupted or disk failure occurs, you must perform the recovery using a backup. In the case of corruption, first find a backup that is not corrupted. After restoring the base backup, do a point-in-time recovery from the binary log files usingmysqlbinlog andmysql to restore the changes that occurred after the backup was made.

In some cases of database corruption, it is enough to dump, drop, and re-create one or a few corrupt tables. You can use theCHECK TABLE statement to check whether a table is corrupt, althoughCHECK TABLE naturally cannot detect every possible kind of corruption.

In some cases, apparent database page corruption is actually due to the operating system corrupting its own file cache, and the data on disk may be okay. It is best to try restarting the computer first. Doing so may eliminate errors that appeared to be database page corruption. If MySQL still has trouble starting because ofInnoDB consistency problems, seeSection 17.20.3, “Forcing InnoDB Recovery” for steps to start the instance in recovery mode, which permits you to dump the data.

InnoDB Crash Recovery

To recover from an unexpected MySQL server exit, the only requirement is to restart the MySQL server.InnoDB automatically checks the logs and performs a roll-forward of the database to the present.InnoDB automatically rolls back uncommitted transactions that were present at the time of the crash.

InnoDBcrash recovery consists of several steps:

  • Tablespace discovery

    Tablespace discovery is the process thatInnoDB uses to identify tablespaces that require redo log application. SeeTablespace Discovery During Crash Recovery.

  • Redo log application

    Redo log application is performed during initialization, before accepting any connections. If all changes are flushed from thebuffer pool to thetablespaces (ibdata* and*.ibd files) at the time of the shutdown or crash, redo log application is skipped.InnoDB also skips redo log application if redo log files are missing at startup.

    • The current maximum auto-increment counter value is written to the redo log each time the value changes, which makes it crash-safe. During recovery,InnoDB scans the redo log to collect counter value changes and applies the changes to the in-memory table object.

      For more information about howInnoDB handles auto-increment values, seeSection 17.6.1.6, “AUTO_INCREMENT Handling in InnoDB”, andInnoDB AUTO_INCREMENT Counter Initialization.

    • When encountering index tree corruption,InnoDB writes a corruption flag to the redo log, which makes the corruption flag crash-safe.InnoDB also writes in-memory corruption flag data to an engine-private system table on each checkpoint. During recovery,InnoDB reads corruption flags from both locations and merges results before marking in-memory table and index objects as corrupt.

    • Removing redo logs to speed up recovery is not recommended, even if some data loss is acceptable. Removing redo logs should only be considered after a clean shutdown, withinnodb_fast_shutdown set to0 or1.

  • Roll back of incompletetransactions

    Incomplete transactions are any transactions that were active at the time of unexpected exit orfast shutdown. The time it takes to roll back an incomplete transaction can be three or four times the amount of time a transaction is active before it is interrupted, depending on server load.

    You cannot cancel transactions that are being rolled back. In extreme cases, when rolling back transactions is expected to take an exceptionally long time, it may be faster to startInnoDB with aninnodb_force_recovery setting of3 or greater. SeeSection 17.20.3, “Forcing InnoDB Recovery”.

  • Change buffer merge

    Applying changes from the change buffer (part of thesystem tablespace) to leaf pages of secondary indexes, as the index pages are read to the buffer pool.

  • Purge

    Deleting delete-marked records that are no longer visible to active transactions.

The steps that follow redo log application do not depend on the redo log (other than for logging the writes) and are performed in parallel with normal processing. Of these, only rollback of incomplete transactions is special to crash recovery. The insert buffer merge and the purge are performed during normal processing.

After redo log application,InnoDB attempts to accept connections as early as possible, to reduce downtime. As part of crash recovery,InnoDB rolls back transactions that were not committed or inXA PREPARE state when the server exited. The rollback is performed by a background thread, executed in parallel with transactions from new connections. Until the rollback operation is completed, new connections may encounter locking conflicts with recovered transactions.

In most situations, even if the MySQL server was killed unexpectedly in the middle of heavy activity, the recovery process happens automatically and no action is required of the DBA. If a hardware failure or severe system error corruptedInnoDB data, MySQL might refuse to start. In this case, seeSection 17.20.3, “Forcing InnoDB Recovery”.

For information about the binary log andInnoDB crash recovery, seeSection 7.4.4, “The Binary Log”.

Tablespace Discovery During Crash Recovery

If, during recovery,InnoDB encounters redo logs written since the last checkpoint, the redo logs must be applied to affected tablespaces. The process that identifies affected tablespaces during recovery is referred to astablespace discovery.

Tablespace discovery relies on theinnodb_directories setting, which defines the directories to scan at startup for tablespace files. Theinnodb_directories default setting is NULL, but the directories defined byinnodb_data_home_dir,innodb_undo_directory, anddatadir are always appended to theinnodb_directories argument value when InnoDB builds a list of directories to scan at startup. These directories are appended regardless of whether aninnodb_directories setting is specified explicitly. Tablespace files defined with an absolute path or that reside outside of the directories appended to theinnodb_directories setting should be added to theinnodb_directories setting. Recovery is terminated if any tablespace file referenced in a redo log has not been discovered previously.