binlog 就是binary log,二进制日志文件,这个文件记录了MySQL所有的DML操作。通过binlog日志我们可以做数据恢复,增量备份,主主复制和主从复制等等。对于开发者可能对binlog并不怎么关注,但是对于运维或者架构人员来讲是非常重要的。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
每当我们重启一次,会自动生成一个binlog文件,当然,我们也可以手动的来刷新binlog文件,通过 flush logs,同样会新创建一个binlog文件。实际上当服务器在重启时,也会调用flush logs操作。如果我们想把这些文件全部清空,可以使用reset master 来处理
mysql> reset master;
Query OK, 0 rows affected (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
注意到上面的截图中有一个position字段,这个字段类似于一个指针,目前这个字段的值为154,表示的就是binlog当前的位置。我们每次执行dml或者DDL操作,position都会改变。比如我们先来插入一行数据
mysql> insert into employee1 values(234,'jjj');
Query OK, 1 row affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 486 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
然后我们来查看创建之后的状态,如下,我们发现position从154变成了486,也就是说我们的操作是在154到486之间,然后我们再来看binlog的内容。
# at 154
#230821 16:09:57 server id 123454 end_log_pos 219 CRC32 0xb2dd0c17 Anonymous_GTID last_committed=0 sequence_number=1 rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 219
#230821 16:09:57 server id 123454 end_log_pos 292 CRC32 0x92dc4448 Query thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1692605397/*!*/;
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=45/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 292
# at 355
#230821 16:09:57 server id 123454 end_log_pos 411 CRC32 0x89997514 Table_map: `mysql`.`employee1` mapped to number 112
# at 411
#230821 16:09:57 server id 123454 end_log_pos 455 CRC32 0xf129de67 Write_rows: table id 112 flags: STMT_END_F
BINLOG '
1RvjZBM+4gEAOAAAAJsBAAAAAHAAAAAAAAEABW15c3FsAAllbXBsb3llZTEAAgMPAigAAhR1mYk=
1RvjZB4+4gEALAAAAMcBAAAAAHAAAAAAAAEAAgAC//zqAAAAA2pqamfeKfE=
'/*!*/;
# at 455
#230821 16:09:57 server id 123454 end_log_pos 486 CRC32 0x1e0d9ce6 Xid = 55
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*/;
我们可以看到,mysql做了很多的隐含的操作,细心的朋友会看到有这个一句:
#230821 16:09:57 server id 123454 end_log_pos 411 CRC32 0x89997514 Table_map: `mysql`.`employee1` mapped to number 112
# at 411
#230821 16:09:57 server id 123454 end_log_pos 455 CRC32 0xf129de67 Write_rows: table id 112 flags: STMT_END_F
mysql> select * from employee1;
+-----+---------+
| id | name |
+-----+---------+
| 2 | wusan |
| 12 | saf |
| 30 | chuang1 |
| 40 | sss |
| 44 | sss |
| 234 | jjj |
+-----+---------+
6 rows in set (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 486 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> select * from employee1;
+-----+---------+
| id | name |
+-----+---------+
| 2 | wusan |
| 12 | saf |
| 30 | chuang1 |
| 40 | sss |
| 44 | sss |
| 234 | jjj |
+-----+---------+
6 rows in set (0.00 sec)
mysql> update employee1 set name='jxjxjx';
Query OK, 6 rows affected (0.00 sec)
Rows matched: 6 Changed: 6 Warnings: 0
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 937 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
# at 486
#230821 16:19:45 server id 123454 end_log_pos 551 CRC32 0xd108afc6 Anonymous_GTID last_committed=1 sequence_number=2 rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 551
#230821 16:19:45 server id 123454 end_log_pos 624 CRC32 0xc14e28af Query thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1692605985/*!*/;
BEGIN
/*!*/;
# at 624
# at 682
#230821 16:19:45 server id 123454 end_log_pos 738 CRC32 0x7e844719 Table_map: `mysql`.`employee1` mapped to number 112
# at 738
#230821 16:19:45 server id 123454 end_log_pos 906 CRC32 0xc70edc20 Update_rows: table id 112 flags: STMT_END_F
BINLOG '
IR7jZBM+4gEAOAAAAOICAAAAAHAAAAAAAAEABW15c3FsAAllbXBsb3llZTEAAgMPAigAAhlHhH4=
IR7jZB8+4gEAqAAAAIoDAAAAAHAAAAAAAAEAAgAC///8AgAAAAV3dXNhbvwCAAAABmp4anhqePwM
AAAAA3NhZvwMAAAABmp4anhqePweAAAAB2NodWFuZzH8HgAAAAZqeGp4anj8KAAAAANzc3P8KAAA
AAZqeGp4anj8LAAAAANzc3P8LAAAAAZqeGp4anj86gAAAANqamr86gAAAAZqeGp4angg3A7H
'/*!*/;
# at 906
#230821 16:19:45 server id 123454 end_log_pos 937 CRC32 0x04f32c0c Xid = 62
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
可以看到
#230821 16:19:45 server id 123454 end_log_pos 738 CRC32 0x7e844719 Table_map: `mysql`.`employee1` mapped to number 112
# at 738
#230821 16:19:45 server id 123454 end_log_pos 906 CRC32 0xc70edc20 Update_rows: table id 112 flags: STMT_END_F
下面我们来简单总结一下关于binlog:
1.binlog文件会随服务的启动创建一个新文件
2.通过flush logs 可以手动刷新日志,生成一个新的binlog文件
3.通过show master status 可以查看binlog的状态
4.通过reset master 可以清空binlog日志文件
5.通过mysqlbinlog 工具可以查看binlog日志的内容
6.通过执行dml,mysql会自动记录binlog