好好的表在MySQL5.6 就Table xxx.xxx dont't exist了

錯誤提示:

Error Code: 1146.
Table mytest.events don't exist.
Please DISCARD the tablespace before IMPORT.


相關原因:

       使用hibernate4.2.10 教程時,練習多對多關聯,person、person_event、events,這三個表進行關聯,建立關聯表的 person_event 時候沒有在MySQL數據庫中設置"外鍵",對數據庫中的表進行

HQL : select p from Person p left outer join p.event where p.id=:pid

等操作並進行存取後,成功存取,關閉數據連接,關閉數據庫服務,再次打開數據庫服務,運行程序,提示Table mytest.events don't exist. 然後打開數據庫,在數據庫中圖形界面點擊查詢events表,提示相同的錯誤。


嘗試操作:

刪除events表,提示

Error: table `mytest`.`events` does not exist in the InnoDB internal
data dictionary though MySQL is trying to drop it.
Have you copied the .frm file of the table to the MySQL database
directory from another database?

重啓數據庫服務後發現表不見了,然後開始建立新表,提示:

Cannot create file '.\mytest\events.ibd'
The file '.\mytest\events.ibd' already exists though the
corresponding table did not exist in the InnoDB data dictionary.
Have you moved InnoDB .ibd files around without using the SQL
commands DISCARD TABLESPACE and IMPORT TABLESPACE, or
did mysqld crash in the middle of CREATE TABLE? You can
resolve the problem by removing the file '.\mytest
\events.ibd' under the 'datadir' of MySQL.


網上尋了一些方法[雖然沒能解決我的問題,但是知道了一些常用的方法]:

不能建立新表,所以直接找到數據庫存數據的地方直接刪除相關文件,找到存數據文件的文件夾

D:\MySQL_Data\MySQL_Server5.6\data\mytest\

發現其他表都有*.frm文件,而events沒有events.frm,可能是之前在數據庫中刪除表的時候只刪除了這個文件的原因,只剩下events.ibd了,也直接刪除。

再次打開數據庫,看看數據庫裏的其他表,發現person_event表也提示相同的錯誤。

網上提示使用工具MySQL自帶的myisamchk ,所以命令行進行如下操作:

D:\MySQL\MySQL Server 5.6\bin>myisamchk
-of D:\MySQL_Data\MySQL_Server5.6\data\mytest\person_event.ibd

提示該表不是ISAM表,建立的表默認是InnoDB,所以改用 innochecksum ,

D:\MySQL\MySQL Server 5.6\bin>innochecksum
-v D:\MySQL_Data\MySQL_Server5.6\data\mytest\person_event.ibd

輸出 :

InnoDB offline file checksum utility.
Variables (--variable-name=value)
and boolean options {FALSE|TRUE}  Value (after reading options)
--------------------------------- ----------------------------------------
verbose                           TRUE
debug                             FALSE
count                             FALSE
start-page                        0
end-page                          0
page                              0
file D:\MySQL_Data\MySQL_Server5.6\data\mytest\person_event.ibd = 1146
88 bytes (7 pages)...
InnoChecksum; checking pages in range 0 to 6
Fail;  page 0 invalid (fails old style checksum)

繼續執行:mysqlcheck

D:\MySQL\MySQL Server 5.6\bin>mysqlcheck -a -u root -p mytest
Enter password: ****
mytest.person                                OK
mytest.person_email_addr                     OK
mytest.person_event
Error    : Table 'mytest.person_event' doesn't exist
status   : Operation failed
mytest.sys_user                              OK

不管用......


解決方法:

       經過了上面的嘗試和網上方法的提示,忽然想到查看一下數據庫的錯誤日誌,

於是找到到存數據的目錄:

D:\MySQL_Data\MySQL_Server5.6\data

在這裏找到了錯誤日誌文件:

PCNAME-yymmddHHMM.err

裏面羅列了好多內容,其中一些是這樣寫的:


2014-03-21 23:46:54 100300 [Warning] InnoDB:
    Load table 'mytest/person_event' failed,
    the table has missing foreign key indexes.
    Turn off 'foreign_key_checks' and try again.
2014-03-21 23:46:54 100300 [Warning] InnoDB:
     Cannot open table mytest/person_event from the internal
    data dictionary of InnoDB though the .frm file for the table exists.
    See http://dev.mysql.com/doc/refman/5.6/en/innodb-
    troubleshooting.html for how you can resolve the problem.


上網查詢了一下關閉'foreign_key_checks'的方法:

禁用外碼約束
    SET FOREIGN_KEY_CHECKS=0;
啓動外碼約束
    SET FOREIGN_KEY_CHECKS=1;
查看當前FOREIGN_KEY_CHECKS的值可用如下命令
    SELECT  @@FOREIGN_KEY_CHECKS;

於是打開數據庫,執行語句

SET FOREIGN_KEY_CHECKS=0;

再次打開表person_event,可以正常查詢了!然後打開表的FOREIGN KEYS附加選項,將裏面的內容刪除“FK_7bh2i9xjn8v93x5ku5w61npwf”。


總結:

看錯誤日誌文件,裏面的提示最能提示我問題出在哪裏了,確定問題再查!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章