MySQL – How to Restore Tables from .ibd, .frm, and mysqllogbin Files

innodbMySQLmysql-5.6mysqlbinlogrecovery

For some reason, when I try to open up my tables that are stored in .frm and .ibd files (whether on MySQL or phpmyadmin) it gives me a syntax error, or it says it does not exist.

I've read the other post that had similar problem to this, but I don't know how to check if innodb_file_per_table is enabled, and I'm overall just really confused. I also converted a copy of my mysql-bin.000002 file to a txt file so I see that the data from my database is not completely lost.

The database was created last year. I have 6 of those mysql-bin.00000 files, but for some reason the .000002 is the largest. Right now, I have the .ibd and .frm files for all my databases, but I'm at a loss as to how I can restore it back into MySQL, or at least into something I can read.

I'm using WampServer 2.4 and MySQL 5.6.12 on Windows 2003 Server. Also, am I supposed to download a plugin in InnoDB?

Best Answer

I finally figured out and resolved my problem through a lot of trial and error. For those who do not have their original ibdata1 file, and only have their .frm and .ibd files, here's how I restored my data.

  1. Download and install the MySQL utilities at -> http://dev.mysql.com/downloads/utilities.
  2. Go into your command/terminal to open the MySQL utility, mysqlfrm, and use it to find the structure of your table that you need to restore. How I did that, was I cd'ed into the file location of mysqlfrm, then entered "mysqlfrm --server=user:pwd@localhost --port=3307 "path_to_.frm_file" > table_name.txt". The .txt file should be saved in the same folder as where your utilities are saved if you didn't specify where you wanted it to go.
  3. In the text file, you'll see the CREATE TABLE statements, that include all the columns and info (basically, the original structure) of your table. Copy that CREATE statement with all that info.
  4. In your MySQL Command, create a new database (CREATE DATABASE database_name). Make it the same name as your original database name.
  5. Create a new table inside the new database--it doesn't have to be the same name as the folder. You can create the new table inside the command prompt, but I created my table in PhpMyAdmin, a free software tool that handles the administration of MySQL over the web. I simply clicked on the database on PhpMyAdmin, then the SQL table, and pasted the table structure from #3. (As a side note, I always received errors if I named my tables "table" in my command prompt, so try to avoid that name).
  6. On your MySQL Command, go into your database, and enter "ALTER TABLE table_name DISCARD TABLESPACE", which will essentially remove this table's .ibd file.
  7. Copy your original table (the table you want to restore)'s .ibd file into the newly-created table to replace the .ibd file that you just removed. Change your initial .ibd file to the newly created table's name. This will mimic the old .ibd file that you just deleted. You can find this folder in the MySQL data folder, under the newly-created database folder on your computer.
  8. Go back to your MySQL Command, go into your database, and enter "ALTER TABLE table_Name IMPORT TABLESPACE." You'll receive a "warning" (1) error-type statement, but just ignore that.
  9. And done! if you try to access your new table, it should contain all the data from your old table.

I hope this helped, and let me know if you have any questions or comments! Also, check out http://www.chriscalender.com/?tag=innodb-error-tablespace-id-in-file for other details.