假如线上 MySQL 数据库宕机了,需要通过 frm 文件和 ibd(数据文件)恢复,怎么实现呢?如何通过表结构定义文件查看表结构呢?

我们知道表结构定义文件是二进制文件,我们是没有办法查看的,就算是通过一些 Linux 命令也看不全,显示效果也不行。这个时候我们就需要使用 MySQL 官方提供的 Utilities 工具了。

MySQL Utilities 工具的下载地址:https://downloads.mysql.com/archives/utilities/,官网提示建议使用 MySQL Shell 工具,不过我没有在 Shell 工具中找到 mysqlfrm 命令,所以还是下载 utilities 工具,并安装。

Windows 环境

Windows 环境比较简单,安装好后,进入安装目录,打开 PowerShell 工具,执行如下命令就可以看到 frm 文件的表结构内容。

  1. PS D:\tools\MySQL Utilities 1.6> mysqlfrm --diagnostic D:\tools\mysql-5.7.28-winx64\data\engine\user_innodb.frm
  2. # WARNING: Cannot generate character set or collation names without the --server option.
  3. # CAUTION: The diagnostic mode is a best-effort parse of the .frm file. As such, it may not identify all of the components of the table correctly. This is especially true for damaged files. It will also not read the default values for the columns and the resulting statement may not be syntactically correct.
  4. # Reading .frm file for D:\tools\mysql-5.7.28-winx64\data\engine\user_innodb.frm:
  5. # The .frm file is a TABLE.
  6. # CREATE TABLE Statement:
  7. CREATE TABLE `engine`.`user_innodb` (
  8. `id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
  9. `name` varchar(400) NOT NULL,
  10. PRIMARY KEY `PRIMARY` (`id`)
  11. ) ENGINE=InnoDB;
  12. #...done.

Linux 环境

Linux 环境安装 Utilities 工具步骤如下:

  1. # 先解压Utilities包
  2. tar zxf mysql-utilities-1.6.2.tar.gz
  3. # 进入解压目录
  4. cd mysql-utilities-1.6.2/
  5. # 执行安装命令
  6. python setup.py install

Utilities 会安装在 /usr/local/bin 目录下。

执行如下命令就可以看到 frm 文件的表结构内容。

  1. mysqlfrm --diagnostic /data/engine/user_innodb.frm

作者:殷建卫 链接:https://www.yuque.com/yinjianwei/vyrvkf/vpoft2 来源:殷建卫 - 架构笔记 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。