linux命令行过滤log,通过Linux命令过滤出binlog中完整的SQL语句

DB:MySQL 5.6.16 CentOS:CentOS release 6.3 (Final) 当insert语句通过空格跨行输入的时候,如何提取完整的insert语句! 创建一个空表: mysql create table yoon as select * from sakila.actor where 1=0; Query OK, 0 rows affected (0.06 sec) Records

DB:MySQL 5.6.16

CentOS:CentOS release 6.3 (Final)

当insert语句通过空格跨行输入的时候,如何提取完整的insert语句!

创建一个空表:

mysql> create table yoon as select * from sakila.actor where 1=0;

Query OK, 0 rows affected (0.06 sec)

Records: 0  Duplicates: 0  Warnings: 0

查看表名:

mysql> show tables;

+----------------+

| Tables_in_yoon |

+----------------+

| yoon          |

+----------------+

1 row in set (0.00 sec)

查看数据:

mysql> select * from yoon;

Empty set (0.00 sec)

查看表结构:

mysql> desc yoon;

+-------------+----------------------+------+-----+-------------------+-----------------------------+

| Field      | Type                | Null | Key | Default          | Extra                      |

+-------------+----------------------+------+-----+-------------------+-----------------------------+

| actor_id    | smallint(8) unsigned | NO  |    | 0                |                            |

| first_name  | varchar(45)          | NO  |    | NULL              |                            |

| last_name  | varchar(45)          | NO  |    | NULL              |                            |

| last_update | timestamp            | NO  |    | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |

+-------------+----------------------+------+-----+-------------------+-----------------------------+

4 rows in set (0.00 sec)

夸行方式插入测试数据:

mysql> insert into yoon

->

-> values

->

-> (1,'YOON','HANK',2006-02-15 04:34:33)

->

-> ;

查看mysql-binlog日志:

[root@hank-yoon data]# ls

auto.cnf            hank      ibdata1  ib_logfile0  ib_logfile2  mysql-bin.000043  mysql-bin.000045  performance_schema  test  yoon.sql

binlog-rollback.pl  hank.sql  ibdata2  ib_logfile1  mysql        mysql-bin.000044  mysql-bin.index  sakila              yoon

将binlog数据转换到yoon.sql:

[root@hank-yoon data]# mysqlbinlog mysql-bin.000045 > yoon.sql

过滤出insert语句:

[root@hank-yoon data]# more yoon.sql | grep --ignore-case -E 'insert' -A2 -B2 | grep yoon > hank.sql

查看insert语句,发现跨行插入的SQL语句截至到insert into yoon就结束:

[root@hank-yoon data]# cat hank.sql

insert into yoon (first_name,last_name) select first_name,last_name from hank

insert into yoon

通过以下命令,可以查看完整的SQL语句,即使是跨行插入,分好(;)都给你带上:

[root@hank-yoon data]# sed  -n  "/insert into yoon/,/;/p"  yoon.sql  |sed  's#\/\*!\*\/##'

insert into yoon (first_name,last_name) select first_name,last_name from hank

;

insert into yoon

values

(1,'YOON','HANK','2006-02-15 04:34:33')

;

关于使用MySQL binlog对数据进行恢复的实战  http://www.68idc.cn/Linux/2016-01/127808.htm

MySQL 5.6.12切换binlog二进制日志路径  http://www.68idc.cn/Linux/2016-01/126979.htm

mysqlbinlog解析binlog乱码问题解密  http://www.68idc.cn/Linux/2016-01/126978.htm

MySQL通过binlog来恢复数据  http://www.68idc.cn/Linux/2015-12/126897.htm

MySQL binlog 组提交与 XA(两阶段提交)  http://www.68idc.cn/Linux/2015-11/124942.htm

MySQL使用备份和binlog进行数据恢复 http://www.68idc.cn/Linux/2014-12/110875.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值