搜尋
首頁資料庫mysql教程MySQL 解密 --> 如何查看二進位日誌ROW模式下最原始的SQL語句

MySQLbinlogROW模式解析

ROW

      在mysql5.6以後,主從資料一致性要求變高了,statement格式逐漸不太適合業務的需求了,所以生產環境大家都採用了row模式,row模式是傳輸最底層的數據變化的insert的模組來進行主從數據的傳輸,那麼在binlog裡面就和普通的statement模式有何差別?能否看到最原始的sql語句呢?

 

1、準備輸入資料

 

 

 




 

 

 

 

2Row Row模式下面,binlog裡面的ddl語句是正常顯示的,但是dml是亂碼,如下圖: 

[root@hch_test_dbm1_121_63 binlog]sql# 3/usr/local /binr/local …… #

這裡前面的省略

#160722 17:02:38 server id 62  end_log_pos 4291 CRC32 0x369e3244     Query    271 error_code=0

SET TIMESTAMP=1469178158/*!*/;CREATE DATABASE IF NOT EXISTS `percona` /* pt-table-checksum */

/*!*/;

# at 4291#160722 17:02:38 server id 62  end_log_pos 5079 32:38 server read_id=60         exec_time=4294967271 error_code=0 use `percona`/*!*/;

SET TIMESTAMP=1469178158/*!*/;

CREATE TABLE IF NOT EXISTS  `percona`.`checksums` (🠎🠟  char(64)     NOT NULL,

      tbl char(64)     NOT NULL,

      chunk          int                NULL,

      chunk_index    varchar(200)     NULL,🎠  

      upper_boundary text              NULL,

    ,

      this_cnt       int          NOT NULL,

      master_crc     char(40)         NULL,

      master_cnt     int              NULL,

      ts             timestamp    NOT NULL DEFAULT CURRENT_TIMESTAMP ON  UPDATE CURRENT_TIMESTAMP,

      PRIMARY KEY (db, tbl, chunk),

      INDEX ts_db_tbl (ts, db, tbl)

  )  ENGINE=InnoDB

/*!*/;

# at 5079

#160820 10:21:1050 8e     Query       thread_id=16         exec_time=0 error_code=0

SET TIMESTAMP=1471659670/*!*/;

SET @@session.sql_mode=1075838976/*!*/;

GRANT ALL PRIVWE ON. BY PASSWORD  '*2976819BD2CCD13612E03F812A2CD297C1A18B23'

/*!*/;

# at 5280

#16088 CRC32 0x85811be7     Query       thread_id=18         exec_time=0    error_code=0

test

test `/*!*/;

SET TIMESTAMP=1471659760/*!*/;

create table test1(id int,c1  varchar(20),type int,address varchar(20),create_time date)* !*/;

# at 5445

#160820 10:24:34 server id 63  end_log_pos 5580 CRC   exec_time=0    error_code=0

SET TIMESTAMP=1471659874/*!*/;

alter table test1 modify  `address` varchar(200) DEFAULT NULL

/*!*/;

# at 5580

#160820 10:24:36 伺服器id 63  end_log_pos 5660 CRC32 0x7b7c645f      查詢       thread_id=1800time

SET TIMESTAMP=1471659876/*!*/ ;

SET @@session.time_zone='SYSTEM'/ *!*/;

BEGIN

/*!*/;

# 在5660

# 在5764

#160820 10:24:36 CRCID 63 58  Table_map : `測試`.`測試1 ` 對應編號74

#,位於5821

#160820 10:24:36 伺服器id 63  end_log_pos 5911 CRC32 0x2f577f52 STM  標記: 

 

BINLOG '

ZL+3VxM/AAAAOQAAAL0WAAAAAEoAAAAAAAEABHRlc3QABXRlc3QxAAUDDwMPEgU8AFgCAB/ DlLwI

ZL+3Vx4/AAAAWgAAABcXAAAAAEoAAAAAAAEAAgAF/+ABAAAACHpoYW5nc2FuAQAAAB4Aemhhbmdz

YW4gcm9hZCBObyA4NemhhAmmA4NN;

# 在5911

#160820 10:24:36 伺服器ID 63  end_log_pos 5942 CRC32 0xb26af81b      Xid = 19999926 /* !*/;

# 在5942

#160820 10:24:48 伺服器id 63  end_log_pos 6022 CRC   exec_time=0    error_code=0

設定時間戳=1471659888/*!*/;

開始

/*!*/;

# at 6022

# at 6122

#160820 10:24:48 伺服器ID 63 :`test`.`test1` 映射到數字7 4

#在6179

#160820 10:24:48 伺服器id 63  end_log_pos 6265 CRC32 0x5f7ad700   '

cL+3VxM /AAAAOQAAACMYAAAAAAEoAAAAAAAEABHRlc3QABXRlc3QxAAUDDwMPEgU8AFgCAB9Lw23c

cL+3Vx4/AAAAVgAAAHAAAAAAAAAAAAAAAAAEA /+ACAAAABGxpc2kBAAAAHgB6aGFuZ3NhbiBy

b2FkIE5vIDg3MCxmbG9vciA2MDKZmiimMADXel8=

'/*!*/;  end_log_pos 6296 CRC32 0xf6833d28   Xid = 200

COMMIT/*!*/;

# at 6296

#160820 10:31:30 伺服器id 63  end_log_pos 6343 CRC32 0xcfcdd344    日誌檔案結束

ROLLBACK /*由mysqlbinlog 新增*/ ;

/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*​​/;

/*!50530 SET @@SESSION.PSEUDO_SL*/;

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/test

_test

 

 

 

 

3、透過--base64-output  執行指令是:/usr /local/mysql/bin/mysqlbinlog  --base64-output=decode-rows -v  mysql-bin.000215 

可以看到以下效果,不過都是最原始的d

[root@hch_test_dbm1_121_63 binlog]#  /usr/local/mysql/bin/mysqlbinlog   --base64-output=decode-rows -v  binlog   --base64-output=decode-rows -v   mylog-bin EUDO_SLAVE_MODE= 1*/;

/*!40019 SET @@session.max_insert_delayed_threads=0*/;

/*!50003 SET  @OLD_COMPLETION_TYPE=@COMPLETION_TYPE,COMPLETIONS888/LIS/E:202;

……#160820 10:24:36 server id 63  end_log_pos 5821 CRC32 0x08bc94c3      Table_map: `test`.`157775 月#160820 10:24:36 server id 63 end_log_pos 5911 CRC32 0x2f577f52       寫

###   @1=1

###    @2='zhangsan'

###   @3=1

###    @4='zhangsan road No

###    @4='zhangsan road No 870, oor 6050 =' 2016-08-20 10:24:36'

# at 5911

#160820 10:24:36 server idaf3  end_log_pos 5942 26910bx MIT/*!*/;

# at 5942

#160820 10:24:48 server id 63  end_log_pos 6022 CRC32 0x09eab31d     Query    0

SET TIMESTAMP=1471659888/*!*/;

BEGIN

/*!*/;

# at 6022

# at 6122

#160820 10:24:48 server id 63  end_log_pos 6179 CRC32 0xdc634b test test34b test_app # at 6179

#160820 10:24 :48 server id 63  end_log_pos 6265 CRC32 0x5f7ad700      Write_rows: table id 74 flags: STMT_END_F#

test_ ERT INTO ``test11``test INS`test_``test.

###   @1= 2

###   @2='lisi'

###   @3=11

###   @3=1

###   @3=1

## #    @5='2016-08-20 10:24:48'

# at 6265

#160820 10:24:48 server id 63 #160820 10:24:48 server id 63  69385 200

COMMIT/*!*/; # at 6296

#160820 10:31:30 server id 63  end_log_pos 6343 CRC32 0xcfcdd344      # End of log file

ROLLBACK /* added by mysqlbinlog */;/*!50003 SET  COMPLETION_TYPE=@OLD_COMPLETION_TYPE*​​/;

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

 

 

 

PS這樣是可以看到了 塊,而看不到原始的過來的insert語句,很多時候排查問題需要原始的insert語句而不是底層的insert塊。所以問題來了,如何看到最原始的insert sql語句呢?     

4

、透過查看

 

4.1)先在線上設置全域的binlog_rows_query_log_events參數,刷新日誌:

mysql>                           Query OK, 0 位logs;Query OK, 0 rows affected (0.01 sec) mysql>exit

 [root@hch_test_dbm1_121_63 ~]# mysql  -utim -ptimgood -h192.168.121.63facesql  -utim -ptimgood -h192.168.121.633faced inface faces.

 

 

4.2

)然後重新登錄錄入新的數據記錄:

Welcome to the MySQL monitor .  Commands end with ; or g.

Your MySQL connection id is 19

Server version: 5.6.12-log Source  distribution. served . Oracle is a registered trademark of  Oracle Corporation and/or itsaffiliates. Other names may be trademarks  of their respectiveiliates. Other names may be trademarks  of their respectiveiliates. Other names may be trademarks  of their respectiveiliates. help. Type 'c'  to clear the current input statement.

 mysql>

mysql> use test;

Database changed

my> sert

Database ,floor 603',now();

Query OK, 1 row affected (0.00 sec)

Records: 1  Duplicates: 0  Warnings: 0

 

 

4.3)解析binlog,没有看到原始的insert语句

[root@hch_test_dbm1_121_63 binlog]#  /usr/local/mysql/bin/mysqlbinlog   --base64-output=decode-rows -v   mysql-bin.000216 >1.sql

[root@hch_test_dbm1_121_63 binlog]# more  1.sql

/*!50530 SET  @@SESSION.PSEUDO_SLAVE_MODE=1*/;

/*!40019 SET  @@session.max_insert_delayed_threads=0*/;

/*!50003 SET  @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 4

#160820 10:31:30 server id 63  end_log_pos 120 CRC32 0x1e4d0366       Start: binlog v 4, server v 5.6.12-log  created 160820 10:31:30

# Warning: this binlog is either in use  or was not closed properly.

# at 120

#160820 10:32:04 server id 63  end_log_pos 200 CRC32 0xab0e625e       Query       thread_id=19   exec_time=0         error_code=0

SET TIMESTAMP=1471660324/*!*/;

SET @@session.pseudo_thread_id=19/*!*/;

SET @@session.foreign_key_checks=1,  @@session.sql_auto_is_null=0, @@session.unique_checks=1,  @@session.autocommit=1/*!*/;

SET @@session.sql_mode=1075838976/*!*/;

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.time_zone='SYSTEM'/*!*/;

SET @@session.lc_time_names=0/*!*/;

SET  @@session.collation_database=DEFAULT/*!*/;

BEGIN

/*!*/;

# at 200

# at 302

#160820 10:32:04 server id 63  end_log_pos 359 CRC32 0x41bf2876        Table_map: `test`.`test1` mapped to  number 74

# at 359

#160820 10:32:04 server id 63  end_log_pos 447 CRC32 0x1a3ab3d8       Write_rows: table id 74 flags:  STMT_END_F

### INSERT INTO `test`.`test1`

### SET

###    @1=3

###    @2='wanger'

###    @3=3

###    @4='zhangsan road No 870,floor 603'

###    @5='2016-08-20 10:32:04'

# at 447

#160820 10:32:04 server id 63  end_log_pos 478 CRC32 0xc5081021        Xid = 208

COMMIT/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 SET  COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

/*!50530 SET  @@SESSION.PSEUDO_SLAVE_MODE=0*/;

[root@hch_test_dbm1_121_63 binlog]#

 

 

 

4.4)通过mysql的命令行查看最原始的insert sql语句,如下所示:


mysql>  show binlog events in 'mysql-bin.000216';
+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                                                             |
+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------+
| mysql-bin.000216 |   4 | Format_desc |        63 |         120 | Server ver: 5.6.12-log, Binlog ver: 4                                            |
| mysql-bin.000216 | 120 | Query       |        63 |         200 | BEGIN                                                                            || mysql-bin.000216 | 200 | Rows_query  |        63 |         302 | # insert into test1 select 3,'wanger','3','zhangsan road No 870,floor 603',now() || mysql-bin.000216 | 302 | Table_map   |        63 |         359 | table_id: 74 (test.test1)                                                        |
| mysql-bin.000216 | 359 | Write_rows  |        63 |         447 | table_id: 74 flags: STMT_END_F                                                   |
| mysql-bin.000216 | 447 | Xid         |        63 |         478 | COMMIT /* xid=208 */                                                             |
+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------+
6 rows in set (0.00 sec)
 
mysql>


 

 

 

 

5、試驗總結

基於以上的試驗,可以得出在row模式下,可以通過mysqlbinlog --base64-output=decode-rows –v查看最底層的insert資料模組,也可以透過命令列show binlog events in 'mysql-bin.000216';來即時查看最原始的insertsql記錄。

 

我們可以在搭建資料庫的時候,在啟動參數檔案my.cnf裡面事先設定好,如下圖所示:

#vim my..commcn =row  # binlog

日誌格式

binlog_rows_query_log_events = 1  #      

  以上是MySQL 解密--> 如何查看二進位日誌ROW模式下最原始的SQL語句的內容,更多相關內容請關注PHP中文網(www.php.cn)!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
您什麼時候應該使用複合索引與多個單列索引?您什麼時候應該使用複合索引與多個單列索引?Apr 11, 2025 am 12:06 AM

在數據庫優化中,應根據查詢需求選擇索引策略:1.當查詢涉及多個列且條件順序固定時,使用複合索引;2.當查詢涉及多個列但條件順序不固定時,使用多個單列索引。複合索引適用於優化多列查詢,單列索引則適合單列查詢。

如何識別和優化MySQL中的慢速查詢? (慢查詢日誌,performance_schema)如何識別和優化MySQL中的慢速查詢? (慢查詢日誌,performance_schema)Apr 10, 2025 am 09:36 AM

要優化MySQL慢查詢,需使用slowquerylog和performance_schema:1.啟用slowquerylog並設置閾值,記錄慢查詢;2.利用performance_schema分析查詢執行細節,找出性能瓶頸並優化。

MySQL和SQL:開發人員的基本技能MySQL和SQL:開發人員的基本技能Apr 10, 2025 am 09:30 AM

MySQL和SQL是開發者必備技能。 1.MySQL是開源的關係型數據庫管理系統,SQL是用於管理和操作數據庫的標準語言。 2.MySQL通過高效的數據存儲和檢索功能支持多種存儲引擎,SQL通過簡單語句完成複雜數據操作。 3.使用示例包括基本查詢和高級查詢,如按條件過濾和排序。 4.常見錯誤包括語法錯誤和性能問題,可通過檢查SQL語句和使用EXPLAIN命令優化。 5.性能優化技巧包括使用索引、避免全表掃描、優化JOIN操作和提升代碼可讀性。

描述MySQL異步主奴隸複製過程。描述MySQL異步主奴隸複製過程。Apr 10, 2025 am 09:30 AM

MySQL異步主從復制通過binlog實現數據同步,提升讀性能和高可用性。 1)主服務器記錄變更到binlog;2)從服務器通過I/O線程讀取binlog;3)從服務器的SQL線程應用binlog同步數據。

mysql:簡單的概念,用於輕鬆學習mysql:簡單的概念,用於輕鬆學習Apr 10, 2025 am 09:29 AM

MySQL是一個開源的關係型數據庫管理系統。 1)創建數據庫和表:使用CREATEDATABASE和CREATETABLE命令。 2)基本操作:INSERT、UPDATE、DELETE和SELECT。 3)高級操作:JOIN、子查詢和事務處理。 4)調試技巧:檢查語法、數據類型和權限。 5)優化建議:使用索引、避免SELECT*和使用事務。

MySQL:數據庫的用戶友好介紹MySQL:數據庫的用戶友好介紹Apr 10, 2025 am 09:27 AM

MySQL的安裝和基本操作包括:1.下載並安裝MySQL,設置根用戶密碼;2.使用SQL命令創建數據庫和表,如CREATEDATABASE和CREATETABLE;3.執行CRUD操作,使用INSERT,SELECT,UPDATE,DELETE命令;4.創建索引和存儲過程以優化性能和實現複雜邏輯。通過這些步驟,你可以從零開始構建和管理MySQL數據庫。

InnoDB緩衝池如何工作,為什麼對性能至關重要?InnoDB緩衝池如何工作,為什麼對性能至關重要?Apr 09, 2025 am 12:12 AM

InnoDBBufferPool通過將數據和索引頁加載到內存中來提升MySQL數據庫的性能。 1)數據頁加載到BufferPool中,減少磁盤I/O。 2)臟頁被標記並定期刷新到磁盤。 3)LRU算法管理數據頁淘汰。 4)預讀機制提前加載可能需要的數據頁。

MySQL:初學者的數據管理易用性MySQL:初學者的數據管理易用性Apr 09, 2025 am 12:07 AM

MySQL適合初學者使用,因為它安裝簡單、功能強大且易於管理數據。 1.安裝和配置簡單,適用於多種操作系統。 2.支持基本操作如創建數據庫和表、插入、查詢、更新和刪除數據。 3.提供高級功能如JOIN操作和子查詢。 4.可以通過索引、查詢優化和分錶分區來提升性能。 5.支持備份、恢復和安全措施,確保數據的安全和一致性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器