有一天因为操作不当导致服务器崩了,再开机发现数据库启动不了,那么我们来看报错日志

[Note] InnoDB: Database was not shutdown normally!
[Note] InnoDB: Starting crash recovery.
[Note] InnoDB: Reading tablespace information from the .ibd files...
[ERROR] InnoDB: Attempted to open a previously opened tablespace. Previous tablespace Xkl/v2_stat_server uses space ID: 40 at filepath: ./Xkl/v2_stat_server.ibd. Cannot open tablespace xkl@002eme/v2_server which uses space ID: 40 at filepath: ./xkl@002eme/v2_server.ibd
InnoDB: Operating system error number 2 in a file operation.
InnoDB: The error means the system cannot find the path specified.
InnoDB: If you are installing InnoDB, remember that you must create
InnoDB: directories yourself, InnoDB does not create them.
InnoDB: Error: could not open single-table tablespace file ./xkl@002eme/v2_server.ibd
InnoDB: We do not continue the crash recovery, because the table may become
InnoDB: corrupt if we cannot apply the log records in the InnoDB log to it.
InnoDB: To fix the problem and start mysqld:
InnoDB: 1) If there is a permission problem in the file and mysqld cannot
InnoDB: open the file, you should modify the permissions.
InnoDB: 2) If the table is not needed, or you can restore it from a backup,
InnoDB: then you can remove the .ibd file, and InnoDB will do a normal
InnoDB: crash recovery and ignore that table.
InnoDB: 3) If the file system or the disk is broken, and you cannot remove
InnoDB: the .ibd file, you can set innodb_force_recovery > 0 in my.cnf

分析

从日志内容来看,MySQL 在机器关机的时候有数据没有落地,表空间损坏,导致重启之后无法正常恢复

需要做特殊操作,让 MySQL 跳过恢复,启动 MySQL,然后把数据导出来,再重建数据库导入。

MySQL 有个一个特性:Forcing InnoDB Recovery

解决

我们在MySQL配置文件中做个参数调整

使 innodb_force_recovery 大于 0 (默认为0,可以设置为 1-6)

不建议设置4或更高的值,因为数据文件可能永久损坏
6也不建议使用,因为数据库页面处于过时状态,这陆续可能在B树和其他数据库结构中引入更多的损坏。

在 /etc/mysql/my.cnf 中修改,这里我将值设置为1

[mysqld]
innodb_force_recovery = 1

修改完成后然后重启 MySQL,立即对数据库用 mysqldump 把数据导出

完成后,去掉 innodb_force_recovery 或者设置为 0,然后重新创建数据库,把数据导入(一定要设置回0,不然会出现报错)

© 版权声明
评论 抢沙发

请登录后发表评论