MySQL 8.4 Release Notes
9.3 示例备份和恢复策略
本节讨论了一个备份过程,用于在多种类型的崩溃后恢复数据:
-
操作系统崩溃
-
电源故障
-
文件系统崩溃
-
硬件问题(硬盘、主板等)
示例命令不包括--user
和--password
等选项,用于mysqldump和mysql客户端程序。您应该根据需要添加这些选项,以便客户端程序连接到MySQL服务器。
假设数据存储在 InnoDB
存储引擎中,该引擎支持事务和自动崩溃恢复。假设 MySQL 服务器在崩溃时处于负载状态。如果不是,崩溃恢复就不需要。
对于操作系统崩溃或电源故障,我们可以假设 MySQL 的磁盘数据在重启后仍然可用。 InnoDB
数据文件可能由于崩溃而不包含一致的数据,但是 InnoDB
读取日志,找到未被写入到数据文件中的已提交和未提交事务,然后自动回滚未提交的事务,并将已提交的事务写入到数据文件中。关于恢复过程的信息通过 MySQL 错误日志传递给用户。以下是一个错误日志摘录:
InnoDB: Database was not shut down normally.
InnoDB: Starting recovery from log files...
InnoDB: Starting log scan based on checkpoint at
InnoDB: log sequence number 0 13674004
InnoDB: Doing recovery: scanned up to log sequence number 0 13739520
InnoDB: Doing recovery: scanned up to log sequence number 0 13805056
InnoDB: Doing recovery: scanned up to log sequence number 0 13870592
InnoDB: Doing recovery: scanned up to log sequence number 0 13936128
...
InnoDB: Doing recovery: scanned up to log sequence number 0 20555264
InnoDB: Doing recovery: scanned up to log sequence number 0 20620800
InnoDB: Doing recovery: scanned up to log sequence number 0 20664692
InnoDB: 1 uncommitted transaction(s) which must be rolled back
InnoDB: Starting rollback of uncommitted transactions
InnoDB: Rolling back trx no 16745
InnoDB: Rolling back of trx no 16745 completed
InnoDB: Rollback of uncommitted transactions completed
InnoDB: Starting an apply batch of log records to the database...
InnoDB: Apply batch completed
InnoDB: Started
mysqld: ready for connections
对于文件系统崩溃或硬件问题,我们可以假设 MySQL 磁盘数据在重启后不可用。这意味着 MySQL 无法成功启动,因为某些磁盘数据块不再可读。在这种情况下,需要重新格式化磁盘、安装新磁盘或解决基础问题。然后,需要从备份中恢复 MySQL 数据,这意味着必须已经做了备份。为了确保这样做,设计和实施备份策略。