If I want to talk to you about the locking mechanism in InnoDB, it will inevitably involve the MySQL log system, binlog, redo log, undo log, etc. I saw these three logs summarized by some friends. Not bad, hurry up and share it with your friends.
The log is an important part of themysql
database, recording various status information during the operation of the database.mysql
Logs mainly include error logs, query logs, slow query logs, transaction logs, and binary logs.
As a developer, what we need to focus on is the binary log (binlog
) and transaction log (includingredo log
andundo log
). This article will introduce these three types of logs in detail next.
binlog
is used to record write operations (excluding queries) information performed by the database and is saved on the disk in binary form.binlog
is the logical log ofmysql
and is recorded by theServer
layer. Themysql
database using any storage engine will recordbinlog
Log.
Logical log: It can be understood that what is recorded is the sql statement.
Physical log:mysql
The data is ultimately saved In the data page, the physical log records the data page changes.
binlog
is written by appending. Eachbinlog
file can be set through themax_binlog_size
parameter. When the file size reaches the given value, a new file will be generated to save the log.
In actual applications,binlog
has two main usage scenarios, namely master-slave replication and data recovery.
Master-slave replication: Openbinlog
on theMaster
side, and then sendbinlog
to eachSlave
side,Slave
side replaysbinlog
to achieve master-slave data consistency.
Data recovery: Recover data by using themysqlbinlog
tool.
For theInnoDB
storage engine, it will only be recorded when the transaction is committedbiglog
, the record is still in the memory at this time, so when wasbiglog
flushed to the disk?
mysql
Control the flushing timing ofbiglog
through thesync_binlog
parameter, the value range is0-N
:
0: No mandatory requirement, the system will decide when to write to the disk;
1: Every timecommit
##binlogmust be written to the disk when #;
binlogwill be written to the disk for every N transactions.
sync_binlogis
1, which is also
MySQL 5.7.7Default value for subsequent versions. However, setting a larger value can improve database performance. Therefore, in actual situations, you can also increase the value appropriately and sacrifice a certain degree of consistency to obtain better performance.
binlogThe log has three formats, namely
STATMENT,
ROWand
MIXED.
MySQL 5.7.7, the default format is
STATEMENT, after
MySQL 5.7.7, the default value is
ROW. The log format is specified by
binlog-format.
STATMENT: Replication based on
SQLstatements (
statement-based replication, SBR), each statement will be modified The SQL statements of the data will be recorded in
binlog.
ROW: Row-based replication (
row-based replication, RBR), does not record the context information of each SQL statement, only It is necessary to record which data has been modified.
MIXED: Mixed-based replication based on
STATMENTand
ROW, MBR
), general copying usesSTATEMENT
mode to savebinlog
, for operations that cannot be copied inSTATEMENT
mode, useROW
mode Savebinlog
So how does
mysqlensure consistency?
The simple way is to flush all data pages involved in modifications to disk every time a transaction is committed. However, doing so will cause serious performance problems, mainly reflected in two aspects:
BecauseInnodb
performs disk interaction in units ofpages
, and a transaction is likely to only modify a few data pages. Bytes, it would be a waste of resources to flush the complete data page to the disk at this time!
A transaction may involve modifying multiple data pages, and these data pages are not physically continuous. The performance of using random IO writing is too poor!
So
mysql
designedredo log
. Specifically, it only records what modifications the transaction has made to the data page, so It can perfectly solve the performance problem (relatively speaking, the file is smaller and it is sequential IO).
redo log
includes two parts: one is the log buffer in memory (redo log buffer
), The other is the log file on disk (redo logfile
).
mysql
Each time aDML
statement is executed, the record is first written toredo log buffer
, and then at a later point in time, multiple records are written at once. Operation records are written toredo log file
. This technology of writing logs first and then writing to disk is theWAL (Write-Ahead Logging)
technology often mentioned in
MySQL.
In computer operating systems, buffer data in user space (user space
) generally cannot be written directly to the disk, and must pass through the operating system kernel space (kernel space
) buffer (OS Buffer
).
Therefore,redo log buffer
writingredo logfile
actually writesOS Buffer
first, and then callsthrough the system fsync()
Flash it toredo log file
, the process is as follows:
mysql
Support Three timings for writingredo log buffer
toredo log file
can be configured through theinnodb_flush_log_at_trx_commit
parameter. The meaning of each parameter value is as follows:
As mentioned earlier,redo log
actually records changes to the data page, and this It is not necessary to save all the change records, soredo log
is implemented using a fixed size and cyclic writing method. When writing to the end, it will return to the beginning to write the log in a loop. As shown below:
At the same time, we can easily know that in innodb, there areredo log
that need to be flushed, and also There aredata pages
that also need to be flushed. The main significance ofredo log
is to reduce the requirement fordata pages
to be flushed**.
In the above figure,write pos
represents theLSN
(logical sequence number) position of theredo log
current record,check point
indicates theLSN
(logical sequence number) position corresponding toredo log
after the data page change record is flushed.
write pos
The part betweencheck point
is the empty part ofredo log
, used to record new records;## Between #check pointand
write posis the
redo logchange record of the data page to be written to the disk. When
write poscatches up with
check point, it will first push
check pointforward, vacate the position, and then record a new log.
innodb, no matter whether it was shut down normally or abnormally last time, the recovery operation will always be performed. Because
redo logrecords the physical changes of the data page, the recovery speed is much faster than the logical log (such as
binlog).
innodb, it will first check the
LSNof the data page in the disk. If the
LSNof the data page is less than the
in the log LSN, the recovery will start from
checkpoint.
checkpointis in progress before the downtime, and the disk brushing progress of the data page exceeds the disk brushing progress of the log page. At this time, data will appear. The
LSNrecorded in the page is greater than the
LSNin the log. At this time, the part that exceeds the progress of the log will not be redone, because this itself means that what has been done does not need to be redone. Do.
binlogand
redo log:
The binloglog is only used for archiving, and relying solely on
binlogdoes not have the
crash-safecapability.
But onlyredo log
will not work, becauseredo log
is unique toInnoDB
, and the records in the log will be overwritten after being written to disk. Therefore, bothbinlog
andredo log
need to be recorded at the same time to ensure that when the database is down and restarted, the data will not be lost.
One of the four major characteristics of database transactions is atomicity. Specifically, atomicity refers to a series of operations on the database, either all succeed or all fail. There may be partial success.
In fact, the bottom layer of atomicity is achieved throughundo log
.undo log
mainly records the logical changes of data. For example, anINSERT
statement corresponds to aundo log
ofDELETE
, for each # The ##UPDATEstatement corresponds to an opposite
UPDATE's
undo log, so that when an error occurs, you can roll back to the data state before the transaction.
undo logis also the key to
MVCC(multi-version concurrency control) implementation.
The above is the detailed content of What is the difference between binlog/redolog/undolog in MySQL?. For more information, please follow other related articles on the PHP Chinese website!