MyISAM表的维护和恢复

时间:2022-06-01 13:23:47

本节将讨论如何使用myisamchk检查和修复MyISAM表。

同时,你可以用myisamchk来检查,修复和优化数据库表。下面将讲述如何执行这些操作并建立维护计划。

虽然使用myisamchk很安全,但在操作前做个备份总是个好Idea。

当然也可以使用Sql达到与myisamchk相同的效果:

8.6.2 How to Check MyISAM Tables for Errors

  myisamchk tbl_name  ,能找到99.99%的错误。不检测Data文件中的Corruption[一般不会出问题].

MyISAM表的维护和恢复

myisamchk -m tbl_name 能检测出99.999%的错误,checks all index entries for errors and then reads through all rows.

myisamchk -e -i tbl_name,全部检查,并输出统计信息。

8.6.3 How to Repair MyISAM Tables

使用perror展示错误码对应的描述:如 perror 123  134

MyISAM表的维护和恢复

Stage 1: Checking your tables

myisamchk -e -i tbl_name,

Stage 2: Easy safe repair

myisamchk -r -q tbl_name :快速修复【quick recovery mode】,对Myisam表Index进行修复而不触及.MYD【数据】。

也可用下面:

  1. Make a backup of the data file before continuing.

  2. Use myisamchk -r tbl_name (-r means “recovery mode”). This removes incorrect rows and deleted rows from the data file and reconstructs the index file.

  3. If the preceding step fails, use myisamchk --safe-recover tbl_name. Safe recovery mode uses an old recovery method that handles a few cases that regular recovery mode does not (but is slower).

Stage 3: Difficult repair  【 index file is destroyed or contains incorrect ,即.MYI文件出了问题,或or if the index file is missing】

  1.移动数据文件到其他地方

  2. Use the table description file to create new (empty) data and index files     

    TRUNCATE TABLE tbl_name;
  3.再将数据文件拷回
4.myisamchk -r should work.
5.重启Msyql

Stage 4: Very difficult repair【the .frm description file has also crashed】

  1.从之前备份的.frm文件恢复,然后按照Stage3的方式处理

  2.如果没有备份,但你知道表怎么被创建,则可以在其他服务器上创建该表,然后将.frm和.MYI文件拷到该服务器上,之后执行Stage2的操作,重建索引文件。

Setting Up a MyISAM Table Maintenance Schedule

You should also check your tables regularly during normal system operation. For example, you can run a cron job to check important tables once a week, using a line like this in a crontab file:

35 0 * * 0 /path/to/myisamchk --fast --silent /path/to/datadir/*/*.MYI