How do I repair an InnoDB table?
The following solution was inspired by Sandro's tip above.
Warning: while it worked for me, but I cannot tell if it will work for you.
My problem was the following: reading some specific rows from a table (let's call this table broken
) would crash MySQL. Even SELECT COUNT(*) FROM broken
would kill it. I hope you have a PRIMARY KEY
on this table (in the following sample, it's id
).
- Make sure you have a backup or snapshot of the broken MySQL server (just in case you want to go back to step 1 and try something else!)
CREATE TABLE broken_repair LIKE broken;
INSERT broken_repair SELECT * FROM broken WHERE id NOT IN (SELECT id FROM broken_repair) LIMIT 1;
- Repeat step 3 until it crashes the DB (you can use
LIMIT 100000
and then use lower values, until usingLIMIT 1
crashes the DB). - See if you have everything (you can compare
SELECT MAX(id) FROM broken
with the number of rows inbroken_repair
). - At this point, I apparently had all my rows (except those which were probably savagely truncated by InnoDB). If you miss some rows, you could try adding an
OFFSET
to theLIMIT
.
Good luck!
First of all stop the server and image the disc. There's no point only having one shot at this. Then take a look here.
stop your application...or stop your slave so no new rows are being added
create table <new table> like <old table>;
insert <new table> select * from <old table>;
truncate table <old table>;
insert <old table> select * from <new table>;
restart your server or slave