众所周知,binlog日志对于mysql数据库来说是十分重要的。在数据丢失的紧急情况下,我们往往会想到用binlog日志功能进行数据恢复(定时全备份+binlog日志恢复增量数据部分),化险为夷!
一、简单了解binlog
MySQL的二进制日志binlog可以说是MySQL最重要的日志,它记录了所有的DDL和DML语句(除了数据查询语句select),以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日志是事务安全型的。
===========================================================
DDL
- Data Definition Language 数据库定义语言
主要的命令有CREATE、ALTER、DROP等,DDL主要是用在定义或改变表(TABLE)的结构,数据类型,表之间的链接和约束等初始化工作上,他们大多在建立表时使用。
DML
- Data Manipulation Language 数据操纵语言
主要的命令是SELECT、UPDATE、INSERT、DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作的语言。
===========================================================
mysqlbinlog常见的选项有以下几个:
--start-datetime:从二进制日志中读取指定等于时间戳或者晚于本地服务器的时间。
--stop-datetime:从二进制日志中读取指定小于时间戳或者等于本地服务器的时间 取值和上述一样。
--start-position:从二进制日志中读取指定position 事件位置作为开始。
--stop-position:从二进制日志中读取指定position 事件位置作为事件截至。
binlog日志有两个最重要的使用场景:
1)MySQL主从复制:MySQL Replication在Master端开启binlog,Master把它的二进制日志传递给slaves来达到master-slave数据一致的目的。
2)自然就是数据恢复了,通过使用mysqlbinlog工具来使恢复数据。
binlog日志包括两类文件:
1)二进制日志索引文件(文件名后缀为.index)用于记录所有的二进制文件。
2)二进制日志文件(文件名后缀为.00000*)记录数据库所有的DDL和DML(除了数据查询语句select)语句事件。
二、开启binlog日志功能
0)查找mysql配置文件位置
[root@localhost ~]# find / -name 'my.cnf' /etc/my.cnf
1)编辑打开mysql配置文件/etc/my.cnf
在[mysqld] 区块添加
log-bin=mysql-bin 确认是打开状态(mysql-bin 是日志的基本名或前缀名)
注意:每次服务器(数据库)重启,服务器会调用flush logs;,新创建一个binlog日志!
[root@localhost ~]# vim /etc/my.cnf
2)重启mysqld服务使配置生效
[root@localhost ~]# systemctl restart mysql.service
3)查看binlog日志是否开启
登录Mysql 数据库,执行以下命令,查看 log_bin的值是否为 ON,XXXXXXX 为数据库数据路径,YYYYYYY为数据库安装地址。
MariaDB [(none)]> show variables like 'log_%'; +---------------------------------+--------------------------------------------------------------- | Variable_name | Value +---------------------------------+--------------------------------------------------------------- | log_bin_basename | XXXXXXX/mysql-bin | log_bin_compress | OFF | log_bin_compress_min_len | 256 | log_bin_index | XXXXXXX/mysql-bin.index | log_bin_trust_function_creators | OFF | log_error | YYYYYYY/mysql/logs/mysqld.log | log_output | FILE | log_queries_not_using_indexes | OFF | log_slave_updates | OFF | log_slow_admin_statements | ON | log_slow_filter | admin,filesort,filesort_on_disk,full_join,full_scan,query_cache,query_cache_miss,tmp_table,tmp_table_on_disk | | log_slow_rate_limit | 1 | log_slow_slave_statements | ON | log_slow_verbosity | | log_tc_size | 24576 | log_warnings | 2 +---------------------------------+------------------------------------------------------------ 17 rows in set (0.17 sec)
三、常用的binlog日志操作命令
1)查看所有binlog日志列表,
执行命令:show master logs;
MariaDB [(none)]> show master logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 1002 | | mysql-bin.000002 | 1548 | | mysql-bin.000003 | 365 | | mysql-bin.000004 | 342 | | mysql-bin.000005 | 342 | +------------------+-----------+ 5 rows in set (0.15 sec)
2)查看master状态,即最后(最新)一个binlog日志的编号名称,及其最后一个操作事件pos结束点(Position)值,
执行命令:show master status;
MariaDB [(none)]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000005 | 342 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
3)flush刷新log日志,自此刻开始产生一个新编号的binlog日志文件
执行命令:flush logs;
MariaDB [(none)]> flush logs; Query OK, 0 rows affected (0.38 sec) MariaDB [(none)]> show master logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 1002 | | mysql-bin.000002 | 1548 | | mysql-bin.000003 | 365 | | mysql-bin.000004 | 342 | | mysql-bin.000005 | 389 | | mysql-bin.000006 | 385 | +------------------+-----------+ 6 rows in set (0.00 sec)
注意:每当mysqld服务重启时,会自动执行此命令,刷新binlog日志;在mysqldump备份数据时加 -F 选项也会刷新binlog日志;
4)重置(清空)所有binlog日志
执行命令:reset master;
MariaDB [(none)]> reset master; Query OK, 0 rows affected (0.02 sec) MariaDB [(none)]> show master logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 328 | +------------------+-----------+ 1 row in set (0.00 sec)
四、查看binlog日志内容,常用有两种方式
1)使用mysqlbinlog自带查看命令法:
注意:
-->binlog是二进制文件,普通文件查看器cat、more、vim等都无法打开,必须使用自带的mysqlbinlog命令查看
-->binlog日志与数据库文件在同目录中
-->在MySQL5.5以下版本使用mysqlbinlog命令时如果报错,就加上 “--no-defaults”选项
查看mysql的数据存放目录,执行以下命令:
[root@localhost ~]# ps -ef|grep mysql
进入mysql数据目录,查看当前文件列表:
[root@localhost mysql]# ls 000001.log aria_log.00000001 ib_buffer_pool ib_logfile0 ibtmp1 multi-master.info mysql-bin.000001 performance_schema 000001.sql aria_log_control ibdata1 ib_logfile1 localhost.pid mysql mysql-bin.index school
使用mysqlbinlog命令查看binlog日志内容,下面截取其中的一个片段分析:
[root@localhost mysql]# mysqlbinlog mysql-bin.000001 ........ # at 370 #190224 23:11:32 server id 1 end_log_pos 506 CRC32 0x9784841e Query thread_id=10 exec_time=1 error_code=0 use `school`/*!*/; SET TIMESTAMP=1551021092/*!*/; SET @@session.pseudo_thread_id=10/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1, @@session.check_constraint_checks=1/*!*/; SET @@session.sql_mode=1411383296/*!*/; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; /*!\C utf8 *//*!*/; SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; insert into list(name,passwd) values ('wangchao','wangchao-passwd') #执行的sql语句 /*!*/; # at 506 #190224 23:11:32 server id 1 end_log_pos 537 CRC32 0x6de5741d Xid = 17 #执行的时间 ........
解释:
server id 1 : 数据库主机的服务号;
end_log_pos 537: sql结束时的pos节点
thread_id=10: 线程号
2)上面这种办法读取出binlog日志的全文内容比较多,不容易分辨查看到pos点信息
下面介绍一种更为方便的查询命令:
命令格式:
mysql> show binlog events [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count];
参数解释:
IN 'log_name' :指定要查询的binlog文件名(不指定就是第一个binlog文件)
FROM pos :指定从哪个pos起始点开始查起(不指定就是从整个文件首个pos点开始算)
LIMIT [offset,] :偏移量(不指定就是0)
row_count :查询总条数(不指定就是所有行)
MariaDB [(none)]> show master logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 537 | +------------------+-----------+ 1 row in set (0.00 sec)
执行以下命令,show binlog events in 'mysql-bin.000001'\G;
MariaDB [(none)]> show binlog events in 'mysql-bin.000001'\G; *************************** 1. row *************************** Log_name: mysql-bin.000001 Pos: 4 Event_type: Format_desc Server_id: 1 End_log_pos: 256 Info: Server ver: 10.2.19-MariaDB-log, Binlog ver: 4 *************************** 2. row *************************** Log_name: mysql-bin.000001 Pos: 256 Event_type: Gtid_list Server_id: 1 End_log_pos: 285 Info: [] *************************** 3. row *************************** Log_name: mysql-bin.000001 Pos: 285 Event_type: Binlog_checkpoint Server_id: 1 End_log_pos: 328 Info: mysql-bin.000001 *************************** 4. row *************************** Log_name: mysql-bin.000001 Pos: 328 Event_type: Gtid Server_id: 1 End_log_pos: 370 Info: BEGIN GTID 0-1-1 *************************** 5. row *************************** Log_name: mysql-bin.000001 Pos: 370 Event_type: Query Server_id: 1 End_log_pos: 506 Info: use `school`; insert into list(name,passwd) values ('wangchao','wangchao-passwd') *************************** 6. row *************************** Log_name: mysql-bin.000001 Pos: 506 Event_type: Xid Server_id: 1 End_log_pos: 537 Info: COMMIT /* xid=17 */ 6 rows in set (0.01 sec) ERROR: No query specified MariaDB [(none)]>
上面这条语句可以将指定的binlog日志文件,分成有效事件行的方式返回,并可使用limit指定pos点的起始偏移,查询条数!
如下操作示例:
a)查询第一个(最早)的binlog日志:
MariaDB [(none)]> show binlog events\G;
b)指定查询 mysql-bin.000001这个文件:
MariaDB [(none)]> show binlog events in 'mysql-bin.000001'\G;
c)指定查询 mysql-bin.000001这个文件,从pos点:370开始查起:
MariaDB [(none)]> show binlog events in 'mysql-bin.000001' from 370\G;
d)指定查询 mysql-bin.000001这个文件,从pos点:370开始查起,查询1条(即1条语句)
MariaDB [(none)]> show binlog events in 'mysql-bin.000001' from 370 limit 1\G;
e)指定查询 mysql-bin.000001这个文件,从pos点:370开始查起,查询2条
MariaDB [(none)]> show binlog events in 'mysql-bin.000001' from 370 limit 0,2\G;
五、利用binlog日志恢复mysql数据
以下对school库的list表进行操作
MariaDB [(none)]> use school; Database changed MariaDB [school]> show tables; +------------------+ | Tables_in_school | +------------------+ | list | +------------------+ 1 row in set (0.00 sec) MariaDB [school]> desc list; +--------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+--------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(50) | YES | | NULL | | | passwd | varchar(100) | YES | | NULL | | +--------+--------------+------+-----+---------+-------+ 3 rows in set (0.01 sec) MariaDB [school]>
事先插入两条数据
MariaDB [school]> insert into list (id,name,passwd) values (1,'lisi','lisi-passwd'); Query OK, 1 row affected (0.00 sec) MariaDB [school]> insert into list (id,name,passwd) values (2,'lisi-2','lisi-passwd-2'); Query OK, 1 row affected (0.00 sec) MariaDB [school]> select * from list; +------+--------+---------------+ | id | name | passwd | +------+--------+---------------+ | 1 | lisi | lisi-passwd | | 2 | lisi-2 | lisi-passwd-2 | +------+--------+---------------+ 2 rows in set (0.00 sec)
1)、备份数据库: mysqldump最好写成绝对地址
[root@localhost ~]# mysqldump -uroot -p -B -F -R -x --master-data=2 school|gzip >/root/school_$(date +%F).sql.gz Enter password: [root@localhost ~]#
找不到mysqldump命令地址的,执行以下命令:
[root@localhost ~]# whereis mysqldump
查看备份文件:
[root@localhost ~]# ls anaconda-ks.cfg initial-setup-ks.cfg school_2019-02-26.sql.gz 公共 模板 视频 图片 文档 下载 音乐 桌面 [root@localhost ~]#
-----------------
参数说明:
-B:指定数据库
-F:刷新日志
-R:备份存储过程等
-x:锁表
--master-data:在备份语句里添加CHANGE MASTER语句以及binlog文件及位置点信息
-----------------
待到数据库备份完成,就不用担心数据丢失了,因为有完全备份数据在!!
由于上面在全备份的时候使用了-F选项,那么当数据备份操作刚开始的时候系统就会自动刷新log,这样就会自动产生一个新的binlog日志,这个新的binlog日志就会用来记录备份之后的数据库“增删改”操作
查看以下:
MariaDB [school]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002 | 371 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
2)、这时模拟误操作(插入2条数据,删除数据库)
MariaDB [school]> insert into list (id,name,passwd) values (3,'lisi-3','lisi-passwd3'),(4,'lisi-4','lisi-passwd4');
3)、修改数据
MariaDB [school]> update list set passwd='lisi-passwd33' where id=3; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [school]> select * from list; +------+--------+---------------+ | id | name | passwd | +------+--------+---------------+ | 1 | lisi | lisi-passwd | | 2 | lisi-2 | lisi-passwd-2 | | 3 | lisi-3 | lisi-passwd33 | | 4 | lisi-4 | lisi-passwd4 | +------+--------+---------------+ 4 rows in set (0.00 sec)
4)、删除school数据库
MariaDB [school]> drop database school; Query OK, 1 row affected (0.15 sec)
5)、恢复数据准备
先仔细查看最后一个binlog日志,并记录下关键的pos点,到底是哪个pos点的操作导致了数据库的破坏(通常在最后几步);
先备份一下最后一个binlog日志文件:
[root@localhost mysql]# cp -v mysql-bin.000003 /backup/ ##带有 -v (verbose)选项,cp命令将告诉用户正在做什么 "mysql-bin.000003" -> "/backup/mysql-bin.000003"
接着执行一次刷新日志索引操作,重新开始新的binlog日志记录文件。按理说mysql-bin.000003
这个文件不会再有后续写入了,因为便于我们分析原因及查找ops节点,以后所有数据库操作都会写入到下一个日志文件。
MariaDB [(none)]> flush logs; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 385 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
6)、读取binlog日志,分析问题。
方法一:使用mysqlbinlog读取binlog日志:
进入数据库存储目录,执行以下命令:
[root@localhost mysql]# mysqlbinlog mysql-bin.000003
方法二:登录服务器,并查看(推荐此种方法)
mysql命令行执行以下命令:show binlog events in 'mysql-bin.000003';
MariaDB [(none)]> show binlog events in 'mysql-bin.000002'; +------------------+-----+-------------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+-----+-------------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------+ | mysql-bin.000002 | 4 | Format_desc | 1 | 256 | Server ver: 10.2.19-MariaDB-log, Binlog ver: 4 | | mysql-bin.000002 | 256 | Gtid_list | 1 | 285 | [] | | mysql-bin.000002 | 285 | Binlog_checkpoint | 1 | 328 | mysql-bin.000001 | | mysql-bin.000002 | 328 | Binlog_checkpoint | 1 | 371 | mysql-bin.000002 | | mysql-bin.000002 | 371 | Gtid | 1 | 413 | BEGIN GTID 0-1-1 | | mysql-bin.000002 | 413 | Query | 1 | 578 | use `school`; insert into list (id,name,passwd) values (3,'lisi-3','lisi-passwd3'),(4,'lisi-4','lisi-passwd4') | | mysql-bin.000002 | 578 | Xid | 1 | 609 | COMMIT /* xid=179 */ | | mysql-bin.000002 | 609 | Stop | 1 | 632 | | +------------------+-----+-------------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------+ 8 rows in set (0.00 sec) MariaDB [(none)]> show binlog events in 'mysql-bin.000003'; +------------------+-----+-------------------+-----------+-------------+-----------------------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+-----+-------------------+-----------+-------------+-----------------------------------------------------------------+ | mysql-bin.000003 | 4 | Format_desc | 1 | 256 | Server ver: 10.2.19-MariaDB-log, Binlog ver: 4 | | mysql-bin.000003 | 256 | Gtid_list | 1 | 299 | [0-1-1] | | mysql-bin.000003 | 299 | Binlog_checkpoint | 1 | 342 | mysql-bin.000003 | | mysql-bin.000003 | 342 | Gtid | 1 | 384 | BEGIN GTID 0-1-2 | | mysql-bin.000003 | 384 | Query | 1 | 502 | use `school`; update list set passwd='lisi-passwd33' where id=3 | | mysql-bin.000003 | 502 | Xid | 1 | 533 | COMMIT /* xid=7 */ | | mysql-bin.000003 | 533 | Gtid | 1 | 575 | GTID 0-1-3 | | mysql-bin.000003 | 575 | Query | 1 | 664 | drop database school | | mysql-bin.000003 | 664 | Rotate | 1 | 711 | mysql-bin.000004;pos=4 | +------------------+-----+-------------------+-----------+-------------+-----------------------------------------------------------------+ 9 rows in set (0.00 sec)
或者:
MariaDB [(none)]> show binlog events in 'mysql-bin.000003'\G;
通过分析,造成数据库破坏的pos点区间是介于 575--664 之间(这是按照日志区间的pos节点算的),只要恢复到875前就可。
7)、先把之前模拟全备份的数据恢复:
[root@localhost ~]# ls anaconda-ks.cfg initial-setup-ks.cfg school_2019-02-26.sql.gz 公共 模板 视频 图片 文档 下载 音乐 桌面 [root@localhost ~]# gzip -d school_2019-02-26.sql.gz [root@localhost ~]# ls anaconda-ks.cfg initial-setup-ks.cfg school_2019-02-26.sql 公共 模板 视频 图片 文档 下载 音乐 桌面 [root@localhost ~]# mysql -uroot -p -v < school_2019-02-26.sql Enter password: .... ....
这样就恢复了之前全备份的数据。查看MySQL数据库
MariaDB [(none)]> show databases; #发现school库已经恢复回来了 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | +--------------------+ 4 rows in set (0.32 sec) MariaDB [(none)]> use school; Database changed MariaDB [school]> show tables; +------------------+ | Tables_in_school | +------------------+ | list | +------------------+ 1 row in set (0.00 sec) MariaDB [school]> select * from list; +------+--------+---------------+ | id | name | passwd | +------+--------+---------------+ | 1 | lisi | lisi-passwd | | 2 | lisi-2 | lisi-passwd-2 | +------+--------+---------------+ 2 rows in set (0.00 sec)
这仅仅恢复了一部分数据,之后新添加、修改的数据还没有恢复!!!!
这可以根据前面提到的mysql-bin.000003的新binlog日志进行恢复。
8)、从binlog日志恢复数据
恢复命令的语法格式:
mysqlbinlog mysql-bin.0000xx | mysql -u用户名 -p密码 数据库名
--------------------------------------------------------
常用参数选项解释:
--start-position=875 起始pos点
--stop-position=954 结束pos点
--start-datetime="2016-9-25 22:01:08" 起始时间点
--stop-datetime="2019-9-25 22:09:46" 结束时间点
--database=zyyshop 指定只恢复zyyshop数据库(一台主机上往往有多个数据库,只限本地log日志)
--------------------------------------------------------
不常用选项:
-u --user=name 连接到远程主机的用户名
-p --password[=name] 连接到远程主机的密码
-h --host=name 从远程主机上获取binlog日志
--read-from-remote-server 从某个MySQL服务器上读取binlog日志
--------------------------------------------------------
小结:实际是将读出的binlog日志内容,通过管道符传递给mysql命令。这些命令、文件尽量写成绝对路径;
a)完全恢复(需要手动vim编辑mysql-bin.000003,将那条drop语句剔除掉)
查找之前备份的文件,手动删除 drop 语句,
[root@localhost /]# ls backup bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@localhost /]# cd backup/ [root@localhost backup]# ls mysql-bin.000003 [root@localhost backup]# [root@localhost backup]# mysqlbinlog mysql-bin.000003 > /backup/000003.sql [root@localhost backup]# ls 000003.sql mysql-bin.000003 [root@localhost backup]# vim /backup/000003.sql [root@localhost backup]# mysql -uroot -p -v < /backup/000003.sql
温馨提示:
在恢复全备数据之前必须将该binlog文件移出,否则恢复过程中,会继续写入语句到binlog,最终导致增量恢复数据部分变得比较混乱!
b)指定pos结束点恢复(部分恢复):
--stop-position=384 pos结束节点(按照事务区间算,是384)
注意:此pos结束节点介于“member表原始数据”与更新“name='lisi-3'”之前的数据,这样就可以恢复到更改“name='lisi-3'”之前的数据了。
操作如下:
[root@localhost mysql]# /usr/bin/mysqlbinlog --stop-position=632 --database=school mysql-bin.000002 | /usr/bin/mysql -uroot -p -v school Enter password:
MariaDB [school]> select * from list; +------+--------+---------------+ | id | name | passwd | +------+--------+---------------+ | 1 | lisi | lisi-passwd | | 2 | lisi-2 | lisi-passwd-2 | | 3 | lisi-3 | lisi-passwd3 | | 4 | lisi-4 | lisi-passwd4 | +------+--------+---------------+ 4 rows in set (0.00 sec) MariaDB [school]>
恢复截止到更改“name='李四'”之间的数据(按照事务区间算,是673)
[root@localhost mysql]# /usr/bin/mysqlbinlog --stop-position=533 --database=school mysql-bin.000003 | /usr/bin/mysql -uroot -p -v school Enter password:
MariaDB [school]> select * from list; +------+--------+---------------+ | id | name | passwd | +------+--------+---------------+ | 1 | lisi | lisi-passwd | | 2 | lisi-2 | lisi-passwd-2 | | 3 | lisi-3 | lisi-passwd33 | | 4 | lisi-4 | lisi-passwd4 | +------+--------+---------------+ 4 rows in set (0.00 sec) MariaDB [school]>
另外:也可以指定时间节点区间恢复(部分恢复),就是说除了用pos节点的办法进行恢复,也可以通过指定时间节点区间进行恢复,按时间恢复需要用mysqlbinlog命令读取binlog日志内容,找时间节点。
首先要分析对应数据库数据添加的时间,然后关闭数据库连接,直接从本机导出,
[root@localhost mysql]# mysqlbinlog mysql-bin.000002 ...................................... # at 556 #190217 22:48:21 server id 1 end_log_pos 598 CRC32 0x07e39fb7 GTID 0-1-5 trans /*!100001 SET @@session.gtid_seq_no=5*//*!*/; BEGIN /*!*/; # at 598 #190217 22:48:21 server id 1 end_log_pos 734 CRC32 0xf9baa720 Query thread_id=19 exec_time=0 error_code=0 SET TIMESTAMP=1550414901/*!*/; insert into list (id,name,passwd) values (102,'lisi','lisi-passwd') /*!*/; # at 734 #190217 22:48:21 server id 1 end_log_pos 765 CRC32 0xccc4b8a3 Xid = 249 COMMIT/*!*/; # at 765 #190217 22:49:57 server id 1 end_log_pos 807 CRC32 0xcedb7648 GTID 0-1-6 trans /*!100001 SET @@session.gtid_seq_no=6*//*!*/; BEGIN /*!*/; # at 807 #190217 22:49:57 server id 1 end_log_pos 905 CRC32 0x07a64448 Query thread_id=19 exec_time=0 error_code=0 SET TIMESTAMP=1550414997/*!*/; delete from list where id=102 /*!*/; # at 905 #190217 22:49:57 server id 1 end_log_pos 936 CRC32 0x146fcced Xid = 253 COMMIT/*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/; .......................................
例如导出“2019-02-17 22:48:20” 到 “2019-02-17 22:48:22” 的binlog数据:
[root@localhost mysql]# mysqlbinlog --start-datetime='2019-02-17 22:48:20' --stop-datetime='2019-02-17 22:48:22' /data/mysql/mysql-bin.000002 >000001.sql
再把对应的sql文件导入到数据库中
[root@localhost mysql]# mysql -uroot -p school < /data/mysql/000001.sql Enter password: [root@localhost mysql]#
MariaDB [school]> select * from list; +------+----------+-----------------+ | id | name | passwd | +------+----------+-----------------+ | 99 | zhangsan | zhangsan-passwd | | 100 | lisi | lisi-passwd | | 101 | lisi | lisi-passwd | +------+----------+-----------------+ 3 rows in set (0.00 sec) MariaDB [school]> select * from list; +------+----------+-----------------+ | id | name | passwd | +------+----------+-----------------+ | 99 | zhangsan | zhangsan-passwd | | 100 | lisi | lisi-passwd | | 101 | lisi | lisi-passwd | | 102 | lisi | lisi-passwd | +------+----------+-----------------+ 4 rows in set (0.00 sec) MariaDB [school]>
本文为崔凯原创文章,转载无需和我联系,但请注明来自冷暖自知一抹茶ckhttp://www.cksite.cn