Table of Contents
1. Redo log concept
2. Cache and disk structure
Home Database Mysql Tutorial What is the concept of MySQL redo logs

What is the concept of MySQL redo logs

Jun 02, 2023 pm 11:14 PM
mysql

In the ACID characteristics of transactions, atomicity (A), consistency (C), and durability (D) are implemented by undo log and redo log, and isolation (I) is implemented by lock MVCC

undo log: The transaction has not been committed yet, and the execution is abnormal in the middle. You can use undo log to restore the data to the state before the transaction execution to ensure the atomicity of the transaction

redo log: The transaction commit is successful, because it takes a while to update the disk data Time, if an exception occurs at this time, you can use redo log to re-execute the SQL of this transaction to ensure the durability of the transaction (As long as the transaction commit is successful, no matter what abnormal events occur, as long as the MySQL service proceeds normally next time, then the The data of a commit must be restored)

1. Redo log concept

redo log: It is called a physical log and records the final modified data stored by page. page, which directly stores the final state of the data and is used to ensure the durability of the transaction.

The logical log, also known as the undo log, records the specific content of the corresponding SQL statement. If insert is executed now, delete will be executed when rolling back; if update is executed now, the original old value will be updated back.

The redo log is placed in /var/lib/mysql by default

What is the concept of MySQL redo logs

The redo log starts recording when the transaction begins (it is not recorded when the transaction commits, because the entire transaction does the operation There may be many. If the redo log is written at the time of commit, once an exception occurs at this time, the redo log has not been written. It is too late and the durability of the transaction cannot be ensured.) No matter whether the transaction is committed or not, it will be recorded. When the exception occurs (such as a power outage during data persistence), InnoDB will use the redo log to restore to the moment before the power outage to ensure the integrity of the data

innodb_log_buffer_size defaults to 16M, which is the size of the redo log buffer. As the transaction begins, the redo log begins to be written. If the transaction is relatively large, in order to avoid spending too much disk IO during transaction execution, a relatively large redo log cache can be set to save disk IO. There is a time to refresh when flushing to the disk. When the time is reached, disk IO is consumed. If the buffer is relatively large, the time to refresh will be reached more slowly and the efficiency will be higher.

What is the concept of MySQL redo logs

InnoDB modifies the operation data, not directly modifying the data on the disk, but actually only modifying the data in the Buffer Pool. InnoDB always first records the data changes in the Buffer Pool to the redo log for data recovery after a crash. Record the redo log first, and then find an opportunity to slowly refresh the dirty data in the Buffer Pool to the disk.

Two files in the directory specified by innodb_log_group_home_dir: ib_logfile0, ib_logfile1, this file is called the redo log

buffer pool Cache pool: Can store index cache, Data caching, etc., can speed up reading and writing, directly operate the data page, write the redo log modification, and even if it is completed, there will be a dedicated thread to write the dirty page in the buffer pool to the disk

The default size of the buffer pool is 134M ( MySQL 5.7)

What is the concept of MySQL redo logs

The general structure is as shown in the figure:

What is the concept of MySQL redo logs

Transaction reads and modifications all prioritize the cache operation Data in the pool. In actual projects, mysqld will run on a separate machine, and a large amount of memory can be allocated specifically for InnoDB's buffer pool to speed up CRUD

2. Cache and disk structure

What is the concept of MySQL redo logs

When a transaction is committed, the operation on the relationship diagram is to write the contents of the InnoDB Log Buffer to the disk. If the writing is successful, the redo log on the disk will record the status - commit, if not If the writing is successful or completed, the record status - prepare

log may also have exceptions, power outages and other problems during the process of writing to the disk, resulting in the writing of the redo log not being completed (this Equivalent to the transaction not being committed successfully). At this time, MySQL does not need to consider the integrity of the transaction when it recovers next time, because the status is not commit. Only when everything is written to the disk does it mean that the redo log is written successfully, and the status becomes commit. . After the status changes to commit, the ACID characteristics of the transaction need to be maintained.

Is it true that the dirty data in the buffer poll (the data has been modified) is written to the disk only when committing?

You don’t need to wait for commit to start. The amount of data that the transaction may modify is relatively large, and the cache capacity is limited. For the data cached by buffer poll, there will be a dedicated thread to refresh the disk at the appropriate time. If there is a power outage, the next time MySQL starts, it will be refreshed according to the redo The data recorded in the log is restored.

undo log itself is also recorded in redo log

The undo log supports transaction rollback, and it cannot be completed in an instant. The data on the disk will eventually be modified. In order to prevent exceptions during the rollback process, the undo log must be recorded in the redo log. For the bottom layer, after the operation is successfully written to the redo log, whether the transaction is successfully committed (commit) or successfully rolled back (rollback), it is considered successful.

What is a real transaction commit success?

Instead of flushing all the data to the disk, the redo log recording the complete operation of the transaction is written to the disk from the log buffer, and then the status of the modified data is set to commit. The transaction commit was successful. Although the data is still in the buffer poll at this time, as long as our redo log is saved completely, the data can be restored. There will be a dedicated thread responsible for writing the data in the buffer poll to the disk.

Transactions are carried out When operating, always write the redo log first, and then write to the buffer pool; if the transaction is successfully committed, it is necessary to ensure that the redo log is completely recorded on the disk

As for the changes to the table data, the dirty data pages of the buffer pool We don’t have to worry about whether the transaction is flushed to the disk. As long as the redo log is completely written to the disk, we can use the redo log to restore the data status of the successfully committed transaction at any time (The most important thing about the database is the log. instead of data)

The above is the detailed content of What is the concept of MySQL redo logs. For more information, please follow other related articles on the PHP Chinese website!

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)

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".

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 use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

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

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.

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

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles