示例备份和恢复策略

来自泡泡学习笔记
跳到导航 跳到搜索

本节介绍了一种执行备份的程序,使您能够在多种类型的崩溃后恢复数据:

  • 操作系统崩溃
  • 电源故障
  • 文件系统崩溃
  • 硬件问题(硬盘、主板等)


示例命令未包含mysqldump和mysql客户端程序的--user和--password等选项。您应根据需要包含这些选项,以使客户端程序能够连接到MySQL服务器。


假设数据存储在具有事务和自动崩溃恢复支持的InnoDB存储引擎中。还假设MySQL服务器在崩溃时正处于负载状态。如果不是这样,就不需要进行任何恢复。


对于操作系统崩溃或电源故障的情况,我们可以假设在重新启动后,MySQL的磁盘数据可用。由于崩溃,InnoDB数据文件可能不包含一致的数据,但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数据,这意味着必须已经进行过备份。为确保如此,设计和实施一项备份策略。