MySQL修复表的简单分析(r11笔记第19天)
今天有个同事问我一个数据库的问题,如果开始他就把环境细节全都告诉我,可能我就知难而退了。等我大体明白了问题之后,发现好像背景比我想的要复杂多了。这是一个远程云主机环境,windows系统,运行着MySQL,在查询表时出现了问题,而且开发同事经过了repair也没有修复,说会卡住没有响应。
当然费了一点功夫,好容易连接到了这台云主机,发现问题似乎比我想的还要复杂一些。当然这是一个内部某一个团队使用的一个环境,可能是确实需要用到环境,大家才不得不想办法修复。
环境是MySQL 5.5版本,查看后台日志发现从8月份就开始有错误了,错误信息如下:
161018 11:15:35 [ERROR] D:websoftmysqlbinmysqld: Table '.utestdbtest_forum_post' is marked as crashed and should be repaired
161018 11:15:36 [ERROR] D:websoftmysqlbinmysqld: Table '.utestdbtest_forum_post' is marked as crashed and should be repaired
而且看日志损坏的还不止一张表,我的注意力暂时先放在了出错的表上。
如果使用show create table test_forum_post或者desc test_forum_post都会抛出错误。
mysql> show create table test_forum_post;
ERROR 145 (HY000): Table '.utestdbtest_forum_post' is marked as crashed and should be repaired
更让我有些胆战心惊的是,我可以从后台的日志看到开发同事也尝试了多次重启MySQL服务。但是问题始终存在。
show create table 得不到信息,而show table status得到的信息也很有限,因为此时的存储引擎显示为NULL
他们用的是MyISAM,查看了其它所有的表的存储引擎,发现清一色都是MyISAM.所以我就可以基本断定这个出问题的表也是MyISAM
对于MyISAM表修复,可以用myisamchk来做或者使用repair的方式都可以,当然发现又是碰到不少问题。
D:websoftmysqlbin>myisamchk.exe -of ..datautestdbtest_forum_post.MYI
这个命令运行下去,竟然弹出了一个窗口显示程序崩溃,反复尝试都是如此。
使用repair命令来看,发现迟迟没有返回,果断停止。
肯定是哪里漏掉了,我重新翻过头来梳理问题。
查看日志发现之前有下面的一些输出,看起来是磁盘空间的问题。
161219 18:07:09 [Warning] Disk is full writing '.distoonpre_common_block.TMD' (Errcode: 28). Waiting for someone to free space... (Expect up to 60 secs delay for server to continue after freeing disk space)
161219 18:07:09 [Warning] Retry in 60 secs. Message reprinted in 600 secs
161219 18:07:18 [ERROR] D:websoftmysqlbinmysqld: Table '.utestdbtest_forum_post' is marked as crashed and should be repaired
经过确认发现确实是磁盘空间导致,他们马上清理预留出一些空间,然后让我继续帮忙修复,再次尝试就没有问题了。
先使用-of选项
D:websoftmysqlbin>myisamchk.exe -of ..datautestdbtest_forum_post.MYI
- recovering (with keycache) MyISAM-table '..datautestdbtest_forum_post.MYI'
Data records: 0
Data records: 55311
接着使用-r选项修复
D:websoftmysqlbin>myisamchk.exe -r ..datautestdbtest_forum_post.MYI
- recovering (with sort) MyISAM-table '..datautestdbtest_forum_post.MYI'
Data records: 55311
- Fixing index 1
- Fixing index 2
- Fixing index 3
- Fixing index 4
- Fixing index 5
- Fixing index 6
- Fixing index 7
- Fixing index 8
最后汇总检查
D:websoftmysqlbin>myisamchk.exe ..datautestdbtest_forum_post.MYI
Checking MyISAM file: ..datautestdbtest_forum_post.MYI
Data records: 55311 Deleted blocks: 0
- check file-size
- check record delete-chain
- check key delete-chain
- check index reference
- check data record references index: 1
- check data record references index: 2
- check data record references index: 3
- check data record references index: 4
- check data record references index: 5
- check data record references index: 6
- check data record references index: 7
- check data record references index: 8
- check record links再次查看问题就不存在了。
当然如果尝试使用repair也是可行的,比如修复表pre_common_member,输出如下:
mysql> repair table pre_common_member;
+----------------------------+--------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+----------------------------+--------+----------+----------+
| utestdb.pre_common_member | repair | status | OK |
+----------------------------+--------+----------+----------+
1 row in set (1.64 sec)
为了把问题补充全面一些,我把问题略微改动下,即 使用myisamchk工具和check/repair命令有什么区别呢.
首先myisamchk和repair只能修复MyISAM表,相比来说,myisamchk的输出信息要更详细一些,优化,分析表的信息都会输出,repair则比较直接,repair无法修复InnoDB的表,否则会报出如下的错误。
The storage engine for the table doesn't support repair
check则同时支持MyISAM表和InnoDB表
其次myisamchk操作myisam表时必须保证表不能被使用,check/repair则可以在线操作。
问题解决了,不过想想问题的最开始扑朔迷离的场景,其实很多大问题的原因都是如此简单。
- Spring Boot 中使用 Java API 调用 Elasticsearch
- Spring Boot 中使用 公共配置
- WebView 和 JS 交互,如何将 Java 对象和 List 传值给 JS ?
- Spring Boot 中使用 LogBack 配置
- Spring Boot 中使用 RabbitMQ
- 手把手教你dubbo怎么用?
- 一步一步实现Android的MVP框架
- Base封装之我的最简MVP架构
- 请求跨域的解决方案
- 运用Kubernetes进行分布式负载测试
- Spring Cloud(五)断路器监控(Hystrix Dashboard)
- 微信技术团队的又一力作,WCDB 简单易用的数据库框架
- Redis特性和应用场景
- Spring Cloud(四)服务提供者 Eureka + 服务消费者 Feign
- MySQL 教程
- MySQL 安装
- MySQL 管理与配置
- MySQL PHP 语法
- MySQL 连接
- MySQL 创建数据库
- MySQL 删除数据库
- MySQL 选择数据库
- MySQL 数据类型
- MySQL 创建数据表
- MySQL 删除数据表
- MySQL 插入数据
- MySQL 查询数据
- MySQL where 子句
- MySQL UPDATE 查询
- MySQL DELETE 语句
- MySQL LIKE 子句
- mysql order by
- Mysql Join的使用
- MySQL NULL 值处理
- MySQL 正则表达式
- MySQL 事务
- MySQL ALTER命令
- MySQL 索引
- MySQL 临时表
- MySQL 复制表
- 查看MySQL 元数据
- MySQL 序列 AUTO_INCREMENT
- MySQL 处理重复数据
- MySQL 及 SQL 注入
- MySQL 导出数据
- MySQL 导入数据
- MYSQL 函数大全
- MySQL Group By 实例讲解
- MySQL Max()函数实例讲解
- mysql count函数实例
- MYSQL UNION和UNION ALL实例
- MySQL IN 用法
- MySQL between and 实例讲解