Home >Database >Mysql Tutorial >Teach you step by step how to play with MySQL binlog

Teach you step by step how to play with MySQL binlog

WBOY
WBOYforward
2022-01-12 17:35:542462browse

This article brings you knowledge about MySQL database deletion and binlog. The more important logs in MySQL include binlog, redo log and undo log. So the main thing related to our article is binlog. I hope it will be helpful to everyone. help.

Teach you step by step how to play with MySQL binlog

The more important logs in MySQL include binlog (archive log), redo log (redo log) and undo log, so the main ones related to our article are binlog.

1. binlog

binlog We generally call it archive log in Chinese. If you have seen the MySQL master-slave setup that Song Ge posted before, You should have an impression of this log. When we build the MySQL master-slave, we cannot do without the binlog.

Binlog is the log of the MySQL Server layer, not the log that comes with the storage engine. It records all DDL and DML (excluding data query statements) statements, and is recorded in the form of events, including statements. The execution time, etc., it should be noted that:

  • binlog is a kind of logical log. What is recorded in it is the original logic of a SQL statement, for example, for a certain field 1. Pay attention to the physical log that is different from the redo log (what modifications were made on a certain data page).

  • When the binlog file is full, it will automatically switch to the next log file to continue writing without overwriting the previous log. This is also different from redo log, which is written in a loop. , that is, what is written later may overwrite what is written earlier.

  • Generally speaking, when we configure binlog, we can specify the validity period of the binlog file, so that after expiration, the log file will be automatically deleted, thus avoiding taking up more storage space.

According to the official MySQL documentation, after turning on binlog, there will be about 1% performance loss, but this is still acceptable. Generally speaking, binlog has two important uses. Scenario:

  • MySQL master-slave replication: open binlog on the host, the host synchronizes the binlog to the slave, and the slave synchronizes data through the binlog, thereby realizing the data between the host and the slave Synchronize.

  • MySQL data recovery, by using the mysqlbinlog tool combined with the binlog file, the data can be restored to a certain moment in the past.

2. Open binlog

For the convenience of demonstration, Brother Song installed MySQL in Docker. We use Let's start today's demonstration with this example. If your friends still don’t know how to use docker, you can reply to docker in the background of the official account. There is a tutorial written by Brother Song.

First we install MySQL in docker, and then enter the container. You can check whether binlog is turned on by running the following command:

Teach you step by step how to play with MySQL binlog

##This OFF means binlog It is in a closed state and not turned on. Next we turn on binlog.

Opening binlog mainly involves modifying the MySQL configuration file mysqld.cnf, which is located in the /etc/mysql/mysql.conf.d directory of the container.

Teach you step by step how to play with MySQL binlog

For this configuration file, we have made the following modifications:

# 这个参数表示启用 binlog 功能,并指定 binlog 的存储目录
log-bin=javaboy_logbin
# 设置一个 binlog 文件的最大字节
# 设置最大 100MB
max_binlog_size=104857600
# 设置了 binlog 文件的有效期(单位:天)
expire_logs_days = 7
# binlog 日志只记录指定库的更新(配置主从复制的时候会用到)
#binlog-do-db=javaboy_db
# binlog 日志不记录指定库的更新(配置主从复制的时候会用到)
#binlog-ignore-db=javaboy_no_db
# 写缓存多少次,刷一次磁盘,默认 0 表示这个操作由操作系统根据自身负载自行决定多久写一次磁盘
# 1 表示每一条事务提交都会立即写磁盘,n 则表示 n 个事务提交才会写磁盘
sync_binlog=0
# 为当前服务取一个唯一的 id(MySQL5.7 之后需要配置)
server-id=1

The meaning of each configuration has been explained in the gaze. The screenshot is as follows:

Teach you step by step how to play with MySQL binlog

After the configuration is completed, execute the following command to restart the mysql container (mysql1 is the name of my container):

docker restart mysql1

After restarting, execute again show variables like 'log_bin%'; you can see that binlog has been turned on.

Teach you step by step how to play with MySQL binlog

In addition to the log_bin variable, there are two variable names worthy of our attention:

  • log_bin_basename: This will be generated in the future The name prefix of the binlog log file. In other words, according to the configuration you have seen so far, the binlog log file generated in the future will be named javaboy_logbin.xxx. This file will be used to record all DDL and DML statement events.

  • log_bin_index: This is the binlog index file, which saves the directories of all binlogs, because there may be multiple binlogs. We can take a look at the current javaboy_logbin.index file:

Teach you step by step how to play with MySQL binlog

As you can see, there is currently only one logbin file.

3. Common binlog operations

Next, let’s introduce some common binlog operation commands.

View all binlog logs

We can view the binlog log list through the following methods:

show master logs;

Teach you step by step how to play with MySQL binlog

可以看到,我这里目前只有一个日志文件,文件名为 javaboy_logbin.000001,File_size 表示这个文件占用的字节大小是 154。

查看 master 状态

这个命令我们在搭建 MySQL 主从的时候经常会用到,如下:

Teach you step by step how to play with MySQL binlog

这个时候可以看到最新的 binlog 日志文件名称以及最后一个操作事件的 Position 值(这个值有啥用,我们后面会给大家详细介绍)。

刷新 binlog

正常来说,一个 binlog 写满之后,会自动切换到下一个 binlog 开始写,不过我们也可以执行一个 flush logs 命令来手动刷新 binlog,手动刷新 binlog 之后,就会产生一个新的 binlog 日志文件,接下来所有的 binlog 日志都将记录到新的文件中。如下:

Teach you step by step how to play with MySQL binlog

由上图可以看到,我们刷新日志之后,再通过 show master logs 去查看日志,发现日志文件已经多了一个新产生的了,然后再通过 show master status 去查看最新的日志文件信息,发现也已经变为 javaboy_logbin.000002。

重置 binlog

reset master 可以重置 binlog 日志文件,让日志重新从 000001 开始记录,不过如果当前主机有一个或者多个从机在运行,那么该命令就运行不了(因为从机是通过 binlog 来实现数据库同步的,主机把 binlog 清空了,从机会报找不到 binlog 的错误)。

Teach you step by step how to play with MySQL binlog

查看 binlog

由于 binlog 是二进制日志文件,所以要是直接打开,那肯定是看不了的:

Teach you step by step how to play with MySQL binlog

没有看到任何有用的信息。

为了查看 binlog,MySQL 为我们提供了两个官方工具,我们一个一个来看,首先是 mysqlbinlog 命令,如下:

Teach you step by step how to play with MySQL binlog

虽然看起来乱糟糟的,不过仔细看着其实都有迹可循。因为我这里是一个新安装的数据库,里边只是创建了一个名为 javaboy 的库,然后创建了一个名为 user 的表加了两条数据,其他什么事情都没做,所以创建库的脚本我们其实能够从纷杂的文件中找到。

产生的日志文件中有一个 end_log_pos 是日志文件的 pos 点,这个将来在数据恢复的时候有用。

不过这种查看方式不够人性化,我们说 binlog 是按照事件来记录日志的,所以如果我们能够按照事件的方式查看日志,就会好很多,我们再来看看如下一个命令:

show binlog events [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count];

这个表示以事件的方式来查看 binlog,这里涉及到几个参数:

  • log_name:可以指定要查看的 binlog 日志文件名,如果不指定的话,表示查看最早的 binlog 文件。

  • pos:从哪个 pos 点开始查看,凡是 binlog 记录下来的操作都有一个 pos 点,这个其实就是相当于我们可以指定从哪个操作开始查看日志,如果不指定的话,就是从该 binlog 的开头开始查看。

  • offset:这是是偏移量,不指定默认就是 0。

  • row_count:查看多少行记录,不指定就是查看所有。

我们来看一个简单的例子:

show binlog events in 'javaboy_logbin.000001';

Teach you step by step how to play with MySQL binlog

这下就清晰多了,我们可以看到之前的所有操作,例如:

  • 在 Pos 219-322 之间创建了一个库。

  • 在 Pos 387-537 之间创建了一张表。

  • 在 Pos 677-780 之间添加了一条记录。

4. 数据恢复实战

好啦,有了前面的基础知识准备,接下来松哥来给大家手把手演示一个删库/恢复的场景。

我先来说说我这个数据库目前的情况。

这是一个新安装的数据库,里边我新建了一个数据库名为 javaboy,javaboy 库中新建了一张表名为 user,user 中有两条记录,如下:

Teach you step by step how to play with MySQL binlog

现在假设我们定期(每周三凌晨三点)对数据库进行备份。

现在凌晨三点了,数据库自动备份开始了,我们通过如下命令将数据库备份成 SQL 脚本,如下:

mysqldump -uroot -p --flush-logs --lock-tables -B javaboy>/root/javaboy.bak.sql

Teach you step by step how to play with MySQL binlog

这里有几个参数跟大家解释下:

  • -u、-p 这两个就不用说了。

  • --flush-logs:这个表示在导出之前先刷新 binlog,刷新 binlog 之后将会产生新的 binlog 文件,后续的操作都存在新的 binlog 中。

  • --lock-tables:这个表示开始导出前,锁定所有表。需要注意的是当导出多个数据库时,--lock-tables 分别为每个数据库锁定表,因此这个选项不能保证导出文件中的表在数据库之间的逻辑一致性,不同数据库表的导出状态可以完全不同。

  • -B:这个表示指定导出的数据库名称,如果使用 --all-databases 或者 -A 代替 -B 表示导出所有的数据库。

以上命令执行完成后,会在 /root 目录下生成一个 javaboy.bak.sql 文件,该文件就是备份的 sql 文件了。

这是星期三凌晨三点发生的事情。

接下来到了星期四早上,来上班了,一顿操作后,往数据库中又添加了两条操作,如下:

Teach you step by step how to play with MySQL binlog

接下来,小 X 今天跟领导吵架了很不爽,决定删除跑路:

Teach you step by step how to play with MySQL binlog

领导发现了大惊,当即要求立马恢复数据。这时候该你表现了。

首先,我们有星期三凌晨的备份文件,先用那个文件进行数据恢复:

Teach you step by step how to play with MySQL binlog

恢复之后,现在到星期三早上凌晨三点的数据有了。

从星期三早上凌晨三点到星期四的数据现在没了。

这个时候我们就要借助于 binlog 来恢复了。大家还记得,我们星期三凌晨三点执行备份的时候,用了一个参数叫做 --flush-logs,使用了该参数表示从备份那一刻起,新的 binlog 将产生在一个新的日志文件中,对于我们这里来说,新的 binlog 文件当然就是 javaboy_logbin.000002 了,我们去查看一下该文件:

show binlog events in 'javaboy_logbin.000002';

我这里生成的该文件比较长,我截取其中一部分:

Teach you step by step how to play with MySQL binlog

可以看到,在 764-865 这个 Pos 中发生了删库跑路事件,那么我们只需要回放该文件将数据恢复到 764 这个位置即可。

由于 javaboy_logbin.000002 文件是在星期三凌晨三点备份之后产生的新文件,因此这个文件从起始到 764 这个 Pos 之间的操作,就是星期三凌晨三点到删库之前的操作了。

那么我们来看下通过 binlog 来恢复数据的命令:

mysqlbinlog /var/lib/mysql/javaboy_logbin.000002 --stop-position=764 --database=javaboy | mysql -uroot -p

那么这里涉及到两个参数:

  • --stop-position=764 表示恢复到 764 这个 Pos,不指定的话就把按整个文件恢复了,如果按当前文件恢复的话,由于这个 binlog 文件中有删除数据库的语句,那么就会导致执行完该 binlog 之后,javaboy 库又被删除了。

  • --database=javaboy 表示恢复 javaboy 这个库。

另外还有一个我们这里没用到的参数叫做 --start-position,这个表示起始的 Pos,不指定的话表示从头开始数据恢复。

好啦,弄完之后,再来查看数据库:

Teach you step by step how to play with MySQL binlog

数据恢复啦~

注意:所有操作之前,记得该备份就备份(防止你操作错了又回不去),松哥为了省事上面省略了一些备份操作。

5. 小结

好啦,今天这篇文章主要是和小伙伴们分享了 MySQL 的 binlog 日志,并通过一个小案例来演示如何通过 binlog 实现数据库的删库恢复。好啦,感兴趣的小伙伴可以试试哦(别在生产库上试哦)~

推荐学习:mysql视频教程

The above is the detailed content of Teach you step by step how to play with MySQL binlog. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete