Table of Contents
1. Buffer Pool
2. undo log file
3. Update buffer pool data
4. redo log buffer
5. If the transaction is not submitted, will there be any impact if the database goes down?
6. Submit the transaction, redo log configuration strategy
7. The final submission of the transaction, binlog
8. Buffer pool Dirty data is flushed to the disk
9. Summary
Home Database Mysql Tutorial Completely master the MySQL principles and InnoDB storage engine architecture design

Completely master the MySQL principles and InnoDB storage engine architecture design

Jan 19, 2022 pm 06:03 PM
mysql

This article brings you relevant knowledge about the InnoDB storage engine architecture design in the mysql principle. I hope it will be helpful to you.

Completely master the MySQL principles and InnoDB storage engine architecture design

InnoDB component structure:

  1. buffer pool: buffer pool, cache disk data

  2. redo log buffer: Record the operations on the buffer pool, and write to the disk according to the policy to prevent data loss due to downtime but the transaction has been submitted

  3. undo log: When the data in the buffer pool is processed When making modifications, you can roll back the transaction before it is committed. The old values ​​are written to the undo log file to facilitate rollback. At this time, the data in the buffer pool is inconsistent with that in the disk and is dirty data

1. Buffer Pool

Suppose there is an update statement now:

update users set name = 'lisi' where id = 1
Copy after login

Needs to be updated to the database, what operations will InnoDB perform?

Completely master the MySQL principles and InnoDB storage engine architecture design

First, InnoDB will determine whether the data id = 1 exists in the buffer pool. If it does not exist, it will be loaded from the disk into the buffer pool, and it will also Add an exclusive lock to the row data to prevent multiple SQLs from modifying the row data at the same time.

Completely master the MySQL principles and InnoDB storage engine architecture design

2. undo log file

Assume that id = 1 and the original value of this data name is name = 'zhangsan', now We want to update to name = 'lisi', then we need to write the old value name='zhangsan' and id=1 to the undo log file.

For students who are familiar with databases, they all understand the concept of transactions. Before the transaction is submitted, all operations may be rolled back, that is, name = 'lisi' can be rolled back to name = 'zhangsan' , so the value before the update is written to the undo log file.

Completely master the MySQL principles and InnoDB storage engine architecture design

3. Update buffer pool data

After the undo log file is written, start updating this entry in the memory data. Update name = 'zhangsan' with id = 1 to name = 'lisi'. At this time, the data in the memory has been updated, but the data on the disk has not changed. At this time, inconsistent dirty data appears.

Completely master the MySQL principles and InnoDB storage engine architecture design

You may have a question at this time. If the transaction is submitted, but the MySQL service is down, and the data in the memory has not been written to the disk, will it happen? Causes data loss and causes inconsistency in SQL execution data?

4. redo log buffer

In the InnoDB structure, there is a redo log buffer buffer to store redo logs. The so-called redo log, for example, id=1, name ='zhangsan' is changed to name='lisi' and it is a log.

Completely master the MySQL principles and InnoDB storage engine architecture design

#But at this time, the redo log buffer only exists in the memory, and data recovery after MySQL is down cannot be realized.

5. If the transaction is not submitted, will there be any impact if the database goes down?

In fact, it has no impact. The transaction was not submitted, which means that the execution was not successful. Even after MySQL crashes or goes down, the modified data in the buffer pool and redo log buffer in the memory will be lost, and there will be no impact. Does not affect the consistency of data. If the transaction commit fails, the data in the database will not change.

Completely master the MySQL principles and InnoDB storage engine architecture design

6. Submit the transaction, redo log configuration strategy

When submitting the transaction, the redo log will implement redo according to the strategy Logs are written to disk from the redo log buffer. The policy is configured via innoDB_flush_log_at_trx_commit.

  1. The parameter of innoDB_flush_log_at_trx_commit is 0. Even after the transaction is committed, the redo log will not be written to the disk. After MySQL crashes, the data in the memory will be lost.

Completely master the MySQL principles and InnoDB storage engine architecture design

  1. ## The parameter of innoDB_flush_log_at_trx_commit is 1. After the transaction is submitted, the redo log will be flushed from the memory to the disk, as long as the transaction is submitted successfully. , the redo log must exist on the disk.

Completely master the MySQL principles and InnoDB storage engine architecture design

Even if the buffer pool data is not flushed to the disk at this time, you can still know which data has been modified from the redo log, and MySQL shuts down and restarts Afterwards, the modified data can be recovered from the redo log.

Completely master the MySQL principles and InnoDB storage engine architecture design

  1. The parameter of innoDB_flush_log_at_trx_commit is 2. After the transaction is submitted, the redo log only stays in the os cache and has not been flushed to the disk, in case the service goes down at this time. Then the data in the os cache will also be lost. Even if the transaction is submitted successfully, data will be lost.

Completely master the MySQL principles and InnoDB storage engine architecture design

After reading these, I believe that in order to ensure data security, parameter 1 is the best strategy.

7. The final submission of the transaction, binlog

binlog is actually a log file belonging to MySQL Server, and it is proposed here because it has a huge relationship with redo log association.

1) The difference between biglog and redo log

  • redo log: records redo logs with partial physical properties, such as "which data page What records have been recorded and what modifications have been made?”

  • binlog: biased toward logical logs, such as: “Do a row of data with id=10 in the users table After the update operation, what is the value after the update?"

2) When submitting the transaction, write the binlog at the same time

While executing the update, innoDB and The executor is constantly interacting, including loading data into the buffer pool, writing undo log files, updating memory data, writing redo logs, and flushing to disk. The writing to binlog is also performed by the executor.

Completely master the MySQL principles and InnoDB storage engine architecture design

Steps 1, 2, 3, and 4 are what you do when you execute the update statement, and steps 5 and 6 are what you do when you commit the transaction.

3) Binlog log flushing strategy analysis

sync_binlog parameter controls binlog flushing strategy

  1. The default value of sync_binlog is 0, after the transaction is submitted , the binlog log will be stored in the os cache. After MySQL is down, the data in the os cache will be lost.

  2. The sync_binlog value is 1. After the transaction is submitted, the binlog log will be flushed directly to the disk. middle.

4) Complete transaction submission based on binlog and redo log

After the binlog is written to the disk, the location and file name of the binlog log file will be written to redo log file, and write a commit mark in the redo log file.

Completely master the MySQL principles and InnoDB storage engine architecture design

5) What is the significance of commit tag?

The commit mark means keeping the redo log and binlog log consistent. If transaction submission starts in step 5 or step 6, MySQL is down, and there is no commit mark in the redo log, the transaction submission is considered failed.

means that the commit mark is that the transaction was finally submitted successfully.

8. Buffer pool Dirty data is flushed to the disk

Dirty data is flushed to the disk randomly by the background IO thread.

Completely master the MySQL principles and InnoDB storage engine architecture design

Consider this, what should I do if MySQL goes down before flashing the disk? At this time, the transaction has been submitted successfully, and there is a commit mark in the redo log. Even if it is down, after restarting, the data will be updated into the memory according to the redo log file, waiting for the IO thread to flush the disk.

9. Summary

After executing the analysis through the update statement, we learned that the InnoDB storage engine contains buffer pool buffer pool, redo log buffer buffer and other cache data. Log files such as undo and reod log, as well as MySQL Server log files.

When executing the update statement, the buffer pool, writing undo log file, writing redo log buffer and other operations will be modified; when the transaction is submitted, the redo log will be flushed, the binlog will be flushed, and the binlog file name will be written. and position, write the commit mark, and finally wait for the IO thread to randomly flush the dirty data in the buffer pool.

Recommended learning: mysql video tutorial

The above is the detailed content of Completely master the MySQL principles and InnoDB storage engine architecture design. 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.

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

How to view sql database error How to view sql database error Apr 10, 2025 pm 12:09 PM

The methods for viewing SQL database errors are: 1. View error messages directly; 2. Use SHOW ERRORS and SHOW WARNINGS commands; 3. Access the error log; 4. Use error codes to find the cause of the error; 5. Check the database connection and query syntax; 6. Use debugging tools.

See all articles