数据库
首页 > 数据库> > Mysql 系列 | binlog 详解

Mysql 系列 | binlog 详解

作者:互联网

binlog 会记录表结构变更和表数据变更,有了 binlog 和 redolog,可以还原任意时刻的数据库状态。

binlog 二进制文件通常用来数据恢复主从复制审计

本篇 Mysql 版本为 8.0

归档日志(binlog)详解

binlog 的写入机制

binlog 基础参数

mysql> show variables like "%log_bin%";
+---------------------------------+---------------------------------------+
| Variable_name                   | Value                                 |
+---------------------------------+---------------------------------------+
| log_bin                         | ON                                    |
| log_bin_basename                | /usr/local/mysql/data/mysql-bin       |
| log_bin_index                   | /usr/local/mysql/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF                                   |
| log_bin_use_v1_row_events       | OFF                                   |
| sql_log_bin                     | ON                                    |
+---------------------------------+---------------------------------------+
6 rows in set (0.00 sec)

相关配置

mysql> show variables like "%binlog%";
+--------------------------------------------+----------------------+
| Variable_name                              | Value                |
+--------------------------------------------+----------------------+
| binlog_cache_size                          | 32768                |
| binlog_checksum                            | CRC32                |
| binlog_direct_non_transactional_updates    | OFF                  |
| binlog_error_action                        | ABORT_SERVER         |
| binlog_expire_logs_seconds                 | 2592000              |
| binlog_format                              | MIXED                |
| binlog_group_commit_sync_delay             | 0                    |
| binlog_group_commit_sync_no_delay_count    | 0                    |
| binlog_gtid_simple_recovery                | ON                   |
| binlog_max_flush_queue_time                | 0                    |
| binlog_order_commits                       | ON                   |
| binlog_row_image                           | FULL                 |
| binlog_row_metadata                        | MINIMAL              |
| binlog_row_value_options                   |                      |
| binlog_rows_query_log_events               | OFF                  |
| binlog_stmt_cache_size                     | 32768                |
| binlog_transaction_dependency_history_size | 25000                |
| binlog_transaction_dependency_tracking     | COMMIT_ORDER         |
| innodb_api_enable_binlog                   | OFF                  |
| log_statements_unsafe_for_binlog           | ON                   |
| max_binlog_cache_size                      | 18446744073709547520 |
| max_binlog_size                            | 1073741824           |
| max_binlog_stmt_cache_size                 | 18446744073709547520 |
| sync_binlog                                | 1                    |
+--------------------------------------------+----------------------+
24 rows in set (0.00 sec)

SQL 命令查看 binlog

mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000083 |   8667374 |
| mysql-bin.000084 |    122684 |
| mysql-bin.000085 |     32099 |
| mysql-bin.000086 | 794208651 |
| mysql-bin.000087 |  39070717 |
+------------------+-----------+
5 rows in set (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000087 | 39120223 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> show binlog events in 'mysql-bin.000087' FROM 12542 limit 0,3 \G;
*************************** 1. row ***************************
   Log_name: mysql-bin.000087
        Pos: 12542
 Event_type: Query
  Server_id: 1
End_log_pos: 12666
       Info: BEGIN
*************************** 2. row ***************************
   Log_name: mysql-bin.000087
        Pos: 12666
 Event_type: Query
  Server_id: 1
End_log_pos: 12988
       Info: use `[数据库名]`; INSERT INTO xxxxxx (xxxxxxxxxxxxxxxx) VALUES (xxxxxxxxxxxxxx)
*************************** 3. row ***************************
   Log_name: mysql-bin.000087
        Pos: 12988
 Event_type: Xid
  Server_id: 1
End_log_pos: 13019
       Info: COMMIT /* xid=541 */
3 rows in set (0.00 sec)

用 mysqlbinlog 查看二进制 binlog 文件

mysqlbinlog [binlog 文件名]

常用参数

文件内容

和上面 SQL 查得同样的 3 个 event,binlog 文件内容如下。

# mysqlbinlog mysql-bin.000087 --start-position=12542 --stop-position=13019
.
.
.
# at 12542
#220714 14:11:35 server id 1  end_log_pos 12666 CRC32 0xca59c21a        Query   thread_id=65    exec_time=0     error_code=0
SET TIMESTAMP=1657775495/*!*/;
SET @@session.pseudo_thread_id=65/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1168113664/*!*/;
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/*!*/;
/*!80005 SET @@session.default_collation_for_utf8mb4=255*//*!*/;
BEGIN
/*!*/;
# at 12666
#220714 14:11:35 server id 1  end_log_pos 12988 CRC32 0xbe5015be        Query   thread_id=65    exec_time=0     error_code=0
use `[数据库名]`/*!*/;
SET TIMESTAMP=1657775495/*!*/;
INSERT INTO xxxxxx (xxxxxxxxxxxx) VALUES (xxxxxxxxxxxx)
/*!*/;
# at 12988
#220714 14:11:35 server id 1  end_log_pos 13019 CRC32 0x81af69ed        Xid = 541
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*/;

binlog 恢复数据

把查到的 binlog 的内容给到 mysql 去执行。(-v 表示执行 sql 语句

# mysqlbinlog mysql-bin.000087 --databse=[database name] --start-position=12542 --stop-position=13019 | mysql -uroot -pxxxx -v [database name]

看懂 binlog 内容,可以更方便地还原数据等操作。

标签:session,binlog,SET,log,Mysql,详解,mysql,id
来源: https://www.cnblogs.com/rendd/p/16501525.html