Skip to content

Instantly share code, notes, and snippets.

@premitheme
Last active September 3, 2024 20:56
Show Gist options
  • Save premitheme/c45a036e9c6d62662d081c302f51ff20 to your computer and use it in GitHub Desktop.
Save premitheme/c45a036e9c6d62662d081c302f51ff20 to your computer and use it in GitHub Desktop.
Recover MAMP MySQL database using .frm and .ibd files after InnoDB crash

Recover MAMP MySQL database using .frm and .ibd files after InnoDB crash

After a power faliur (also can be a sudden restart or system crash), I ended up with corrupted database and lost the access to my local hosted websites for development. Even the MAMP's MySQL server was not starting.

Preparation

You will need to find the databases folders, in case of MAMP they are located in Applications/MAMP/db/mysql56 (or mysql57 depending on MySQL version).

You will find folders containing the database name, inside them you will find .frm and .ibd files. Take a copy of the entire folder for backup in another place, the desktop for example.

Go back to the Applications/MAMP/db/mysql56 and delete 3 files, ib_logfile0, ib_logfile1 and ibdata1. This will get your MAMP's MySQL server back to work.

We will use dbsake on macOS to recover tables structure.

Install dbsake

In the terminal type the following commands:

curl -s http://get.dbsake.net > dbsake

chmod u+x dbsake

Then insure that you installed dbsake properly by typing the following command:

./dbsake --version

You should get something like this:

dbsake, version 2.1.2 (git: 50de953)

Restore Tables Structure

In the in database backup folder on the Desktop, for every .frm in the database folder, do the following command in the terminal:

./dbsake frmdump /path/to/database/folder/filename.frm

HINT: Just type ./dbsake frmdump (notice the sapce at the end of the line), then drag the .frm file to the terminal window and the path will be inserted automatically.

The command will return SQL query that will be used to create the table in the database via phpMyAdmin, Sequel Pro or similar tools. You will get something like this:

  CREATE TABLE `wp_options` (
    `option_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `option_name` varchar(191) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
    `option_value` longtext COLLATE utf8mb4_unicode_520_ci NOT NULL,
    `autoload` varchar(20) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT 'yes',
    PRIMARY KEY (`option_id`),
    UNIQUE KEY `option_name` (`option_name`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

Copy this code and go to phpMyAdmin, delete (drop) the old corrupted database and create new empty one with the same name (don't forget to take a backup copy as stated above), click on it and go to SQL tab and passte the code. A table will be created.

Repeat this with every .frm file you have in the database folder. And now you have restored the database with tables structure but it has no data.

Restore Old Data

Now in the same SQL tab of the database in phpMyAdmin, paste the folowing SQL query (Change wp_options with the table name that you have):

ALTER TABLE wp_options DISCARD TABLESPACE;

This code will unlink and delete the empty, newly created .ibd files. Thses files holdes the actual data of the database tables, while .frm holds the tables structure only. Repeat this line of code for every database table you have (of course change the table name evry time).

After that, go to the database backup folder on the Desktop and copy all the .ibd files there and paste them in the newlycreated database folder in Applications/MAMP/db/mysql56 (or mysql57).

Then, in the same SQL tab of the database in phpMyAdmin, paste the folowing SQL query (Change wp_options with the table name that you have):

ALTER TABLE wp_options IMPORT TABLESPACE;

This will link the new database tables to the old .ibd files which hold the actual old data.

Voila! you're done and you should get your local sites working again.

@James-Did-It
Copy link

This is a god-send. Thank you!

@premitheme
Copy link
Author

Glad it helped.

@AleksIralda
Copy link

Life-saver Snippets 🙌

@dennismunene
Copy link

Thank you for this , saved me a lot of trouble!

@shishir-bd
Copy link

Life saver man, thanks a lot, i was broke.
Thanks n times

@guhill1
Copy link

guhill1 commented Feb 13, 2023

Thanks, this is the most helpful to me.

@elpadjo
Copy link

elpadjo commented Jun 11, 2023

Very Detailed and works, Thanks

@silllli
Copy link

silllli commented Jun 28, 2023

You can simply enter the database directory’s path, not the individual .frm files’, using the -r (recursive) option and dbsake will automatically look for all .frm files in there

@FlowinBeatz
Copy link

FlowinBeatz commented Jul 10, 2024

This was a life saver! Thanks so much.

To make this process faster you can use ChatGPT:

  1. Write all the SQL queries that were created by dbsake into a .sql file
  2. Upload this file into ChatGPT and ask it to write down all names of the used db tables
  3. Take that list and make a find and replace in your favorite text editor / IDE around the table names to create the DISCARD queries in one long query
  4. Import the .ibd Files
  5. Make a find and replace from DISCARD to IMPORT for the last step

Saves you a lot of single queries.

Take care to not upload sensitive data such as passwords etc. into ChatGPT!

@mahendraai
Copy link

Life saver
but if we crashed 10.2 mariadb
and now we have mariadb 10.5
same solution work for it or not because solutions is not working or not
I am facing error when import table space

Error

SQL query: Copy

ALTER TABLE wp_actionscheduler_claims IMPORT TABLESPACE;

MySQL said: Documentation
#1815 - Internal error: Drop all secondary indexes before importing table xyzdomain/wp_actionscheduler_claims when .cfg file is missing.

@mahendraai
Copy link

ALTER TABLE wp_options IMPORT TABLESPACE;
this command has error

@mahendraai
Copy link

Thank you for this , saved me a lot of trouble!

But I am facing error

@mahendraai
Copy link

Very Detailed and works, Thanks
can you explain my now works

@mahendraai
Copy link

SQL query:

ALTER TABLE wp_actionscheduler_claims IMPORT TABLESPACE

MySQL said: Documentation
#1808 - Schema mismatch (Table has ROW_TYPE_COMPACT row format, .ibd file has ROW_TYPE_DYNAMIC row format

@Supanaught
Copy link

Thank you so much for taking the time to transcribe this process in detail, an asbolute life saver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment