目录

数据库技术——使用binlog日志恢复指定范围数据

命令格式

案例

网络框架

操作步骤

如何查看日志,了解日志的格式

插入和删除数据以生成日志记录

使用日志记录的起始偏移量和结束偏移量恢复数据

使用日志记录的起始时间和结束时间恢复数据


数据库技术——使用binlog日志恢复指定范围数据

命令格式

把查看到的文件内容管道给连接mysql服务的命令执行

#适用于恢复所有数据

mysqlbinlog /目录/文件名 | mysql –uroot –p密码

#适用于恢复指定范围的数据

mysqlbinlog 选项  /目录/文件名 | mysql –uroot –p密码

选项:

--start-datatime=”yyyy-mm-dd hh:mm:ss”  #起始时间

--stop-datatime=”yyyy-mm-dd hh:mm:ss”  #结束时间

--start-position=数字                   #起始偏移量

--stop-position=数字                   #结束偏移量

示例:

mysqlbinlog --start-position=200 --stop-position=918 /mylog/plj.000001 | mysql –uroot –p123456

案例

网络框架

Db51    192.168.88.51

操作步骤

如何查看日志,了解日志的格式
  1. 首先安装数据库软件包并启服,登录数据库

#显示主数据库状态

mysql> show master.status;

File            Position  …

host51.000001      154  …

2、插入记录并查看日志文件

mysql> instert into tarena.usr(name) valudw(“A”);

mysql> show master.status;

File            Position  …

host51.000001      426  …

#查看日志文件内容

mysql> Show binlog events in host51.000001

没有看到insert into命令,默认情况下,执行的命令在binlog中是看不到的

需要改变日志格式

#查看binlog日志默认格式

mysql>show variables like “binlog_format”;

显示 value ROW  就是行模式  还有混合模式和报表模式

报表模式会拆分命令,比如:

Update tarena.user set shell=null where id<=3;

会拆分成3条语句

Update tarena.user set shell=null where id=1;

Update tarena.user set shell=null where id=2;

Update tarena.user set shell=null where id=3;

mysql>exit

3、修改主配置文件的参数,修改binlog日志模式为混合模式,保存退出,重启服务

vim /etc/my.cnf

#在第5行输入

binlog_format=”mixed”

systemctl restart mysqld

4、登录数据库,查看binlog日志格式

mysql –uroot –p123456

mysql>show variables like “binlog_format”;

mysql>show master status;#重启数据库服务会生成新的日志文件

File               Position  …

host51.000002      154

插入和删除数据以生成日志记录

5、插入6条数据

mysql>show binlog events in “host51.000002”;

mysql>insert into tarena.user(name) values(“b”);

mysql>insert into tarena.user(name) values(“c”);

mysql>insert into tarena.user(name) values(“d”);

mysql>insert into tarena.user(name) values(“e”);

mysql>insert into tarena.user(name) values(“f”);

mysql>insert into tarena.user(name) values(“g”);

6、删除这6条数据

#查询到这6条数据的id号

mysql>select id ,name from tarena.user;

#根据id号删除这6条数据

mysql>delete from tarena.user where id >= 24;

#查看binlog日志就可以看到插入和删除的命令了, End_log_pos就是偏移量, COMMIT就是回车偏移量

mysql>show binlog events in “host51.000002”;

Log_name      Pos    Event_type   Server_id  End_log_pos  Info

host51.000002  328    Query             51         441  insert into …

host51.000002  441    Query             51         472  COMMIT /* xid=7 */

host51.000002  646    Query             51         759  insert into …

host51.000002  759    Query             51         790  COMMIT /* xid=8 */

host51.000002  964    Query             51        1077  insert into …

host51.000002 1077    Query             51         1108  COMMIT /* xid=9 */

host51.000002 1282    Query             51        1395  insert into …

host51.000002 1395    Query             51        1426  COMMIT /* xid=10 */

host51.000002 1600    Query             51        1744  insert into …

host51.000002 1744    Query             51        1809  COMMIT /* xid=11 */

host51.000002 1918    Query             51        2031  insert into …

host51.000002 2031    Query             51        2062  COMMIT /* xid=12 */

……

mysql>exit

使用日志记录的起始偏移量和结束偏移量恢复数据
  1. 根据起始偏移量和结束偏移量查看日志,并管道执行插入记录命令

mysqlbinlog –strat-position=328 –stop-position=1108 /var/l插入ib/mysql/host51.000002 | mysql –uroot –p123456

  1. 插入后,查看是否插入成功

Mysql –uroot –p123456

mysql>select id,name from tarena.user

看到插入的三条数据b c d

mysql>exit

使用日志记录的起始时间和结束时间恢复数据

注意: 在mysql数据库里是看不到日志的起始时间和结束时间,只有从命令行查看

mysqlbinlog /var/lib/mysql/host51.000002

找到e f g三条数据插入命令的日志记录

比如:下面是插入e记录的完成日志

#220605  4:09:52  server  id  51  end_log_pos  1395 CRC32…..

SET TIMESTAMP=1654416592/*!*/;

insert into tarena.user(name) values(“e”)

/*!*/

# at 1395

#220605  4:09:52  server  id  51  end_log_pos  1426 CRC32…..

COMMIT /*!*/;

# at 1426

#220605  4:09:54  server  id  51  end_log_pos  1491 CRC32…..

#恢复e这条数据,插入命令执行的开始时间是220605  4:09:52,结束时间是220605  4:09:54

mysqlbinlog --start-datetime=”2022/06/05 4:09:52” --stop-datetime=”2022/06/05 4:09:54” | mysql –uroot –p123456

Logo

码道开发者社区,聚焦华为云码道 CodeArts 代码智能体,沉淀 Agent、Skill、鸿蒙开发实战内容,供开发者查阅资料、交流技术、分享工程实践

更多推荐