MySQL binary log (binlog) utilise analysis (2) - utilise la commande mysqlbinlog pour afficher les informations du journal

MySQL binary log (binlog) utilise analysis (2) - utilise la commande mysqlbinlog pour afficher les informations du journal

Par défaut, le journal binlog est un fichier binaire et ne peut pas être affiché directement. La fonction de la commande mysqlbinlog est de convertir les journaux de Mysql binlog en instructions Mysql. Le format de la commande mysqlbinlog est le suivant:

1. Fonctions de base

mysqlbinlog 日志文件名;

Description: affichez les informations du fichier journal directement sur l'écran.

Le résultat de l'exécution de la commande est le suivant:

[root@localhost wgx]# mysqlbinlog /var/lib/mysql/mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#200703 11:57:47 server id 2  end_log_pos 123 CRC32 0x4f25334b 	Start: binlog v 4, server v 5.7.27-log created 200703 11:57:47 at startup
ROLLBACK/*!*/;
BINLOG '
u6z+Xg8CAAAAdwAAAHsAAAAAAAQANS43LjI3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAC7rP5eEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AUszJU8=
'/*!*/;

.......

# at 692
#200703 12:05:38 server id 2  end_log_pos 692 CRC32 0x4b642e50 	Intvar
SET INSERT_ID=1/*!*/;
#200703 12:05:38 server id 2  end_log_pos 801 CRC32 0x8929f76f 	Query	thread_id=2	exec_time=0	error_code=0
SET TIMESTAMP=1593749138/*!*/;
insert into t2(name) values('Jack')
/*!*/;
# at 801
#200703 12:05:38 server id 2  end_log_pos 832 CRC32 0x6e8cf9f7 	Xid = 12
COMMIT/*!*/;
# at 832
#200703 12:06:15 server id 2  end_log_pos 879 CRC32 0x3497fb32 	Rotate to mysql-bin.000002  pos: 4
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

2. Exportez les informations du fichier journal dans un fichier texte

Utilisez le paramètre [-r] ou le symbole de redirection (>), le format est le suivant:

mysqlbinlog -r 日志文件名 文本文件名;
mysqlbinlog 日志文件名 > 文本文件名;

Par exemple:

[root@localhost wgx]# mysqlbinlog /var/lib/mysql/mysql-bin.000001 > /tmp/log_bak001.sql
[root@localhost wgx]# mysqlbinlog -r /var/lib/mysql/mysql-bin.000001 /tmp/log_bak002.sql
[root@localhost wgx]# ls /tmp/
log_bak001.sql  log_bak002.sql

3. Exporter le binlog de la base de données

Utilisez le paramètre [-d] pour exporter les informations du journal d'une certaine opération de base de données, le format est le suivant:

mysqlbinlog -d 数据库名称 日志文件名 > 文本文件名;

Par exemple, exportez les informations du journal des opérations relatives à la base de données [hist]:

[root@localhost wgx]# mysqlbinlog -d hist /var/lib/mysql/mysql-bin.000001 > /tmp/log_bak004.sql
[root@localhost wgx]# 

4. Sortie des informations du journal à l'emplacement spécifié

Utilisez les paramètres [–start-position] et [–stop-position] pour générer des informations de journal entre [–start-position] et [–stop-position].
Si l'option [–stop-position] est
omise , les informations du journal de [–start-position] à la fin seront affichées; si l'option [–start-position] est omise , le journal du début à la [–stop-position] sera affiché information;

Par exemple:
(1) Informations de journal de sortie entre 1315 et 1451:

[root@localhost wgx]# mysqlbinlog --start-position=1315 --stop-position=1451 /var/lib/mysql/mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#200703 22:27:21 server id 2  end_log_pos 123 CRC32 0x41949ba4 	Start: binlog v 4, server v 5.7.27-log created 200703 22:27:21 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
SUD/Xg8CAAAAdwAAAHsAAAABAAQANS43LjI3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAABJQP9eEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AaSblEE=
'/*!*/;
# at 1315
#200703 22:36:34 server id 2  end_log_pos 1451 CRC32 0x8fb104a1 	Query	thread_id=4	exec_time=0	error_code=0
use `wgx`/*!*/;
SET TIMESTAMP=1593786994/*!*/;
SET @@session.pseudo_thread_id=4/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549152/*!*/;
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.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create table t1(id int primary key auto_increment,name char(20))
/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

(2) Sortie des informations du journal du début du fichier à 154

[root@localhost wgx]# mysqlbinlog --stop-position=154 /var/lib/mysql/mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#200703 22:27:21 server id 2  end_log_pos 123 CRC32 0x41949ba4 	Start: binlog v 4, server v 5.7.27-log created 200703 22:27:21 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
SUD/Xg8CAAAAdwAAAHsAAAABAAQANS43LjI3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAABJQP9eEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AaSblEE=
'/*!*/;
# at 123
#200703 22:27:21 server id 2  end_log_pos 154 CRC32 0xa490108e 	Previous-GTIDs
# [empty]
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

(3) Informations de journal de sortie de 1741 à la fin du fichier

[root@localhost wgx]# mysqlbinlog --start-position=1741 /var/lib/mysql/mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#200703 22:27:21 server id 2  end_log_pos 123 CRC32 0x41949ba4 	Start: binlog v 4, server v 5.7.27-log created 200703 22:27:21 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
SUD/Xg8CAAAAdwAAAHsAAAABAAQANS43LjI3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAABJQP9eEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AaSblEE=
'/*!*/;
# at 1741
#200703 22:36:51 server id 2  end_log_pos 1772 CRC32 0x1d33aa48 	Xid = 44
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
5. Sortie des informations du journal dans une plage de temps spécifiée

Utilisez les paramètres [–start-datetime] et [–stop-datetime] pour générer les informations de journal entre [–start-datetime] et [–stop-datetime].
Si l'option [–stop-datetime] est
omise , les informations du journal de [–start-datetime] à la fin seront affichées; si l'option [–start-datetime] est omise , les informations du journal du début au [–stop-datetime] seront affichées information;

Les exemples sont omis.

6. Convertissez le binlog dont le format de journal est ROW en instruction pseudo SQL annotée

En utilisant le paramètre [-v], le binlog avec le format de journal ROW peut être converti en instructions pseudo SQL annotées.

Par exemple: affichez directement les informations du fichier journal au format ROW comme suit:

[root@Mysql11 ~]# mysqlbinlog --start-position=509 --stop-position=845 /var/lib/mysql/mysql-bin.000001

..........

# at 509
#200703 22:24:26 server id 1  end_log_pos 581 CRC32 0xa68d8aed 	Query	thread_id=2	exec_time=0	error_code=0
SET TIMESTAMP=1593786266/*!*/;
SET @@session.pseudo_thread_id=2/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549152/*!*/;
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.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 581
#200703 22:24:26 server id 1  end_log_pos 629 CRC32 0x220d2e02 	Table_map: `hist`.`t2` mapped to number 113
# at 629
#200703 22:24:26 server id 1  end_log_pos 845 CRC32 0x4cd0e798 	Update_rows: table id 113 flags: STMT_END_F

BINLOG '
mj//XhMBAAAAMAAAAHUCAAAAAHEAAAAAAAEABGhpc3QAAnQyAAID/gL+PAICLg0i
mj//Xh8BAAAA2AAAAE0DAAAAAHEAAAAAAAEAAgAC///8AQAAAAVKb2huefxlAAAABUpvaG55/AIA
AAADVG9t/GYAAAADVG9t/AMAAAAESmFja/xnAAAABEphY2v8BAAAAAVNZXJyefxoAAAABU1lcnJ5
/AUAAAAEUm9zZfxpAAAABFJvc2X8BgAAAAR3YW5n/GoAAAAEd2FuZ/wHAAAABXpoYW5n/GsAAAAF
emhhbmf8CAAAAAJsafxsAAAAAmxp/AkAAAAEemhhb/xtAAAABHpoYW+Y59BM
'/*!*/;
ROLLBACK /* added by mysqlbinlog */ /*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

Le fichier journal au format ROW écrit une transaction dans le journal sous forme binaire. Les informations du journal au format ROW sont incompréhensibles, même avec l'outil mysqlbinlog. Utilisez le paramètre [-v] pour convertir la description binaire de la transaction en une forme SQL similaire et l'afficher.
Utilisez le paramètre [-v] pour afficher les informations du journal:

[root@Mysql11 ~]# mysqlbinlog -v --start-position=509 --stop-postion=845 /var/lib/mysql/mysql-bin.000001
mysqlbinlog: [ERROR] unknown variable 'stop-postion=845'
[root@Mysql11 ~]# mysqlbinlog --base64-output='decode-rows' -v --start-position=509 --stop-position=845 /var/lib/mysql/mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 509
#200703 22:24:26 server id 1  end_log_pos 581 CRC32 0xa68d8aed 	Query	thread_id=2	exec_time=0	error_code=0
SET TIMESTAMP=1593786266/*!*/;
SET @@session.pseudo_thread_id=2/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549152/*!*/;
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.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 581
#200703 22:24:26 server id 1  end_log_pos 629 CRC32 0x220d2e02 	Table_map: `hist`.`t2` mapped to number 113
# at 629
#200703 22:24:26 server id 1  end_log_pos 845 CRC32 0x4cd0e798 	Update_rows: table id 113 flags: STMT_END_F
### UPDATE `hist`.`t2`
### WHERE
###   @1=1
###   @2='Johny'
### SET
###   @1=101
###   @2='Johny'
### UPDATE `hist`.`t2`
### WHERE
###   @1=2
###   @2='Tom'
### SET
###   @1=102
###   @2='Tom'
### UPDATE `hist`.`t2`
### WHERE
###   @1=3
###   @2='Jack'
### SET
###   @1=103
###   @2='Jack'
### UPDATE `hist`.`t2`
### WHERE
###   @1=4
###   @2='Merry'
### SET
###   @1=104
###   @2='Merry'
### UPDATE `hist`.`t2`
### WHERE
###   @1=5
###   @2='Rose'
### SET
###   @1=105
###   @2='Rose'
### UPDATE `hist`.`t2`
### WHERE
###   @1=6
###   @2='wang'
### SET
###   @1=106
###   @2='wang'
### UPDATE `hist`.`t2`
### WHERE
###   @1=7
###   @2='zhang'
### SET
###   @1=107
###   @2='zhang'
### UPDATE `hist`.`t2`
### WHERE
###   @1=8
###   @2='li'
### SET
###   @1=108
###   @2='li'
### UPDATE `hist`.`t2`
### WHERE
###   @1=9
###   @2='zhao'
### SET
###   @1=109
###   @2='zhao'
ROLLBACK /* added by mysqlbinlog */ /*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

Je suppose que tu aimes

Origine blog.csdn.net/weixin_44377973/article/details/107117185
conseillé
Classement