mysqlbinlog数据库简单恢复例子

mysqlbinlog常见的选项有以下几个:

--start-datetime:从二进制日志中读取指定等于时间戳或者晚于本地计算机的时间

--stop-datetime:从二进制日志中读取指定小于时间戳或者等于本地计算机的时间 取值和上述一样

--start-position:从二进制日志中读取指定position 事件位置作为开始。

--stop-position:从二进制日志中读取指定position 事件位置作为事件截至

执行查看当天记录日记的文件 

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000024 |   227373 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
再执行刷新日记记录: 
mysql> flush logs;

Query OK, 0 rows affected (0.02 sec)

使用mysqlbinlog导出sql语句

mysqlbinlog 具体路径地址 -d 数据库名 -vvv --base64-output=decode-rows mysql-bin.0001具体地址 > 导出具体路径+文件名

例子:

[root]#/www/server/mysql/bin/mysqlbinlog -d webdb   -vvv --base64-output=decode-rows  /www/server/data/mysql-bin.000016 > /www/server/data/111.sql

也可以增加时间点查询或者增加具体到某个表的操作 |grep '`ls_order`

[root]#www/server/mysql/bin/mysqlbinlog -d webdb --start-datetime='2021-11-22 00:00:00' --stop-datetime='2021-11-24 15:26:38'  /www/server/data/mysql-bin.000015 |grep '`ls_order`' >>/www/server/data/ls_order01.sql

也可以以操作节点去查询。

[root]#www/server/mysql/bin/mysqlbinlog -d webdb --start-position=875 --stop-position=21564  /www/server/data/mysql-bin.000015 |grep '`ls_order`' >>/www/server/data/ls_order01.sql

重新运行导入丢失sql

1.shell > mysql -uroot -p
Enter password: 

输入数据库密码,回车。
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 59
Server version: 5.6.50-log Source distribution

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

2.切换操作数据库。
mysql> use  webdb;
Database changed mysql
-> source /root/binlog2sql/ls_order01.sql; 回车 3.执行还原数据。再查看一下数据是否回复。

 

[root]#www/server/mysql/bin/mysqlbinlog -d dingho_cc --start-datetime='2021-11-22 00:00:00' --stop-datetime='2021-11-24 15:26:38'  /www/server/data/mysql-bin.000015|grep '`ls_order`' >>/www/server/data/ls_order01.sql
posted @ 2022-04-21 22:11  张志健  阅读(54)  评论(0编辑  收藏  举报