环境
系统:centos7 数据库:mysql5.7.37 数据库目录: /usr/bin 数据目录: /var/lib/mysql 配置文件位置: /etc/my.cnf
报错日志
2024-02-18T08:45:58.039432Z 0 [ERROR] InnoDB: Page [page id: space=0, page number=238] log sequence number 8738273916 is in the future! Current system log sequence number 8738272796.
2024-02-18T08:45:58.039450Z 0 [ERROR] InnoDB: Your database may be corrupt or you may have copied the InnoDB tablespace but not the InnoDB log files. Please refer to http://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html for information about forcing recovery.
2024-02-18T08:45:58.039504Z 0 [ERROR] InnoDB: Page [page id: space=0, page number=239] log sequence number 8738275019 is in the future! Current system log sequence number 8738272796.
2024-02-18T08:45:58.039510Z 0 [ERROR] InnoDB: Your database may be corrupt or you may have copied the InnoDB tablespace but not the InnoDB log files. Please refer to http://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html for information about forcing recovery.
2024-02-18T08:45:58.040921Z 0 [Note] InnoDB: Starting an apply batch of log records to the database...
打开提示的引用地址 MySQL :: MySQL 5.7 Reference Manual :: 14.22.2 Forcing InnoDB Recoveryhttp://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html 查看网页 可知主要 通过修改配置文件 innodb_force_recovery来 打开恢复模式
打开恢复模式
编辑数据库配置文件
在配置文件[mysqld]下加入参数 innodb_force_recovery = 1 ,其中后面的值设置为1、如果1不能恢复,再逐步增加为2/3/4等。直到能启动mysql为止!!!
注意:最高值为6,但当参数值大于3的时候。会对数据文件造成永久性的破坏。
[mysqld] innodb_force_recovery = 1
启动数据库
systemctl start mysqld.service
此时进入的恢复模式,数据库是只读的
数据备份
下面需要把数据库备份出来,然后清除之前损坏的数据,利用备份数据恢复
执行:
/usr/bin/mysqldump -uroot -p123456 --all-databases > /ambuf/mysql/backup240818.sql
执行完了 去掉 innodb_force_recovery = 1 参数
清除损坏的数据
清除之前需要把服务停止:
systemctl stop mysqld.service
备份原data目录以防万一:
cp -r /var/lib/mysql/ /ambuf/mysql/data_bak/ rm -rf /var/lib/mysql/*
数据库初始化
上个步骤已经清空了data目录,所以此时数据库是启动不了的,需要进行初始化
进入 安装目录下进行初始化
/usr/bin/mysql_install_db --user=mysql&
下面就可以利用mysqldump出来的数据进行恢复了,但因为刚刚进行了初始化,之前的密码已经没用了,需要在配置文件加入 skip-grant-tables
vi /etc/mysql/my.cnf
[mysqld] skip-grant-tables
启动mysql数据库
systemctl start mysqld.service
通过 查找日志获取临时密码
grep "temporary password" /var/log/mysqld.log
执行结果如下
2023-11-08T03:40:10.570444Z 1 [Note] A temporary password is generated for root@localhost: bf*17blep(_R
2024-02-18T09:23:22.815511Z 1 [Note] A temporary password is generated for root@localhost: i45emkw99*Ug
再次登录数据库
mysql -uroot -pi45emkw99*Ug
重新设置 登录用户密码权限。执行:
set global validate_password_policy = 0; SET PASSWORD = PASSWORD("123456"); ALTER USER USER() IDENTIFIED BY "123456"; grant all privileges on *.* to root@'%' identified by "123456"; flush privileges;
打开配置文件my.cnf, 去掉 skip-grant-tables 参数
vi /etc/mysql/my.cnf
登录数据库恢复数据
再次登录数据库
mysql -uroot -p123456
导入之前 备份文件
source /ambuf/mysql/backup-240218.sql
重启数据库查看 数据恢复情况。