有時候需要隻有在.ibd檔案的情況下恢複資料。我們盡力将它load到新的執行個體或者其他執行個體當中,可能會遇到 table id 的錯誤。
我這裡有兩種方式來恢複單個ibd資料。
前提是:你需要.ibd的檔案,和對應該表的 CREATE TABLE 語句。
第一種:模拟Innodb internal table id計數方式。啟用innodb_file_per_table,建立work table,直至internal table id 等于 (要恢複表的table id -1)
第二種:手動修改16進制的.ibd檔案來改變table id
最後,由于innodb中繼資料的中内部結構,我們需要對dump import 我們已經恢複的表。
方法1、建立 work table.
1、建立立一個MySQL 執行個體,并啟用innodb_file_per_table
2、找到work table 在新執行個體中的table id,和需要恢複表的table id,
note:
對于第二步(2a--2f)詳細過程是找出執行個體中各個.ibd檔案對應的table id,我已經寫了php 腳本來做這個事情,
2a:建立測試庫:
2b:在test1中建立對應.ibd(需要恢複的表) 檔案的資料表
2c:删除表空間
2d: 拷貝原先的.ibd 檔案(需要恢複的表的資料檔案)到 test1的資料庫目錄下
2e:導入表空間
該步驟通常情況下會報錯,除非導入的表空間對應的table id 等于建立表的table id
報錯資訊;
2f:檢查error.log ,我們能夠找到該.ibd檔案的 table id
我們知道 internal table id 是1,.ibd file對應的 table id 是1193
3、清空 test1
3a、手動移動 .ibd 檔案到 到其他位置(一會兒還會需要)
3b、删除表
這個步驟不會重設 internal table 計數器
4、建立相應數量的表來使 internal table id 的值增加
在這個案例中,我們需要在 test1中建立 1191個 innodb table,(table id 1已經被占用,需要比對應.ibd 檔案的table id 小1, 是以是 1193-2=1191 )
執行下面的程式:
注:我已經用Php 程式搞定
5、完成以上步驟後,删除所有的db 和 table。
drop database test1;
6、重新執行步驟 2a-2e
拷貝對應的.ibd file 到對應的 test1資料庫目錄
7、用mysqldump來備份該表(這一步必須執行),然後可以在任何一個執行個體中進行恢複。
以上的情況常常發生在資料庫 crash 或者表空間損壞的情況下。
如果發生以上 情況,先嘗試force innodb recovery 并dump出資料。 從 1開始innodb_force_recovery=1 (and try 2,3,4,5,6) 直到能夠dump出資料。
對于以上的例子,我是設定 innodb_force_recovery=5 來解決問題。
以下是我的操作記錄:
設定為5的原因是由于以下error中的資訊:
以上是 undo log中的資訊,文檔中設定為5的解釋是:
"Do not look at undo logs when starting the database: InnoDB treats even incomplete transactions as committed"
方法二 修改.ibd file
在此之前先備份資料(ibdata file,ib_logfile, data)
按照下面的 1-5步來 操作:
<a href="http://dev.mysql.com/doc/refman/5.0/en/adding-and-removing.html"> http://dev.mysql.com/doc/refman/5.0/en/adding-and-removing.html</a>
Let me post them here for completeness, however:
Use mysqldump to dump all your InnoDB tables.
Stop the server.
Remove all the existing tablespace files, including the ibdata and ib_log files. If you want to keep a backup copy of the information, then copy all the ib* files to another location before the removing the files in your MySQL installation.
Remove any .frm files for InnoDB tables.
Configure a new tablespace.
Restart the server.
Import the dump files.
重複 2a-2f的過程,來獲得 該.ibd file的table id,
在windows上使用 Freeware Hex Editor XVI32 (http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm)
以下是修改部分:
For me, and I assume it should be the same for you, but just look at the values to be sure, I see the tablespace id values listed at position 37 and 41 (positions 25 and 29 in hex). In the actual hex column, if you're previous tablespace id was 2, then in positions 37 and 41, you'd see 02 and 02.
(Note these positions can change. For instance, I tested on a table with an internal id of 1193. This in hex is 04A9. However, when searching the file, for the first instance of the table id, I found the '04' in position 39 and 'A9' in position 40. Then, for the second instance of the table id, the '04' was at position 43 and the 'A9' was at position 44. So, you'll have to convert the table id to hex, and then search for that value, near the beginning of the file.)
Note that this value (02) may vary depending on what your actual tablespace id is.
Then, simply modify both of those fields to 01, and save the file.
再執行下面的部分:
以下是涉及到的 php 腳本: