Binlog是binary log的缩写,这种以二进制文件形式存放的log,记录了mysql的数据变更,所有的DDL和DML语句都会以时间的形式予以记录,同时语句执行的消耗时间等也会被记录。它也是在数据丢失的时候,往往第一时间想到的部分。当你不小心drop database的时候,如果你有日别的定时备份和binlog,使用binlog恢复增量数据部分,往往问题并不会很大。
主要的使用场景有如下两种:
Master端打开binlog,需要进行主从复制时通过binlog文件的传递来实现
使用mysqlbinlog工具来进行数据的恢复,本文将使用简单示例进行说明。
binlog主要有如下三种模式:
使用如下命令可以确认binlog状态,可以看到当前状态为OFF
mysql> show variables like 'log_bin%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| log_bin | OFF |
| log_bin_basename | |
| log_bin_index | |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
+---------------------------------+-------+
5 rows in set (0.00 sec)
mysql>
此官方mysql镜像,设定文件可以直接修改/etc/mysql/mysql.conf.d/mysqld.cnf。
# echo "log_bin=ON" >>/etc/mysql/mysql.conf.d/mysqld.cnf
#
但是请注意,在mysql 5.7及其以上版本的话,如果开启binlog,做上述设定时,必须同时设定server-id,此server-id在MYSQL集群中需要指定不重复的信息,单机版本的MYSQL可以随意指定,但是如果没有指定,则可能会由于出现如下错误,导致mysql无法启动
[ERROR] You have enabled the binary log, but you haven't provided the mandatory server-id. Please refer to the proper server start-up parameters documentation
比如这里指定信息如下所示
server-id=1001
重启mysql的容器即可开启binlog,在此确认binlog状态如下所示,可以看到log_bin_basename和log_bin_index也同时被设定。
mysql> show variables like 'log_bin%';
+---------------------------------+-------------------------+
| Variable_name | Value |
+---------------------------------+-------------------------+
| log_bin | ON |
| log_bin_basename | /var/lib/mysql/ON |
| log_bin_index | /var/lib/mysql/ON.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
+---------------------------------+-------------------------+
5 rows in set (0.00 sec)
mysql>
可以通过show master logs来查询binlog的日志信息
mysql> show master logs;
+-----------+-----------+
| Log_name | File_size |
+-----------+-----------+
| ON.000001 | 154 |
+-----------+-----------+
1 row in set (0.00 sec)
mysql>
可以看到当前日志名称为ON.000001,此文件在/var/lib/mysql下,详细信息如下
# pwd
/var/lib/mysql
# ls ON.*
ON.000001 ON.index
#
这里可以看到有两类文件:
使用show master status可以查看master状态:
mysql> show master status;
+-----------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------+----------+--------------+------------------+-------------------+
| ON.000001 | 154 | | | |
+-----------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
此命令会显示最新的binlog日志的编号名称(File)以及Postion(最后一个操作事件的结束值)
使用flush logs可以进行日志的刷新,此时会产生一个新的编号的日志文件
mysql> flush logs;
Query OK, 0 rows affected (0.02 sec)
mysql>
结果信息确认如下
mysql> show master logs;
+-----------+-----------+
| Log_name | File_size |
+-----------+-----------+
| ON.000001 | 194 |
| ON.000002 | 154 |
+-----------+-----------+
2 rows in set (0.00 sec)
mysql> show master status;
+-----------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------+----------+--------------+------------------+-------------------+
| ON.000002 | 154 | | | |
+-----------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
文件信息如下所示
# pwd
/var/lib/mysql
# ls ON.*
ON.000001 ON.000002 ON.index
#
可以使用show binlog events来查询事件信息
mysql> show binlog events;
+-----------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-----------+-----+----------------+-----------+-------------+---------------------------------------+
| ON.000001 | 4 | Format_desc | 1001 | 123 | Server ver: 5.7.16-log, Binlog ver: 4 |
| ON.000001 | 123 | Previous_gtids | 1001 | 154 | |
| ON.000001 | 154 | Rotate | 1001 | 194 | ON.000002;pos=4 |
+-----------+-----+----------------+-----------+-------------+---------------------------------------+
3 rows in set (0.00 sec)
mysql>
使用自带的命令mysqlbinlog可以查看日志内容,同样也可使用show binlog events in 'ON.00002’¥G;命令进行查询。
# pwd
/var/lib/mysql
# ls ON.*
ON.000001 ON.000002 ON.index
# mysqlbinlog ON.000002
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#190307 14:51:58 server id 1001 end_log_pos 123 CRC32 0x805f2f4b Start: binlog v 4, server v 5.7.16-log created 190307 14:51:58
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
DjCBXA/pAwAAdwAAAHsAAAABAAQANS43LjE2LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AUsvX4A=
'/*!*/;
# at 123
#190307 14:51:58 server id 1001 end_log_pos 154 CRC32 0x0e5d49ca 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*/;
#
也可以使用如下命令:
mysql> show binlog events in 'ON.000002'\G;
*************************** 1. row ***************************
Log_name: ON.000002
Pos: 4
Event_type: Format_desc
Server_id: 1001
End_log_pos: 123
Info: Server ver: 5.7.16-log, Binlog ver: 4
*************************** 2. row ***************************
Log_name: ON.000002
Pos: 123
Event_type: Previous_gtids
Server_id: 1001
End_log_pos: 154
Info:
2 rows in set (0.00 sec)
ERROR:
No query specified
mysql>
这篇文章介绍了如何设定binlog以及常见的命令的使用方法,接下来介绍drop table或者drop table的恢复方法和示例。