search
HomeDatabaseMysql TutorialMySQL decryption --> How to view the most original SQL statement in binary log ROW mode

MySQLbinlogROWPattern analysis

## After mysql5.6, the consistency of master-slave data The requirements have become higher, and the statement format is gradually less suitable for business needs, so everyone in the production environment adopts the row mode. The row mode is an insert module that transmits the lowest data changes to transmit master-slave data. Then in binlog What is the difference between it and the ordinary statement mode? Can you see the original sql statement?

1、Prepare to enter data

2RowMode binlog is garbled

Under Row mode, the ddl statement in the binlog is displayed normally, but The dml is garbled, as shown below:


mysql> create table test1(id int,c1 varchar(20),type int,address varchar(20),create_time datetime);
Query OK, 0 rows affected (0.00 sec)
 
mysql> insert into test1 select 1,'zhangsan','1','zhangsan road No 870,floor 602',now();
ERROR 1406 (22001): Data too long for column 'address' at row 1
mysql>
mysql> show create table test1;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                         |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test1 | CREATE TABLE `test1` (
  `id` int(11) DEFAULT NULL,
  `c1` varchar(20) DEFAULT NULL,
  `type` int(11) DEFAULT NULL,
  `address` varchar(20) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
 
mysql> alter table test1 modify  `address` varchar(200) DEFAULT NULL;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
mysql> insert into test1 select 1,'zhangsan','1','zhangsan road No 870,floor 602',now();
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
mysql> insert into test1 select 2,'lisi','1','zhangsan road No 870,floor 602',now();
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
mysql>



##[root@hch_test_dbm1_121_63 binlog]# /usr /local/mysql/bin/mysqlbinlog mysql-bin.000215

…… #The preceding ones are omitted here

#160722 17:02: 38 server id 62 end_log_pos 4291 CRC32 0x369e3244 Query thread_id=60 exec_time=4294967271 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 CRC32 0x8abc6e67 Query thread_id =60 exec_time=4294967271 error_code=0

use `percona`/*!*/;

SET TIMESTAMP=1469178158/*!*/;

CREATE TABLE IF NOT EXISTS `percona`.`checksums` (

db char(64) NOT NULL,

tbl char(64) NOT NULL,

chunk int NOT NULL,

chunk_time float NULL,

chunk_index varchar(200) NULL,

lower_boundary text NULL,

upper_boundary text This_crc NULL,

this_crc char( 40) NOT NULL,

this_cnt int NOT NULL,

master_crc 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:10 server id 63 end_log_pos 5280 CRC32 0xd147bd8e Query thread_id=16 exec_time=0 error_code=0

SET TIMESTAMP=1471659670/*!*/;

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

GRANT ALL PRIVILEGES ON *.* TO 'tim' @'192.168%' IDENTIFIED BY PASSWORD '*2976819BD2CCD13612E03F812A2CD297C1A18B23'

/*!*/;

# at 5280

#160820 10:22:40 server id 63 end_log_pos 5445 CRC32 0x85811be7 Query thread_id=18 exec_time=0 error_code=0

use `test`/*!*/;

SET TIMESTAMP=1471659760/*!*/;

create table test1(id int,c1 varchar(20),type int,address varchar(20),create_time datetime)

/*!*/;

# at 5445

#160820 10:24:34 server id 63 end_log_pos 5580 CRC32 0x2626220c Query thread_id=18 exec_time=0 error_code=0

SET TIMESTAMP=1471659874/*!*/;

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

/*!*/;

# at 5580

#160820 10:24:36 server id 63  end_log_pos 5660 CRC32 0x7b7c645f      Query       thread_id=18         exec_time=0    error_code=0

SET TIMESTAMP=1471659876/*!*/;

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

BEGIN

/*!*/;

# at 5660

# at 5764

#160820 10:24:36 server id 63  end_log_pos 5821 CRC32 0x08bc94c3      Table_map: `test`.`test1` mapped to  number 74

# at 5821

#160820 10:24:36 server id 63  end_log_pos 5911 CRC32 0x2f577f52       Write_rows: table id 74 flags:  STMT_END_F

 

BINLOG '

ZL+3VxM/AAAAOQAAAL0WAAAAAEoAAAAAAAEABHRlc3QABXRlc3QxAAUDDwMPEgU8AFgCAB/DlLwI

ZL+3Vx4/AAAAWgAAABcXAAAAAEoAAAAAAAEAAgAF/+ABAAAACHpoYW5nc2FuAQAAAB4Aemhhbmdz

YW4gcm9hZCBObyA4NzAsZmxvb3IgNjAymZoopiRSf1cv

'/*!*/;

# at 5911

#160820 10:24:36 server id 63  end_log_pos 5942 CRC32 0xb26af81b      Xid = 199

COMMIT/*!*/;

# at 5942

#160820 10:24:48 server id 63  end_log_pos 6022 CRC32 0x09eab31d     Query       thread_id=18         exec_time=0    error_code=0

SET TIMESTAMP=1471659888/*!*/;

BEGIN

/*!*/;

# at 6022

# at 6122

#160820 10:24:48 server id 63  end_log_pos 6179 CRC32 0xdc6dc34b      Table_map: `test`.`test1` mapped to  number 74

# at 6179

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

 

BINLOG '

cL+3VxM/AAAAOQAAACMYAAAAAEoAAAAAAAEABHRlc3QABXRlc3QxAAUDDwMPEgU8AFgCAB9Lw23c

cL+3Vx4/AAAAVgAAAHkYAAAAAEoAAAAAAAEAAgAF/+ACAAAABGxpc2kBAAAAHgB6aGFuZ3NhbiBy

b2FkIE5vIDg3MCxmbG9vciA2MDKZmiimMADXel8=

'/*!*/;

# at 6265

#160820 10:24:48 server id 63  end_log_pos 6296 CRC32 0xf6833d28      Xid = 200

COMMIT/*!*/;

# at 6296

#160820 10:31:30 server id 63  end_log_pos 6343 CRC32 0xcfcdd344       Rotate to mysql-bin.000216  pos: 4

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]

#

3, through --base64-output =decode-rows -v to view the dml statement

The execution command is: /usr/local/mysql/bin/mysqlbinlog --base64-output=decode-rows -v mysql-bin.000215

You can see the following effect, But they are all the original dml block sql statements:

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;[root@hch_test_dbm1_121_63 binlog]

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

/*!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 /*!*/;

……

#160820 10:24:36 server id 63 end_log_pos 5821 CRC32 0x08bc94c3 Table_map: `test`.`test1` mapped to number 74

# at 5821

#160820 10:24:36 server id 63 end_log_pos 5911 CRC32 0x2f577f52 Write_rows: table id 74 flags: STMT_END_F

INSERT INTO `test`.`test1`

SET

@1=1

@2='zhangsan'

## @3=1

@4='zhangsan road No 870,floor 602'

@5='2016-08-20 10:24:36'

# at 5911

#160820 10:24:36 server id 63 end_log_pos 5942 CRC32 0xb26af81b CRC32 0x09eab31d Query thread_id=18 exec_time=0 error_code=0

SET TIMESTAMP=1471659888/*!*/;

BEGIN

/*!*/;

# at 6022

# at 6122

#160820 10:24:48 server id 63 end_log_pos 6179 CRC32 0xdc6dc34b Table_map: `test`.`test1` mapped to number 74

# at 6179

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

## INSERT INTO ` test`.`test1`

SET

## @1=2

@2='lisi'

@3=1

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

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

# at 6265

#160820 10:24:48 server id 63 end_log_pos 6296 CRC32 0xf6833d28 Xid = 200

COMMIT/*!*/;

# at 6296

#160820 10:31:30 server id 63 end_log_pos 6343 CRC32 0xcfcdd344 Rotate to mysql-bin.000216 pos: 4

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

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

#############

PSYou can see it this way# The insert statement of ##dml, but only the final insert block is seen, but not the The original insert statement. In many cases, troubleshooting requires the original insert statement instead of the underlying insertblock. So the question is, how to see the original insert sql statement?

4、Passbinlog_rows_query_log_eventsParameters to view the original insert sql

4.1) First set the global binlog_rows_query_log_events parameters online and refresh the log:

mysql> set binlog_rows_query_log_events=1;                                                                        

#mysql> flush logs;

Query OK, 0 rows affected (0.01 sec)

mysql>exit

4.2

)Then log in again and enter the new one Data record:

##[root@hch_test_dbm1_121_63 ~]# mysql -utim -ptimgood -h192.168.121.63Warning: Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 19
Server version: 5.6.12-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

mysql> ; use test;

Database changed

mysql> insert into test1 select 3,'wanger','3','zhangsan road No 870,floor 603',now();

Query OK, 1 row affected (0.00 sec)

Records: 1 Duplicates: 0 Warnings: 0

mysql>

 

 

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、Test summary

Based on From the above experiments, it can be concluded that in row mode, you can view the bottom-level insert data module through mysqlbinlog --base64-output=decode-rows –v, or through the command Line show binlog events in 'mysql-bin.000216'; to view the original insertsql record in real time.

We can set it in advance in the startup parameter file my.cnf when building the database, as shown below:

# vim my.cnf

[mysqld]

binlog_format=row # binlog Log format

binlog_rows_query_log_events = 1 #Write the original operation sql record into the event          


## The above is MySQL decryption--> How to view the original binary log in ROW mode The content of the SQL statement, please pay attention to the PHP Chinese website (www.php.cn) for more related content!



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment