>  기사  >  데이터 베이스  >  MySQL 암호 해독 --> 바이너리 로그 ROW 모드에서 가장 원본 SQL 문을 보는 방법

바이너리 로그 ROW 모드에서 가장 원본 SQL 문을 보는 방법

-->

MySQL 암호 해독 --> 바이너리 로그 ROW 모드에서 가장 원본 SQL 문을 보는 방법

黄舟
黄舟원래의
2017-02-16 11:43:252383검색

MySQLbinlogROW패턴 구문 분석

mysql5.6 이후 마스터-슬레이브 데이터 일관성 요구 사항이 높아지고 명령문 형식이 점차 비즈니스 요구에 적합하지 않아 프로덕션 환경의 모든 사람이 행 모드를 채택합니다. 행 모드는 마스터-슬레이브 데이터를 전송하기 위해 가장 낮은 데이터 변경 사항을 전송하는 삽입 모듈입니다. 일반 명령문 모드와 차이점은 무엇입니까? 원본 SQL 문을 볼 수 있나요?

1, 데이터 입력 준비


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>


 


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>


2, 모드 binlog가 깨졌습니다

Row 모드에서는 binlog의 ddl 문이 정상적으로 표시되지만 아래와 같이 dml이 깨졌습니다:

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

… :02: 38 서버 ID 62 end_log_pos 4291 CRC32 0x369e3244 쿼리 thread_id=60 exec_time=4294967271 error_code=0SET TIMESTAMP=1469178158/*!*/;

EX가 아닌 경우 데이터베이스 생성 ISTS `페르코나 ` /* pt-table-checksum */

/*!*/;

# at 4291

#160722 17:02:38 서버 ID 62 end_log_pos 5079 CRC32 0x8abc6e67 쿼리 thread_id =60 exec_time=4294967271 error_code=0

`percona`/*!*/ 사용;

SET TIMESTAMP=1469178158/*!*/;

CREATE TABLE IF NOT Exists `Percona`.`checksums` (

DB Char(64) Not Null,

TBL CHAR(64) Not null,

Chunk int null이 아님 ,

Chunk_time float NULL,

Chunk_index varchar(200) NULL,

lower_boundary text NULL,

upper_boundary text NULL,

this_crc char( 40) NOT NULL,

this_cnt int NOT NULL,

master_crc char(40) NULL,

master_cnt int NULL,

ts timestamp CURRENT_TIMESTAMP 업데이트 시 NULL이 아닌 기본 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/*!*/;

*에 모든 권한 부여.* TO 'tim' @'192.168%' 비밀번호로 식별됨 '*2976819BD2CCD13612E03F812A2CD297C1A18B23'

/*!*/;

# 5280

#160820 10:22:40 서버 ID 63 end_log_pos 5445 CRC32 0x85811be7 쿼리 thread_id=18 exec_time=0 error_code=0

`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 서버 ID 63 end_log_pos 5580 CRC32 0x2626220c 쿼리 thread_id=18 exec_time=0 error_code=0

SET TIMESTAMP=1471659874/* !*/;

alter table test1 수정 `address` varchar(200) DEFAULT NULL

/*!*/;

# at 5580

#160820 10:24:36 서버 ID 63  end_log_pos 5660 CRC32 0x7b7c645f      쿼리       thread_id=18         exec_time=0    error_code=0

SET TIMESTAMP=1471659876 /*!*/;

설정 @@session.time_zone='SYSTEM'/*!*/;

시작

/*!*/;

# 5660

# at 5764

#160820 10:24:36 server id 63  end_log_pos 5821 CRC32 0x08bc94c3      Table_map: `test`.`test1`이  번호 74에 매핑됨

# at 5821

#160820 10:24:36 서버 ID 63  end_log_pos 5911 CRC32 0x2f577f52       Write_rows: 테이블 ID 74 플래그:  STMT_END_F

 

BINLOG '

ZL+3Vx M/AAAAOQAAAL0WAAAAAAEoAAAAAAAEABHRlc3QABXRlc3QxAAUDDwMPEgU8AFgCAB/DlLwI

ZL+3Vx4/AAAAWgAAABcXAAAAAEoAAAAAAAEAAgAF/+ABAAAACHpoYW5nc2FuAQAAAB4Aemhhbmdz

YW4gcm9hZCBObyA4NzAsZmxvb3IgNjAymZoopiRSf1cv

'/*!*/;

# 5911

#160820 10:24:36 서버 ID 63  end_log_pos 5942 CRC32 0xb26af81b      Xid = 199

COMMIT/*!*/;

# at 5942

#160820 10:24:48 서버 ID 63  end_log_pos 6022 CRC32 0x09eab31d     쿼리       thread_id=18         exec_time=0    error_code=0

SET TIMESTAMP=1471659888/*!*/;

시작

/*!* /;

# 6022

# 6122

#160820 10:24:48 server id 63  end_log_pos 6179 CRC32 0xdc6dc34b      Table_map: `test`.`test1` 매핑됨 번호 74

# at 6179

#160820 10:24:48 server id 63  end_log_pos 6265 CRC32 0x5f7ad700      Write_rows: 테이블 ID 74 플래그: STMT_END_F

 

BINLOG '

cL+3VxM/AAAAOQAAACMYAAAAAEoAAAAAAAEABHRlc3QABXRlc3QxAAUDDwMPEgU8AFgCAB9Lw23c

cL+3Vx4/AAAAVgAAAAVgAAAHkYAAAAAAEoAAAAAAAEAAgAF/+ACAAAABG xpc2kBAAAAHgB6aGFuZ3NhbiBy

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       mysql-bin.000216으로 회전  pos: 4

DELIMITER ;

# 종료 로그 파일

ROLLBACK /* mysqlbinlog에 의해 추가됨 */;

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

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

[root@hch_test_dbm1_121_63 binlog]#

3, --base64-output =decode-rows -v dml

실행 명령을 보려면 : /usr/local/mysql/bin/mysqlbinlog --base64-output=decode-rows -v mysql-bin.000215

다음을 볼 수 있습니다. 효과가 있지만 모두 원본 dml 블록 sql 문입니다.

[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      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

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

/*!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 /*!*/;… 번호 74 # at 5821#160820 10:24:36 서버 ID 63 end_log_pos 5911 CRC32 0x2f577f52 Write_rows: 테이블 ID 74 플래그: STMT_END_F### `test`.`test1`에 삽입## # 설정 ### @1=1### @2='장산'### @3=1 ### @4='장산로 870번, 602층'### @5='2016-08-20 10:24:36'# 5911# 160820 10:24:36 서버 ID 63 end_log_pos 5942 CRC32 0xb26af81b : 48 서버 ID 63 end_log_pos 6022 CRC32 0x09eab31d 쿼리 thread_id=18 exec_time=0 error_code=0SET TIMESTAMP=1471 659888/*!*/; 시작/*! */;# 6022# 6122#160820 10:24:48 서버 ID 63 end_log_pos 6179 CRC32 0xdc6dc34b Table_map: `test`.`test1` 번호 74# at 6179#160820 10:24:48 서버 ID 63 end_log_pos 6265 CRC32 ad700 Write_rows : 테이블 ID 74 플래그: STMT_END_F### INSERT INTO `test`.`test1` ### 세트### @1 =2### @2='lisi'### @3 =1### @4=' 장산로 870호 602층### @5='2016-08-20 10:24:48 '# at 6265 #160820 10:24:48 server id 63 end_log_pos 6296 CRC32 0xf6833d28 Xid = 200COMMIT/*!*/;# at 6296#16082 0 10:31:30 server id 63 end_log_pos 6343 CRC32 0xcfcdd344 Rotate to mysql-bin.000216 pos: 4DELIMITER ; # 로그 파일 끝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]#

PS: 이렇게 보시면 됩니다 dmlinsert 문이 표시되지만 마지막 insert 블록만 표시되지만 표시되지는 않습니다. 최종 원본 insert 문 대부분의 경우 문제 해결에는 기본 insert 문이 필요합니다. > 블록을 삽입하세요. 그래서 질문은 원본 insert sql 문을 어떻게 보는가 입니다.

4, 합격binlog_rows_query_log_events 매개변수를 사용하여 원본 insert sql

( 4.1) 먼저 전역 binlog_rows_query_log_events 매개변수를 온라인으로 설정하고 로그를 새로 고칩니다.

mysql> set binlog_rows_query_log_events=1; sec)

mysql> set  binlog_rows_query_log_events=1;                                         

Query OK, 0 rows affected (0.01 sec)

 

mysql>

mysql> flush logs;

Query OK, 0 rows affected (0.01 sec)

 

mysql>exit

 

mysql>

mysql> 로그 플러시;쿼리 확인, 0개 행이 영향을 받음(0.01초) mysql> ;exit

[root@hch_test_dbm1_121_63 ~]# mysql  -utim -ptimgood -h192.168.121.63

Warning: 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.2) 그러면 새 데이터 레코드를 입력하려면 다시 로그인하세요.
[root @hch_test_dbm1_121_63 ~]# mysql -utim -ptimgood -h192.168.121.63경고: 명령줄 인터페이스에서 비밀번호를 사용하면 안전하지 않을 수 있습니다.MySQL 모니터에 오신 것을 환영합니다. ; 또는 g.MySQL 연결 ID는 19서버 버전: 5.6.12-log 소스 배포 저작권(c) 2000 , 2013, Oracle 및/또는 그 계열사. Oracle은 Oracle Corporation 및/또는계열사의 등록 상표입니다. 소유자의 상표입니다. 도움말을 보려면 'help;' 또는 'h'를 입력하여 현재 입력 문을 지우세요. mysql>mysql> test1에 삽입하여 3,'wanger','3','zhangsan road No 870,floor 603',now() ;쿼리 확인, 1개 행 영향(0.00초)기록: 1 중복: 0 경고: 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, 테스트 요약

기반 on 위의 실험을 통해 행 모드에서는 mysqlbinlog --base64-output=decode-rows –v를 통해 최하위 수준 삽입 데이터 모듈을 볼 수 있다는 결론을 내릴 수 있습니다. , 또는 명령줄 show binlog events in 'mysql-bin.000216';을 통해 원본 insertsql 기록을 실시간으로 볼 수 있습니다.

다음과 같이 데이터베이스 구축 시 시작 매개변수 파일 my.cnf에 미리 설정할 수 있습니다.

# vim my.cnf

[mysqld]

binlog_format=row  # binlog 日志格式

binlog_rows_query_log_events = 1  # 将原始的操作sql记录写入事件中              


# vim my.cnf

[mysqld]


binlog_format= row

# binlog


로그 형식

binlog_rows_query_log_events = 1 # 원래 연산 변환 sql 기록 작성 이벤트                                                                     위의 내용은 MySQL 복호화 --> 바이너리 로그 ROW 모드의 SQL 문에 대한 자세한 내용은 PHP를 참조하세요. 중국사이트(www.php.cn)!
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.