Advertisement
database Learn how to fix a corrupted InnoDB database using simple and beginner-friendly recovery techniques. This guide explains how to recover MySQL after a crash, repair damaged tables, restore .ibd files, rebuild ibdata1, and safely bring your server back to life. Perfect for beginners, developers, and system administrators who have no idea what to do when MySQL suddenly refuses to start.

How to Repair a Corrupted InnoDB Database: A Complete Step-by-Step Guide for Beginners

5 Min Read Verified Content

If you’ve ever woken up to find your MySQL server refusing to start with scary messages like:

InnoDB: Corruption detected InnoDB: Cannot read ibdata1 InnoDB: Page corruption

—don’t panic.
InnoDB corruption happens more often than you think, especially on low-budget VPS, sudden power failures, or servers that get killed by force.

This tutorial will walk you through every common InnoDB recovery method, starting from the easiest ones to the more advanced “disaster recovery” techniques. Even if you’re a complete beginner or someone who calls themselves a “dummy”, you can follow these steps safely.

Let’s rescue your database.



# 🧪 Part 1 — First: Understand What Type of InnoDB Corruption You Have


Before recovering anything, identify the situation. These are the most common cases:


1. MySQL cannot start (global InnoDB corruption)

Symptoms:

  • MySQL service fails to start

  • Errors mention “ibdata1” or “redo log corruption”


2. Only one table is corrupted

Symptoms:

  • Database starts fine

  • But one table throws:

ERROR 1812: Table is corrupted

3. You only have .ibd or .frm files (manual recovery needed)

Symptoms:

  • You migrated manually

  • ibdata1 is missing

  • MySQL says “space id mismatch”


4. ibdata1 is destroyed completely

Symptoms:

  • MySQL crashes instantly

  • All InnoDB tables are unreadable

Each scenario has different solutions, and we’ll cover all of them.




# 🧯 Part 2 — Method 1: Start MySQL in InnoDB Recovery Mode (Beginner Friendly)


This is the safest and most beginner-friendly way to recover a corrupted database.


Step 1 — Edit your MySQL config file

Open:

sudo nano /etc/mysql/my.cnf

Add:

[mysqld] innodb_force_recovery = 1

Restart MySQL:

sudo systemctl restart mysql

If it still fails, increase the level:

1 → 2 → 3 → 4 → 5 → 6

⚠️ Warning:
Levels 4–6 make InnoDB read-only. Use them only to extract data, not for normal operations.




Step 2 — Export ALL your databases

Once MySQL successfully starts:

mysqldump --all-databases > full-backup.sql


Step 3 — Reinstall MySQL (Fresh System)

Stop MySQL:

sudo systemctl stop mysql

Remove storage files:

sudo rm -rf /var/lib/mysql/*

Start MySQL again:

sudo systemctl start mysql

Restore:

mysql < full-backup.sql

Done.
This method fixes about 70% of corruption issues.




# 🧯 Part 3 — Method 2: Fix Only One Corrupted Table


If MySQL starts normally but one table refuses to work, do a targeted repair.

Step 1 — Check the table

CHECK TABLE table_name;

If it says corrupted, proceed.




Step 2 — Repair with ALTER FORCE

InnoDB doesn’t support REPAIR TABLE, but you can rebuild it using:

ALTER TABLE table_name FORCE;

MySQL will recreate and reorganize the table internally, repairing many corruption issues automatically.




# 🧯 Part 4 — Method 3: Recover a Table Using Its .IBD File Only


This scenario is extremely common for beginners:
you only have:

table_name.ibd table_name.frm (maybe)

—but not the original ibdata1.

When MySQL loads the .ibd file, it throws:

table id does not match space id mismatch

Here’s how to fix it.




Step 1 — Recreate or restore the table structure

If you still have .frm files:
Great — they contain the table structure.

If not, extract it using MySQL utilities:

mysqlfrm --diagnostic /path/to/file.frm > create.sql

Or recreate the structure manually if you know the schema.




Step 2 — Discard the tablespace

This tells MySQL:

“Forget your current tablespace, I’m going to give you a new one.”

ALTER TABLE table_name DISCARD TABLESPACE;


Step 3 — Move the .ibd file back

cp /backup/table_name.ibd /var/lib/mysql/database_name/

Set correct permissions:

chown mysql:mysql table_name.ibd


Step 4 — Import the new tablespace

ALTER TABLE table_name IMPORT TABLESPACE;

If it succeeds → congratulations, your table is back.
If it fails → space_id mismatch, continue to Method 4.




# 🧨 Part 5 — Method 4: Recover When ibdata1 Is Completely Destroyed (Full Disaster Mode)


This is the worst-case scenario.

Your MySQL shows errors like:

InnoDB: Cannot read the data dictionary InnoDB: ibdata1 is corrupted

Here’s what to do.



Step 1 — Move the corrupted files away

mv /var/lib/mysql/ibdata1 /root/recovery/ mv /var/lib/mysql/ib_logfile* /root/recovery/


Step 2 — Start MySQL (it will recreate fresh files)

sudo systemctl start mysql


Step 3 — Recover each table manually

Using the discard + import tablespace method (Method 3).

This is slow, but effective.




# 🧨 Part 6 — Method 5: Extract Rows from a Damaged InnoDB File


Even if the .ibd file is corrupted, you can still extract data.

Install InnoDB tools:

sudo apt install percona-toolkit

Or use:

  • innodb_ruby

  • InnoDB Data Recovery Toolkit

Example:

innodb_space -f table_name.ibd space-indexes innodb_space -f table_name.ibd page-dump > rows.txt

This allows you to manually extract rows even if MySQL refuses to load the table.




# 🧨 Part 7 — Method 6: Use Percona Recovery Tools (Highly Recommended)


Percona has the most reliable MySQL recovery toolkit.

Install:

sudo apt install percona-toolkit

Useful tools:

  • pt-innodb-summary

  • pt-table-sync

  • pt-duplicate-key-checker

  • Percona Data Recovery Tool for InnoDB (advanced)

If nothing else works, Percona tools usually save the day.




# 🛡️ How to Prevent InnoDB Corruption in the Future


Follow these rules and you’ll avoid 90% of corruption issues:

✔ Use a reliable VPS (avoid cheap unstable storage)
✔ Use a UPS or cloud-based MySQL
✔ Never kill MySQL with "kill -9"
✔ Avoid full disk conditions
✔ Configure:

innodb_flush_method = O_DIRECT innodb_flush_log_at_trx_commit = 1

✔ Always keep backups
✔ Enable binary logs
✔ Monitor disk errors (smartctl)




# ⭐ Conclusion


InnoDB corruption feels terrifying, especially for beginners…
but with the correct approach, it’s absolutely recoverable.

You now know how to:

✔ Start MySQL in safe recovery mode
✔ Repair a single corrupted table
✔ Import .ibd files safely
✔ Rebuild a destroyed ibdata1
✔ Extract data from corrupted files
✔ Use Percona tools for deep recovery

Advertisement
Back to Database