Wednesday, December 19, 2018

MySQL InnoDB lost tables but files exist

PROBLEM
=====================================================================
I have a MySQL InnoDB which has all the database table files, but MySQL doesn't see them, and isn't loading them.
The problem happened because I deleted these three files: ibdata1ib_logfile0 and ib_logfile1
because I was having issues with mysql starting up, and what I read was to remove them because MySQL will just regenerate them (I know I should have backed them up but didn't).
What can I do to get MySQL to see the tables again?

FIX:
======================================================================
Here is why MySQL cannot see those files: The system tablespace (ibdata1) has a Storage-Engine specific data dictionary that lets InnoDB map out potential table usage:
InnoDB Architecture
Moving InnoDB tables from one place to another requires commands like
ALTER TABLE tblname DISCARD TABLESPACE;
ALTER TABLE tblname IMPORT TABLESPACE;
Here is a part of the MySQL 5.5 Documentation explaining what needs to be considered
Portability Considerations for .ibd Files
You cannot freely move .ibd files between database directories as you can with MyISAM table files. The table definition stored in the InnoDB shared tablespace includes the database name. The transaction IDs and log sequence numbers stored in the tablespace files also differ between databases.
To move an .ibd file and the associated table from one database to another, use a RENAME TABLE statement:
RENAME TABLE db1.tbl_name TO db2.tbl_name; If you have a “clean” backup of an .ibd file, you can restore it to the MySQL installation from which it originated as follows:
The table must not have been dropped or truncated since you copied the .ibd file, because doing so changes the table ID stored inside the tablespace.
Issue this ALTER TABLE statement to delete the current .ibd file:
ALTER TABLE tbl_name DISCARD TABLESPACE; Copy the backup .ibd file to the proper database directory.
Issue this ALTER TABLE statement to tell InnoDB to use the new .ibd file for the table:
ALTER TABLE tbl_name IMPORT TABLESPACE; In this context, a “clean” .ibd file backup is one for which the following requirements are satisfied:
There are no uncommitted modifications by transactions in the .ibd file.
There are no unmerged insert buffer entries in the .ibd file.
Purge has removed all delete-marked index records from the .ibd file.
mysqld has flushed all modified pages of the .ibd file from the buffer pool to the file.
Given these caveats and protocols, here is a suggested course of action
For this example, let's try to restore the tags table to the mydb database

STEP #1

Make sure you have backups of those .frm and .ibd files in /tmp/innodb_data

STEP #2

Get the CREATE TABLE tags statement and execute it as CREATE TABLE mydb.tags .... Make sure it is the exact same structure as the original tags.frm

STEP #3

Delete the empty tags.ibd using MySQL
ALTER TABLE mydb.tags DISCARD TABLESPACE;

STEP #4

Bring in the backup copy of tags.ibd
cd /var/lib/mysql/mydb
cp /tmp/innodb_data.tags.ibd .
chown mysql:mysql tags.ibd

STEP #5

Add tags table to the InnoDB Data Dictionary
ALTER TABLE mydb.tags IMPORT TABLESPACE;

STEP 6

Test the table's accessibility
SHOW CREATE TABLE mydb.tags\G
SELECT * FROM mydb.tags LIMIT 10;
If you get normal results, congratulations you import an InnoDB table.

STEP 7

In the future, please don't delete ibdata1 and its logs



ALT.
UPDATED
First of all, about the files:
  • .frm - table structure;
  • .myd - table data;
  • .myi - indexes.
To recover tables, you can try (make backup first):
1) run check table tablename - for all db tables;
2) run repair table tablename - for necessary tables.

1 comment:

  1. CLASSIC NOTCH

    Have you ever wondered why people get so easily scammed by imposters all over the Web?

    It's no other reason than their vulnerability caused by desperation to get a problem fixed, which has led to Millions being ripped off their hard earned wealth and that's why Classic Cyber Notch has come to bring a stop to this wicked act and help already scammed victims get their hard earned wealth back.

    Classic Notch is a body of well Seasoned professional Hackers whom are well equipped with what it takes to getting your lost fund either probably through Binary options recovery scam back or lost via other means.

    We do jobs ranging from Securing Cyber Security stance to Cyber Hijacking of diverse types.

    Such as:

    ☑ Private investigating

    ☑ Phone hack

    ☑ Social media platform hack

    ☑ E-mail hack

    ☑ Recovery of passwords/documents

    ☑ Increase Credit score

    ☑ Clearing of Criminal records

    ☑ Binary options Recovery

    ☑ Issuing of Blank ATM

    ☑ Bit coin mining

    ☑ And many more.. Etc.

    ☑ Our success rate is top notch ☑

    Reach us via E-mail @

    Classic cyber notch at gmail dot com

    Classic cyber hacks at gmail dot com

    to put a smile back on your face.

    Signed,

    Collins .A.

    ReplyDelete