一、bin-log备份作用
 mysql的binlog是记录数据库数据操作行为的事务安全型的二进制日志。
 二、开启bin-log
 设置配置文件my.inf
 配置文件中加入以下内容
log-bin=/var/lib/mysql/mysql-bin  //开启并设置备份文件目录,以及备份命名格式
server-id=1  //集群中数据库服务器的唯一id,非集群环境可随意调写。mysql5.7以上版本需填写
 
其他可选配置项举例
binlog_format =mixed  //binlog文件格式,有row, statement, mixed
expire_logs_days = 10  //自动删除10天前的日志文件
max_binlog_size = 200M  //日志文件最大200M
binlog-ignore-db = test  //不记录数据库test的二进制日志。
binlog-do-db = test  //只记录数据库test的二进制日志。
 
三、查看binlog中的数据
 可先在数据库中创建student表并插入两条数据
- 查看binlog文件

 - 查看当前正在写入的binlog文件

 - 在mysql中查看binlog日志
a. 查看指定binlog文件日志 
show binlog events in 'mysql-bin.000032'
 

 b. 查看从指定位置开始,显示row_count条记录,通过offset控制结果中从哪个位置开始显示数据row_count条数据。
show binlog events [in 'log_name'] [FROM pos] [limit [offset,] row_count]
 

 
- 退出mysql,在命令行中查看日志。
a. 将binlog文件中特定数据库的日志以utf8格式输出为sql文件 
mysqlbinlog --database=test --set-charset=utf8 /www/server/data/mysql-bin.000032 > /leo/log.sql
 

 在导出的sql文件中可看到我们对数据库的操作内容。
 b. 查看从特定时间开始的日志
 –stop-datetime="xxxx-xx-xx xx:xx:xx"可指定截止时间。
mysqlbinlog --start-datetime="2022-02-17 14:22:27" /www/server/data/mysql-bin.000032
 

 c. 查看从指定位置开始的日志
 从上面sql中可以看到“at xxx”的内容,这就是位置节点,可根据这些节点选择日志开始位置。–stop-position="xxx"可指定结束位置。
mysqlbinlog --start-position="386" /www/server/data/mysql-bin.000032
 
四、从binlog中恢复数据
 场景1. 整体数据恢复
 现有数据库test,test中有表student,表里有两条数据
 
 将整个表删除,通过binlog恢复。
- 找到并查看相应binlog日志
 
mysql> show binlog events in 'mysql-bin.000032';
+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                                                                                                       |
+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------+
| mysql-bin.000032 |   4 | Format_desc |         1 |         107 | Server ver: 5.5.62-log, Binlog ver: 4                                                                                      |
| mysql-bin.000032 | 107 | Query       |         1 |         213 | CREATE DATABASE `test` CHARACTER SET 'utf8'                                                                                |
| mysql-bin.000032 | 213 | Query       |         1 |         386 | use `test`; CREATE TABLE `test`.`student`  (
  `id` int(0) NOT NULL,
  `name` varchar(15) NULL,
  PRIMARY KEY (`id`)
) |
| mysql-bin.000032 | 386 | Query       |         1 |         454 | BEGIN                                                                                                                      |
| mysql-bin.000032 | 454 | Query       |         1 |         577 | use `test`; INSERT INTO `test`.`student`(`id`, `name`) VALUES (1, 'Leo')                                                   |
| mysql-bin.000032 | 577 | Xid         |         1 |         604 | COMMIT /* xid=2879748 */                                                                                                   |
| mysql-bin.000032 | 604 | Query       |         1 |         672 | BEGIN                                                                                                                      |
| mysql-bin.000032 | 672 | Query       |         1 |         798 | use `test`; INSERT INTO `test`.`student`(`id`, `name`) VALUES (2, 'Sharry')                                                |
| mysql-bin.000032 | 798 | Xid         |         1 |         825 | COMMIT /* xid=2879751 */                                                                                                   |
| mysql-bin.000032 | 825 | Query       |         1 |         934 | use `test`; DROP TABLE `student` /* generated by server */                                                                 |
+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------+
 
经过分析节点从213开始到825之前的都是关于student表的操作,可直接恢复。运行以下命令:
mysqlbinlog /www/server/data/mysql-bin.000032 --start-position=213 --stop-position=825 | mysql -u root -p test
 
输入密码执行后,再查阅数据库,发现student表已恢复。
 
 建议执行恢复语句前在mysql中使用flush logs命令,该命令或暂停现在使用的binlog文件,使用新的binlog文件,这样后续操作就不会污染之前的日志。










