Home Database Mysql Tutorial mysql对binlog的处理说明

mysql对binlog的处理说明

Jun 07, 2016 pm 06:03 PM
binlog mysql

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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

See all articles