开启Binlog
启用binlog,通过配置 /etc/my.cnf
或 /etc/mysql/mysql.conf.d/mysqld.cnf
配置文件的 log-bin
选项 (my.cnf优先级参考这里),如下图所示:
启用Binlog后,重启mysql生效。
常用Binlog命令:
参开来源:https://laijianfeng.org/2019/03/MySQL-Binlog-介绍/
# 是否启用binlog日志
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
# mysql数据存储目录
mysql> show variables like '%dir%';
+-----------------------------------------+----------------------------+
| Variable_name | Value |
+-----------------------------------------+----------------------------+
| basedir | /usr/ |
| binlog_direct_non_transactional_updates | OFF |
| character_sets_dir | /usr/share/mysql/charsets/ |
| datadir | /var/lib/mysql/ |
| ignore_db_dirs | |
| innodb_data_home_dir | |
| innodb_log_group_home_dir | ./ |
| innodb_max_dirty_pages_pct | 75.000000 |
| innodb_max_dirty_pages_pct_lwm | 0.000000 |
| innodb_tmpdir | |
| innodb_undo_directory | ./ |
| lc_messages_dir | /usr/share/mysql/ |
| plugin_dir | /usr/lib64/mysql/plugin/ |
| slave_load_tmpdir | /tmp |
| tmpdir | /tmp |
+-----------------------------------------+----------------------------+
15 rows in set (0.01 sec)
# 查看binlog的目录
mysql> show global variables like "%log_bin%";
+---------------------------------+---------------------------------------+
| Variable_name | Value |
+---------------------------------+---------------------------------------+
| log_bin | ON |
| log_bin_basename | /var/lib/mysql/mysql_binary_log |
| log_bin_index | /var/lib/mysql/mysql_binary_log.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
+---------------------------------+---------------------------------------+
5 rows in set (0.00 sec)
# 查看当前服务器的biglog文件及大小
mysql> show binary logs;
+-------------------------+------------+
| Log_name | File_size |
+-------------------------+------------+
| mysql_binary_log.000001 | 177 |
| mysql_binary_log.000002 | 177 |
| mysql_binary_log.000003 | 2281160 |
| mysql_binary_log.000004 | 5466152 |
| mysql_binary_log.000005 | 2531902 |
| mysql_binary_log.000006 | 1148362 |
| mysql_binary_log.000007 | 1660323 |
| mysql_binary_log.000008 | 1777165 |
| mysql_binary_log.000009 | 377290 |
| mysql_binary_log.000010 | 643021 |
| mysql_binary_log.000011 | 1161346 |
| mysql_binary_log.000012 | 1180519 |
| mysql_binary_log.000013 | 1152434 |
| mysql_binary_log.000014 | 1160027 |
| mysql_binary_log.000015 | 1157951 |
| mysql_binary_log.000016 | 1073742019 |
| mysql_binary_log.000017 | 29559616 |
+-------------------------+------------+
17 rows in set (0.00 sec)
# 查看最新一个binlog日志文件名称和Position
mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| mysql_binary_log.000017 | 29566384 | | | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
# 查看具体一个binlog文件的内容
mysql> show binlog events in 'mysql_binary_log.000002';
+-------------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql_binary_log.000002 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.24-log, Binlog ver: 4 |
| mysql_binary_log.000002 | 123 | Previous_gtids | 1 | 154 | |
| mysql_binary_log.000002 | 154 | Stop | 1 | 177 | |
+-------------------------+-----+----------------+-----------+-------------+---------------------------------------+
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
红框中的信息解释如下:
- 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。