【MySQL Errors】Table 'xxx' is marked as crashed and should be repaired 的解决方案

时间:2023-03-09 18:54:03
【MySQL Errors】Table 'xxx' is marked as crashed and should be repaired 的解决方案

现象描述

访问 Zabbix Web,出现如下错误提示:

•    Error in query [SELECT * FROM history_uint h WHERE h.itemid='25067' ORDER BY h.clock DESC LIMIT 1 OFFSET 0] [Table './zabbix/history_uint' is marked as crashed and should be repaired]
• Error in query [SELECT * FROM history_uint h WHERE h.itemid='26280' ORDER BY h.clock DESC LIMIT 1 OFFSET 0] [Table './zabbix/history_uint' is marked as crashed and should be repaired]
• Error in query [SELECT * FROM history_uint h WHERE h.itemid='26286' ORDER BY h.clock DESC LIMIT 1 OFFSET 0] [Table './zabbix/history_uint' is marked as crashed and should be repaired]

解决办法

1、首先进入mysql命令台:
      mysql -u root -p
      回车,然后输入密码

2、查询所有的库
      mysql> show databases;

3、进入数据库“zabbix”是库名
      mysql> use zabbix;

4、检查表
      check table history_uint;
      (history_uint :出现错误的表)用来检查出现问题的表的状态,出现错误就正常。

mysql> check table history_uint;
+---------------------+-------+----------+-------------------------------------------------------+
| Table | Op | Msg_type | Msg_text |
+---------------------+-------+----------+-------------------------------------------------------+
| zabbix.history_uint | check | warning | Table is marked as crashed |
| zabbix.history_uint | check | warning | 1 client is using or hasn't closed the table properly |
| zabbix.history_uint | check | error | record delete-link-chain corrupted |
| zabbix.history_uint | check | error | Corrupt |
+---------------------+-------+----------+-------------------------------------------------------+
4 rows in set (2 min 48.75 sec)

5、修复表
      repair table history_uint;

mysql> repair table history_uint;
+---------------------+--------+----------+--------------------------------------------------+
| Table | Op | Msg_type | Msg_text |
+---------------------+--------+----------+--------------------------------------------------+
| zabbix.history_uint | repair | warning | Number of rows changed from 28813609 to 28843608 |
| zabbix.history_uint | repair | status | OK |
+---------------------+--------+----------+--------------------------------------------------+
2 rows in set (9 min 12.42 sec)

6、再次检查表。
      check table history_uint;

mysql> check table history_uint;
+---------------------+-------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+---------------------+-------+----------+----------+
| zabbix.history_uint | check | status | OK |
+---------------------+-------+----------+----------+
1 row in set (13.57 sec)

7、ok 搞定。

预防措施

1、一定要备份一次数据库,起码保留了表结构,有些可有可无的数据,可以直接覆盖。
       2、重要的数据要经常注意备份,一般一个月左右备份一次。
       3、出现此类错误,一般能够解决,经上面的修复方法是比较可行的。但偶尔会有数据丢失的情况,强烈建议先备份。

Windows下使用MyISAM存储引擎碰到此问题的解决方法

首先需要定位到你的mysql的bin目录,里面包含myisamchk.exe文件的目录
./myisamchk -c -r 数据库表MYI文件的路径 (例如:D:/mysql/data/hangban/user.MYI)
如果还不行,就-f 强制修复

D:\mysql\bin> cd .. 返回上级目录
D:\mysql> cd data 进入数据库所在目录(mysql的数据库文件都是放在data这个目录里面的)
D:\mysql\data> cd hangban 进入数据库,windows服务器中数据库就是一个文件夹(这里以hangban为例子)

D:\mysql\data\hangban> myisamchk -r user

- recovering (with sort) MyISAM-table 'user'
Data records: 7216
- Fixing index 1
- Fixing index 2
- Fixing index 3

D:\mysql\data\hangban>

user为数据库的表名,提示哪个表错误,就修复哪个表。