通過Linux命令過濾出binlog中完整的SQL語句

來源:互聯網
上載者:User

通過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: 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對資料進行恢複的實戰 

MySQL 5.6.12切換binlog二進位日誌路徑 

mysqlbinlog解析binlog亂碼問題解密 

MySQL通過binlog來恢複資料 

MySQL binlog 組提交與 XA(兩階段交易認可) 

MySQL使用備份和binlog進行資料恢複

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.