MySQL 的逻辑恢复
发布时间:2023-09-06 13:07:44 所属栏目:MySql教程 来源:
导读:逻辑恢复通常支持两种恢复方式:完全恢复、不完全恢复,本小节结合 MysqLdump 和 MysqLbinlog 工具,介绍这两种恢复方式的操作步骤。
1. 完全恢复
MysqL 中,逻辑备份的完全恢复相对比较简单,一般包含两个步骤:
1. 完全恢复
MysqL 中,逻辑备份的完全恢复相对比较简单,一般包含两个步骤:
逻辑恢复通常支持两种恢复方式:完全恢复、不完全恢复,本小节结合 MysqLdump 和 MysqLbinlog 工具,介绍这两种恢复方式的操作步骤。 1. 完全恢复 MysqL 中,逻辑备份的完全恢复相对比较简单,一般包含两个步骤: 恢复最新的全备文件 shell> MysqL -uroot -p < backup.sql 恢复日志文件 shell> MysqLbinlog binlog-file | MysqL -uroot -p 实际案例:完整的MysqLdump备份与恢复 1. 中午12点,备份数据库 [MysqL@localhost ~]$ MysqLdump --single-transaction -F -uroot -p --databases tempdb > /tmp/db_tempdb.sql Enter password: [MysqL@localhost ~]$ ls -lrt db_tempdb.sql -rw-r--r-- 1 MysqL MysqL 19602842 Jul 23 12:01 db_tempdb.sql 参数 --single-transaction 表示给 InnoDB 表生成快照,保持数据一致性 参数 -F 表示生成一个新的日志文件 此时表 customer 的数据如下: MysqL> select * from customer; +----+-----------+------------+------------+--------+---------+ | id | last_name | first_name | birth_date | gender | balance | +----+-----------+------------+------------+--------+---------+ | | | | -- | | | | | | | -- | | | +----+-----------+------------+------------+--------+---------+ rows in set ( sec) 2. 13点,表 customer 插入新的数据: MysqL> insert into customer(id,last_name,first_name,birth_date,gender,balance) values(,,,'2020-08-10',,); Query OK, row affected ( sec) MysqL> insert into customer(id,last_name,first_name,birth_date,gender,balance) values(,,,'2020-09-10',,); Query OK, row affected ( sec) 3. 14点,数据库故障,需要恢复数据: [MysqL@localhost ~]$ MysqL -uroot -p tempdb < /tmp/db_tempdb.sql Enter password: 恢复后表 customer 的数据如下: MysqL> select * from customer; +----+-----------+------------+------------+--------+---------+ | id | last_name | first_name | birth_date | gender | balance | +----+-----------+------------+------------+--------+---------+ | | | | -- | | | | | | | -- | | | +----+-----------+------------+------------+--------+---------+ rows in set ( sec) 4. 从 binlog 日志恢复 12 点备份以来的数据(MysqL-bin.000021 为 12 点备份后产生的新的 binlog 日志): [MysqL@localhost ~]$ MysqLbinlog MysqL-bin.000021 | MysqL -uroot -p tempdb Enter password: 恢复日志后,表 customer 的数据如下: MysqL> select * from customer; +----+-----------+------------+------------+--------+---------+ | id | last_name | first_name | birth_date | gender | balance | +----+-----------+------------+------------+--------+---------+ | | | | -- | | | | | | | -- | | | | | | | -- | | | | | | | -- | | | +----+-----------+------------+------------+--------+---------+ rows in set ( sec) 表 customer 的数据全部恢复。 2. 不完全恢复 MysqL 中,不完全恢复分为基于时间点的恢复和基于位置的恢复。一般来说,不完全恢复需要跳过一些语句,比如说,由于误操作删除了一张表,这时用完全恢复是没有用的,因为 binlog 日志还存在误操作的语句,我们需要跳过误操作语句,在恢复后面的语句,来完成恢复。 2.1 基于时间点恢复 以下是基于时间点恢复的操作步骤: 1. 13点,运维人员误删除表 customer,可以用备份和 binlog 日志恢复到故障前(中午 12 点,备份数据库) [MysqL@localhost ~]$ MysqL -uroot -p tempdb < /tmp/db_tempdb.sql Enter password: [MysqL@localhost ~]$ MysqLbinlog --stop-datetime="2020-07-23 11:59:59" MysqL-bin.000021 | MysqL -uroot -p tempdb Enter password: 2. 跳过故障时间点,继续使用后面的binlog日志完成恢复。 [MysqL@localhost ~]$ MysqLbinlog --start-datetime="2020-07-23 12:01:00" MysqL-bin.000021 | MysqL -uroot -p tempdb Enter password: 基于时间的恢复,稍显粗糙,因为同一时间点可能会有很多条 sql 在执行,那就会跳过一些正常执行的sql。一般我们会考虑使用更为精确的基于位置的恢复。 2.2 基于位置恢复 以下是基于位置恢复的操作步骤: 1. 执行如下 MysqLbinlog 命令: [MysqL@localhost ~]$ MysqLbinlog --start-datetime="2020-07-23 11:55:00" --stop-datetime="2020-07-23 12:05:00" MysqL-bin.000021 > /tmp/temp_restore.sql 从 temp_restore.sql 找到误操作语句前后的位置号为 383 和 437: 2. 恢复备份文件,使用 binlog 日志跳过故障语句的位置号,完成恢复。 [MysqL@localhost ~]$ MysqL -uroot -p tempdb < /tmp/db_tempdb.sql Enter password: [MysqL@localhost ~]$ MysqLbinlog --stop-position="383" MysqL-bin.000021 | MysqL -uroot -p tempdb Enter password: [MysqL@localhost ~]$ MysqLbinlog --start-position="437" MysqL-bin.000021 | MysqL -uroot -p tempdb Enter password: (编辑:汽车网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |