程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> MYISAM索引文件損壞的修復方法

MYISAM索引文件損壞的修復方法

編輯:關於MYSQL數據庫
   使用MYISAM存儲引擎,當創建一個表時,會出現三個文件:數據文件MYD,索引文件MYI,格式文件frm。當索引文件損壞時或者丟失時,可以用如下的方法進行修復。下面是實驗過程。

[MySQL@testapp74 notify]$ mv message_ignore.MYI message_ignore.MYI.bak
[MySQL@testapp74 notify]$
[mysql@testapp74 notify]$ MySQLd_safe &
[1] 26267
[mysql@testapp74 notify]$ 080630 14:19:03 mysqld_safe Logging to '/usr/MySQL_data/log/alert.log'.
080630 14:19:03 mysqld_safe Starting mysqld daemon with databases from /usr/MySQL_data/data

[MySQL@testapp74 notify]$
[MySQL@testapp74 notify]$
[MySQL@testapp74 notify]$
[mysql@testapp74 notify]$ vi /usr/MySQL_data/log/alert.log

080630 14:18:14 [Note] /usr/sbin/MySQLd: Normal shutdown

080630 14:18:14 [Note] Event Scheduler: Purging the queue. 0 events
080630 14:18:16  InnoDB: Starting shutdown...
080630 14:18:19  InnoDB: Shutdown completed; log sequence number 162 783173592
080630 14:18:19 [Note] /usr/sbin/MySQLd: Shutdown complete

080630 14:18:19 mysqld_safe mysqld from pid file /usr/MySQL_data/data/testapp74.pid ended
080630 14:19:03 mysqld_safe Starting mysqld daemon with databases from /usr/MySQL_data/data
080630 14:19:03  InnoDB: Started; log sequence number 162 783173592
080630 14:19:03 [Note] Event Scheduler: Loaded 0 events
080630 14:19:03 [Note] /usr/sbin/MySQLd: ready for connections.
Version: '5.1.23-rc-community'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Edition (GPL)

從上面可以看出,當一個表出現問題,MySQL的警告日志並沒有任何的錯誤出現。

[mysql@testapp74 notify]$ MySQL -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.23-rc-community MySQL Community Edition (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

MySQL> use notify
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MySQL>
MySQL>
MySQL>
MySQL>
MySQL> select * from message_ignore limit 1;        --當查詢表時,會報錯
ERROR 1017 (HY000): Can't find file: 'message_ignore' (errno: 2)

MySQL> check table message_ignore;
+-----------------------+-------+----------+----------------------------------------------+
| Table                 | Op    | Msg_type | Msg_text                                     |
+-----------------------+-------+----------+----------------------------------------------+
| notify.message_ignore | check | Error    | Can't find file: 'message_ignore' (errno: 2) |
| notify.message_ignore | check | error    | Corrupt                                      |
+-----------------------+-------+----------+----------------------------------------------+
2 rows in set (0.03 sec)

MySQL>
MySQL> repair table message_ignore;
+-----------------------+--------+----------+----------------------------------------------+
| Table                 | Op     | Msg_type | Msg_text                                     |
+-----------------------+--------+----------+----------------------------------------------+
| notify.message_ignore | repair | Error    | Can't find file: 'message_ignore' (errno: 2) |
| notify.message_ignore | repair | error    | Corrupt                                      |
+-----------------------+--------+----------+----------------------------------------------+
2 rows in set (0.00 sec)

使用格式化文件frm重新創建索引文件
MySQL> repair table message_ignore USE_FRM;
+-----------------------+--------+----------+----------+
| Table                 | Op     | Msg_type | Msg_text |
+-----------------------+--------+----------+----------+
| notify.message_ignore | repair | status   | OK       |
+-----------------------+--------+----------+----------+
1 row in set (0.02 sec)

MySQL>
MySQL> check table message_ignore;
+-----------------------+-------+----------+----------+
| Table                 | Op    | Msg_type | Msg_text |
+-----------------------+-------+----------+----------+
| notify.message_ignore | check | status   | OK       |
+-----------------------+-------+----------+----------+
1 row in set (0.02 sec)

修復後,數據可以正常查詢

MySQL> select * from message_ignore limit 1;
Empty set (0.00 sec)

如果是格式化文件frm損壞,可以從slave拷貝過來即可,或者從備份中恢復。

附錄,在哪些情況下,myisam存儲引擎的表容易損壞:

14.1.4.1. Corrupted MyISAM Tables

Even though the MyISAM table format is very reliable (all changes to a table made by an SQL statement are written before the statement returns), you can still get corrupted tables if any of the following events occur:

The MySQLd process is killed in the middle of a write.

An unexpected computer shutdown occurs (for example, the computer is turned off).

Hardware failures.

You are using an external program (such as myisamchk) to modify a table that is being modifIEd by the server at the same time.

A software bug in the MySQL or MyISAM code.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved