迁移mysql数据文件,启动时出现: Aborting because of a corrupt database page in the system tablespace. Or, there

/ mysql / 没有评论 / 2096浏览

迁移mysql数据文件,启动时出现: Aborting because of a corrupt database page in the system tablespace. Or, there

利用 Forcing InnoDB Recovery 特性解决 MySQL 重启失败的问题。服务器磁盘空间突然爆满,主要是数据库默认放到 /var/lib/mysql 目录下,导致系统盘占满,mysql运行出现异常。

2018-07-15T04:40:13.995234Z 0 [Note] InnoDB: Uncompressed page, stored checksum in field1 643411285, calculated checksums for field1: crc32 2478637551/3971325484, innodb 1335711502, none 3735928559, stored checksum in field2 0, calculated checksums for field2: crc32 2478637551/3971325484, innodb 713899775, none 3735928559, page LSN 45 984470675, low 4 bytes of LSN at page end 0, page number (if stored to page already) 2, space id (if created with >= MySQL-4.1.1 and stored already) 1386836InnoDB: Page may be an 'inode' page2018-07-15T04:40:13.995255Z 0 [Note] InnoDB: It is also possible that your operating system has corrupted its own file cache and rebooting your computer removes the error. If the corrupt page is an index page. You can also try to fix the corruption by dumping, dropping, and reimporting the corrupt table. You can use CHECK TABLE to scan your table for corruption. Please refer to http://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html for information about forcing recovery.2018-07-15T04:40:13.995265Z 0 [ERROR] [FATAL] InnoDB: Aborting because of a corrupt database page in the system tablespace. Or, there was a failure in tagging the tablespace as corrupt.2018-07-15 12:40:13 0x7fe733636700 InnoDB: Assertion failure in thread 140630976325376 in file ut0ut.cc line 916

从日志内容来看,MySQL 在服务器磁盘空间占满mysql运行异常,表空间损坏,导致重启之后无法正常恢复,线程在数据页中读取不到需要的 page 和数据。需要做特殊操作,让 MySQL 跳过恢复,启动 MySQL,然后把数据导出来,再重建数据库导入。

MySQL 有个一个特性:Forcing InnoDB Recovery,启用这个特性需要设置 innodb_force_recovery 大于 0。

innodb_force_recovery 可以设置为 1-6,大的值包含前面所有小于它的值的影响。

解决:

vi /etc/my.cnf 在 [mysqld]中新增

[mysqld]innodb_force_recovery = 6

然后再重新启动mysql,恢复正常。

恢复启动后,你会发现所有表都是只读状态,无法增删改,因为设置为6时,是为恢复数据时使用,在启动正常后需设置为0,然后再重启mysql服务,就可恢复正常增删改。修复完成后,会发现有些表增删改查时都是抛出以下异常:

[Err] 2013 - Lost connection to MySQL server during query

经过日志分析,发现是之前的表数据文件损坏,mysql中check table 发现表有损坏,但表是innodb类型不能修复。Innodb 自检过程中checksum与退出时不一致便会去recover;

2018-07-23T06:11:38.767992Z 135 [ERROR] InnoDB: Database page corruption on disk or a failed file read of page [page id: space=1386836, page number=2]. You may have to recover from a backup.

解决方法:

将数据库配置中my.cnf 添加以下配置

innodb_force_recovery=1

然后重启mysql,然后再查询该表是否可以正常查询,然后将该表导出成mysql文件。然后删除该表。再将my.cng中的innodb_force_recovery=1 删除掉,再重启mysql服务。然后再将上一步骤导出的mysql文件脚本导入到数据库。