开启Binlog

启用binlog,通过配置 /etc/my.cnf/etc/mysql/mysql.conf.d/mysqld.cnf 配置文件的 log-bin 选项 (my.cnf优先级参考这里),如下图所示:

MySQL BinLog介绍 - 图1

启用Binlog后,重启mysql生效。

常用Binlog命令:

参开来源:https://laijianfeng.org/2019/03/MySQL-Binlog-介绍/

  1. # 是否启用binlog日志
  2. mysql> show variables like 'log_bin';
  3. +---------------+-------+
  4. | Variable_name | Value |
  5. +---------------+-------+
  6. | log_bin | ON |
  7. +---------------+-------+
  8. 1 row in set (0.00 sec)
  9. # mysql数据存储目录
  10. mysql> show variables like '%dir%';
  11. +-----------------------------------------+----------------------------+
  12. | Variable_name | Value |
  13. +-----------------------------------------+----------------------------+
  14. | basedir | /usr/ |
  15. | binlog_direct_non_transactional_updates | OFF |
  16. | character_sets_dir | /usr/share/mysql/charsets/ |
  17. | datadir | /var/lib/mysql/ |
  18. | ignore_db_dirs | |
  19. | innodb_data_home_dir | |
  20. | innodb_log_group_home_dir | ./ |
  21. | innodb_max_dirty_pages_pct | 75.000000 |
  22. | innodb_max_dirty_pages_pct_lwm | 0.000000 |
  23. | innodb_tmpdir | |
  24. | innodb_undo_directory | ./ |
  25. | lc_messages_dir | /usr/share/mysql/ |
  26. | plugin_dir | /usr/lib64/mysql/plugin/ |
  27. | slave_load_tmpdir | /tmp |
  28. | tmpdir | /tmp |
  29. +-----------------------------------------+----------------------------+
  30. 15 rows in set (0.01 sec)
  31. # 查看binlog的目录
  32. mysql> show global variables like "%log_bin%";
  33. +---------------------------------+---------------------------------------+
  34. | Variable_name | Value |
  35. +---------------------------------+---------------------------------------+
  36. | log_bin | ON |
  37. | log_bin_basename | /var/lib/mysql/mysql_binary_log |
  38. | log_bin_index | /var/lib/mysql/mysql_binary_log.index |
  39. | log_bin_trust_function_creators | OFF |
  40. | log_bin_use_v1_row_events | OFF |
  41. +---------------------------------+---------------------------------------+
  42. 5 rows in set (0.00 sec)
  43. # 查看当前服务器的biglog文件及大小
  44. mysql> show binary logs;
  45. +-------------------------+------------+
  46. | Log_name | File_size |
  47. +-------------------------+------------+
  48. | mysql_binary_log.000001 | 177 |
  49. | mysql_binary_log.000002 | 177 |
  50. | mysql_binary_log.000003 | 2281160 |
  51. | mysql_binary_log.000004 | 5466152 |
  52. | mysql_binary_log.000005 | 2531902 |
  53. | mysql_binary_log.000006 | 1148362 |
  54. | mysql_binary_log.000007 | 1660323 |
  55. | mysql_binary_log.000008 | 1777165 |
  56. | mysql_binary_log.000009 | 377290 |
  57. | mysql_binary_log.000010 | 643021 |
  58. | mysql_binary_log.000011 | 1161346 |
  59. | mysql_binary_log.000012 | 1180519 |
  60. | mysql_binary_log.000013 | 1152434 |
  61. | mysql_binary_log.000014 | 1160027 |
  62. | mysql_binary_log.000015 | 1157951 |
  63. | mysql_binary_log.000016 | 1073742019 |
  64. | mysql_binary_log.000017 | 29559616 |
  65. +-------------------------+------------+
  66. 17 rows in set (0.00 sec)
  67. # 查看最新一个binlog日志文件名称和Position
  68. mysql> show master status;
  69. +-------------------------+----------+--------------+------------------+-------------------+
  70. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  71. +-------------------------+----------+--------------+------------------+-------------------+
  72. | mysql_binary_log.000017 | 29566384 | | | |
  73. +-------------------------+----------+--------------+------------------+-------------------+
  74. 1 row in set (0.00 sec)
  75. # 查看具体一个binlog文件的内容
  76. mysql> show binlog events in 'mysql_binary_log.000002';
  77. +-------------------------+-----+----------------+-----------+-------------+---------------------------------------+
  78. | Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
  79. +-------------------------+-----+----------------+-----------+-------------+---------------------------------------+
  80. | mysql_binary_log.000002 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.24-log, Binlog ver: 4 |
  81. | mysql_binary_log.000002 | 123 | Previous_gtids | 1 | 154 | |
  82. | mysql_binary_log.000002 | 154 | Stop | 1 | 177 | |
  83. +-------------------------+-----+----------------+-----------+-------------+---------------------------------------+
  84. 3 rows in set (0.13 sec)

Binlog日志格式

记录在二进制日志中的事件的格式取决于二进制记录格式。支持三种格式类型:

  • STATEMENT:基于SQL语句的复制(statement-based replication, SBR)
  • ROW:基于行的复制(row-based replication, RBR)
  • MIXED:混合模式复制(mixed-based replication, MBR)

在MySQL 5.7.22中,默认的binlog日志格式是mixed:

binlog_format = mixed

三种格式的优缺点介绍,请看https://laijianfeng.org/2019/03/MySQL-Binlog-%E4%BB%8B%E7%BB%8D/描述。

mysqlbinlog命令的使用

mysqlbinlog -v —base64-output=decode-rows mysql_binary_log.000009 | less

MySQL BinLog介绍 - 图2

红框中的信息解释如下:

  • position: 位于文件中的位置,即第一行的(# at 367),说明该事件记录从文件第367个字节开始
  • timestamp: 事件发生的时间戳,即第二行的(#200415 3:06:29)
  • server id: 服务器标识(1)
  • end_log_pos 表示下一个事件开始的位置(即当前事件的结束位置+1)
  • thread_id: 执行该事件的线程id (thread_id=113)
  • exec_time: 事件执行的花费时间
  • error_code: 错误码,0意味着没有发生错误
  • type:事件类型Query

(红框中没有thread_id exec_time error_code,但是其他记录中有)

Binlog事件类型

事件类型 说明
UPDATE_ROWS_EVENT 用在binlog_format为ROW模式下,对应 update 操作
DELETE_ROWS_EVENT 用在binlog_format为ROW模式下,对应 delete 操作
WRITE_ROWS_EVENT 用在binlog_format为ROW模式下,对应 insert 操作
QUERY_EVENT 执行更新语句时会生成此事件,包括:create,insert,update,delete;
START_EVENT_V3 每个binlog文件开始的时候写入的事件,此事件被用在MySQL3.23 – 4.1,MYSQL5.0以后已经被 FORMAT_DESCRIPTION_EVENT 取代
STOP_EVENT 当mysqld停止时生成此事件

上表列出的是常见事件。其他事件请google。