dushaoshuai / dushaoshuai.github.io

0 stars 0 forks source link

MySQL: The Binary Log #101

Open dushaoshuai opened 1 year ago

dushaoshuai commented 1 year ago

binary log 中存储着数据库变更事件。binary log 在 MySQL server 中实现,不局限于哪个存储引擎。

mindmap
    The Binary Log
        purposes
            replication
            Point in Time Incremental Recovery
        formats
            statement based logging
            row based logging
            mixed logging
        SHOW
            SHOW BINARY LOGS
            SHOW BINLOG EVENTS
            mysqlbinlog
                display binlog
                backup binlog

作用

binary log 有两个重要的作用:

复制:replica 将 source server 的 binary log 中的事件拷贝到自己的 relay log,并应用 relay log 中事件,产生和 source server 一样的数据变更。

时间点恢复(Point-in-Time (Incremental) Recovery):在恢复一个全量备份后(将数据库状态恢复至制作该备份的时间点),执行(备份制作时间点之后产生的)binary log 中的事件,增量地将数据库从备份制作的时间点恢复到一个更近的时间点。

格式

statement-based logging 和 row-based logging 各有优缺点,具体可以查看 17.2.1.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication,精力有限,这里暂不展开。

查看

从远端服务器下载 binlog 文件到本地

查看现有的 binlog 日志文件名:

MySQL [db_1]> SHOW BINARY LOGS;
+------------------+-----------+-----------+
| Log_name         | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.001337 |    439259 | No        |
| mysql-bin.001338 |    432792 | No        |
+------------------+-----------+-----------+
2 rows in set (0.039 sec)

下载到本地:

$ mysqlbinlog --host $host --port $port --user $user --password --read-from-remote-server mysql-bin.001339  --raw 
Enter password: 

下载时,还可能会用到选项 --result-file

查看已下载到本地的 binlog 文件

mysqlbinlog mysql-bin.001330
mysqlbinlog -v mysql-bin.001330
mysqlbinlog -vv mysql-bin.001330

NOTE:阿里云这篇文档云数据库RDS MySQL版远程获取Binlog日志并解析Binlog日志里关于 --base64-output=decode-rows 选项的说法,个人认为并不准确,仅仅使用 -v 或者 -vv,事件就会以 伪-SQL 的形式显示,使用 --base64-output=decode-rows 选项只是会导致 BINLOG 语句不显示,也就是显示内容更精简了。

直接查看远端服务器上的 binlog 文件

$ mysqlbinlog --host $host --port $port --user $user --password --read-from-remote-server mysql-bin.001339  [-v/-vv]
Enter password: 

参见

链接 该链接涉及
https://dev.mysql.com/doc/refman/8.0/en/binary-log.html binary log, replication, data recovery, general query log, performance, Passwords, encryption, enable and disable binary log, ...
https://dev.mysql.com/doc/refman/8.0/en/binary-log-formats.html statement-based logging, row-based logging, mixed logging, different storage engines, issues with statement-based replication
https://dev.mysql.com/doc/refman/8.0/en/binary-log-setting.html binlog-format, enable and disable binary log, binlog_row_event_max_size, ...
https://dev.mysql.com/doc/refman/8.0/en/show-binary-logs.html SHOW BINARY LOGS
https://dev.mysql.com/doc/refman/8.0/en/show-binlog-events.html SHOW BINLOG EVENTS
https://dev.mysql.com/doc/refman/8.0/en/mysqlbinlog.html mysqlbinlog
https://dev.mysql.com/doc/refman/8.0/en/mysqlbinlog-row-events.html mysqlbinlog, Row Event Display, WRITE_ROWS_EVENT, UPDATE_ROWS_EVENT, DELETE_ROWS_EVENT, --base64-output=DECODE-ROWS, --verbous, BINLOG Statement
https://dev.mysql.com/doc/refman/8.0/en/mysqlbinlog-backup.html Back Up Binary Log Files
https://help.aliyun.com/knowledge_detail/41751.html?spm=a2c4g.290878.0.0.2b368acas6jbQW 云数据库RDS MySQL版远程获取Binlog日志并解析Binlog日志