通過Linux命令過濾出binlog中完整的SQL語句
DB: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')
;
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')
;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28939273/viewspace-1965787/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL_通過binlog檢視原始SQL語句MySql
- MySQL在ROW模式下通過binlog提取SQL語句MySql模式
- 通過SQL語句提取儲存過程中的內容SQL儲存過程
- 通過sql語句分析足彩SQL
- 通過java程式抽取日誌中的sql語句JavaSQL
- 通過分析SQL語句的執行計劃優化SQL語句SQL優化
- oracle 通過sql profile為sql語句加hintOracleSQL
- 通過SQL PROFILE自動優化SQL語句SQL優化
- 一個過濾重複資料的sql語句(轉)SQL
- oracle自定義過程來獲得完整的sql語句OracleSQL
- ORACLE 通過SPM為SQL語句加HINTOracleSQL
- 通過java來格式化sql語句JavaSQL
- 通過使用hint unnest調優sql語句SQL
- 完整sql語句輸出的工具類SQL
- 通過ORA錯誤反思sql語句規範SQL
- 通過sql語句分析足彩(第三篇)SQL
- 在 laravel 中輕鬆容易的輸出完整的 sql 語句LaravelSQL
- 通過分析SQL語句的執行計劃優化SQL 二SQL優化
- 通過圖表簡化sql語句的表關聯SQL
- SQL語句的處理過程SQL
- 用 Phoenix 通過 SQL 語句更新操作 HBase 資料SQL
- 通過分析SQL語句的執行計劃優化SQL(總結)SQL優化
- 透過java程式抽取日誌中的sql語句JavaSQL
- 通過日誌檢視mysql正在執行的SQL語句MySql
- 【轉】通過sql語句獲取資料庫的基本資訊SQL資料庫
- 透過sql語句分析足彩SQL
- Oracle釋出一個SQL語句的處理過程OracleSQL
- CoreData執行過程的sql語句SQL
- SQL語句的處理過程修正SQL
- 剖析SQL語句的執行過程SQL
- Oracle SQL 語句的執行過程OracleSQL
- 【AWR】通過AWR報告中記錄的 SQL Id獲得SQL語句的執行計劃SQL
- mysql執行sql語句過程MySql
- Linux基礎命令---文字過濾colrmLinux
- Linux基礎命令—文字過濾colLinux
- Linux基礎命令---文字過濾colLinux
- 通過儲存過程執行通過DBLINK的查詢語句失敗-單個語句成功--ORA-00604儲存過程
- 一條sql語句的執行過程SQL