search
HomeDatabaseMysql Tutorialmysql对binlog的处理说明

Mysql和其它开源数据库相比,具有更好的扩展性。其主要原因是它提供了存储引擎的开放接口。喜欢自己折腾数据库的程序员可以从这个接口起步,打造有个性的数据库。

然而这里不打算对某种存储引擎的实现细节进行描述,也不打算介绍各种存储引擎的优缺点,只是描述一下mysql如何处理binlog,并澄清几个容易混淆的问题。
Binlog对mysql而言是重要的,主要体现在它的功能上。Mysql官方文档明确指出,binlog的启动大概会为mysql增加1%的负载,因此在绝大多数情况下,binlog都不会成为mysql的性能瓶颈。
Binlog是mysql以二进制形式打印的日志,它默认不加密,不压缩。每个正常的binlog文件头部,有4个字节的标记,值为0xfe 0x62 0x69 0x6e。LOG_EVENT是binlog里的单位,即正常情况下binlog按照逐LOG_EVENT的形式增长。除去头部的标记,binlog就是一个LOG_EVENT的序列。每个LOG_EVENT都独立单元,没有互相引用的关系,它也有自己的二进制头部,主要是记录了时间戳、类型标记等描述信息。
Mysql把磁盘操作的实现封装在IO_CACHE结构里,这也方便了我们对binlog的研究和描述,后文如果没有特别说明,读写binlog与读写IO_CACHE的含义相同。
为了解mysql写入binlog的过程,可以找一个sql语句的处理过程进行跟踪。以update为例,在最简单的情况下,mysql会先调用为存储引擎开放的接口ha_update_row,然而执行binlog_query对binlog进行写操作。这样处理的原因是,在主从备份的场景下,如果主库先写入binlog成功、在执行update的过程中crash,从库有可能执行update成功,此时主库重启之后,与从库的数据不一致。如果update操作发生在事务性的表上,在写入binlog之后会执行开放接口ha_autocommit_or_rollback,由存储引擎判断操作结果。
在主从备份的场景下,主库相当于server,从库相当于client,双方采用tcp短连接。从库发出读取日志的请求,主库接收请求、读取本地binlog、然后发送给从库。从库接收日志,进行简单校验后写本地日志,称为relay log。此处从库的流程专门由一个线程负责,称为同步io线程。从库还有一个线程,称为同步sql线程。它的行为是,定期读取relay log,解析并执行同步过来的sql语句。

下面回答几个问题:

1. binlog的格式?
二进制顺序存储,不加密,不压缩

2.binlog使用WAL吗?

No
3.主库发送binlog,是使用内存里的copy吗?

无法确定,很有可能是先从磁盘上读一份,然后发送。

4. relaylog使用WAL吗?

Yes。从库接收到日志后,会先写relay log

5. binlog和relaylog的SQL是否一致?

在网络传输正确性可靠的前提下,yes

提一个问题:
既然binlog不使用WAL,那么在主从场景下,mysql异常之后,主库和从库是否会不一致呢?

之前有个问题一直没弄明白:
既然mysql是先做数据操作、再写binlog,如果写binlog的时候失败,mysql又crash,数据怎么办?
答案是由存储引擎决定数据。
可以把mysql和它的存储引擎分开看,因为mysql只是一个框架,而不是一个实现。
binlog是mysql自己的日志,而事务是由存储引擎本身保证的。
以update为例,mysql做的事情简单分为:
1. 修改数据update
2. 写binlog
3. 如果当前处理的表是一个事务性的表,则commit或rollback
注意此处的update和commit/rollback都由存储引擎实现,mysql只是站在逻辑的高度上理解这些操作。
对于事务型的引擎innodb,它本身有日志保证数据的一致性。在innodb的实现中,update修改数据之前,
会新建一个事务,并建立一个回滚点。而在innodb提供的commit/rollback接口会提交/回滚事务。
因此对innodb而言,每条SQL语句的事务,其实包含了binlog的写操作。然而即使是这样,innodb仍然无法保证
binlog和数据的一致性,因为innodb在写commit成功后crash,回滚操作不会回滚binlog。按照手册上的说法,
把--innodb-support-xa设置为1,同时保证sync_binlog=1,才能保证innodb的binlog和数据一致。

对于非事务型的引擎myisam,没有commit/rollback的机会,因此在异常情况下,数据会和binlog不一致。
那么新的问题出现了:myisam如何处理这个不一致呢?
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are the different storage engines available in MySQL?What are the different storage engines available in MySQL?Apr 26, 2025 am 12:27 AM

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

What are some common security vulnerabilities in MySQL?What are some common security vulnerabilities in MySQL?Apr 26, 2025 am 12:27 AM

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

How can you identify slow queries in MySQL?How can you identify slow queries in MySQL?Apr 26, 2025 am 12:15 AM

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

How can you monitor MySQL server health and performance?How can you monitor MySQL server health and performance?Apr 26, 2025 am 12:15 AM

To monitor the health and performance of MySQL servers, you should pay attention to system health, performance metrics and query execution. 1) Monitor system health: Use top, htop or SHOWGLOBALSTATUS commands to view CPU, memory, disk I/O and network activities. 2) Track performance indicators: monitor key indicators such as query number per second, average query time and cache hit rate. 3) Ensure query execution optimization: Enable slow query logs, record and optimize queries whose execution time exceeds the set threshold.

Compare and contrast MySQL and MariaDB.Compare and contrast MySQL and MariaDB.Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

How does MySQL's licensing compare to other database systems?How does MySQL's licensing compare to other database systems?Apr 25, 2025 am 12:26 AM

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

When would you choose InnoDB over MyISAM, and vice versa?When would you choose InnoDB over MyISAM, and vice versa?Apr 25, 2025 am 12:22 AM

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

Explain the purpose of foreign keys in MySQL.Explain the purpose of foreign keys in MySQL.Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft