mysql innodb数据恢复

这几天在从一台已经启动不了的机器上恢复一个web应用。其中数据库使用的是mysql,部分表采用myisam,部分表采用了innodb。但是innodb没有采用一个表一个数据文件的形式,也就是说,所有的数据都在一个ibdata1文件中。

由于那台机器已经无法启动,通过无盘系统进入之后,将mysql的data目录整体复制出来,在另一台服务器上试图恢复数据。

首先,先将数据库文件复制到新机器的mysql data对应的目录中,然后启动mysql,能看见之前的表,但是无法查询出数据。有一下异常日志:

[ERROR] Cannot find or open table process2/process_risk from
the internal data dictionary of InnoDB though the .frm file for the
table exists. Maybe you have deleted and recreated InnoDB data
files but have forgotten to delete the corresponding .frm files
of InnoDB tables, or you have moved .frm files to another database?
or, the table contains indexes that this version of the engine
doesn't support.

具体原因就是之前说过的,这些表都采用了一个数据文件,所有数据都在ibdata1文件中。

然后替换原有的ibdata1文件,启动mysql失败,异常日志为:

InnoDB: Error: auto-extending data file /data/mysqldata1/innodb_ts/ibdata1 is of a different size
InnoDB: 3200 pages (rounded down to MB) than specified in the .cnf file:
InnoDB: initial 131072 pages, max 0 (relevant if non-zero) pages!
InnoDB: Could not open or create data files.
InnoDB: If you tried to add new data files, and it failed here,
InnoDB: you should now edit innodb_data_file_path in my.cnf back
InnoDB: to what it was, and remove the new ibdata files InnoDB created
InnoDB: in this failed attempt. InnoDB only wrote those files full of
InnoDB: zeros, but did not yet use them in any way. But be careful: do not
InnoDB: remove old data files which contain your precious data!

因为没有修改数据文件的配置,这里发现数据文件大小校验失败了。
从这里看见,需要恢复的数据文件有3200页,通过查询mysql文档innodb也结构,可以知道现在mysql innodb每个页的大小是固定的,均为16k,这里的3200页,折算下:3200 * 16 / 1024 = 50M
因此,修改mysql的my.cnf为:

innodb_data_file_path = ibdata1:50M;ibdata2:50M:autoextend

既使用ibdata1文件,标注大小为50M,同时创建ibdata2文件,初始大小为50M,并且自动扩展。

这时候就可以重启mysql,从日志中可以看见,mysql成功加载了ibdata1,并且自动创建了初始大小为50M的ibdata2文件,然后开始恢复数据。数据恢复完成之后,就可以立即使用mysqldump,将对应的整个database导出成sql文件,方面后面恢复到其他mysql中。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据