Detailed explanation of the three major logs of MySQL (binlog, redo log and undo log)

1.redo log

The redo log is the log of the InnoDB storage engine layer, also known as the redo log file. It is used to record changes in transaction operations. It records the value after data modification. It will be recorded regardless of whether the transaction is committed or not. The
redo log includes two parts: one is the log buffer (redo log buffer) in memory, and the other is the log on the disk. file (redo log file).
MySQL supports three timings for writing the redo log buffer to the redo log file, which can be configured through the innodb_flush_log_at_trx_commit parameter, roughly writing once every second, writing once every transaction commit, writing to the cache every second and pulling from the cache every second once to the file.

2.undo log

The undo log saves a version of the data before the transaction occurs, which can be used for rollback . At the same time, it can provide read under multi-version concurrency control (MVCC), that is, non-locking read.
The undo log mainly records the logical changes of the data, such as a The INSERT statement corresponds to a DELETE undo log, and each UPDATE statement corresponds to an opposite UPDATE undo log, so that when an error occurs, the data state before the transaction can be rolled back.

3.binlog

Binlog belongs to the MySQL Server level , also known as archive log, which belongs to logical log and is recorded in binary form. It is used to record the write operation (not including query) information performed by the database . There is no crash-safe by relying on binlog capable.

In practical applications, there are two main usage scenarios of binlog, namely master-slave replication and data recovery

The difference between redolog and binlog
is that the redo log belongs to the innoDB level, and the binlog belongs to the MySQL Server level, so that the consistency requirements can be met when the database uses other storage engines.
The redo log is a physical log, which records the updated content of the data page; the binlog is a logical log, which records the original logic of the update statement. redo log It is a physical log, and the record content is "what modification was made on a certain data page", which belongs to the InnoDB storage engine. The binlog is a logical log, and the record content is the original logic of the statement, similar to "add 1 to the c field of the line ID=2", which belongs to the MySQL Server layer.

Redo log is cyclic writing, and the size of the log space is fixed; binlog is append writing, which means that when a file reaches a certain size, the next file will be replaced without overwriting.
Binlog can be used as recovery data, master-slave replication is built, and redo log is used for data recovery after abnormal downtime or media failure.

The redo log is the log of the InnoDB storage engine layer, and the binlog is the log recorded by the MySQL Server layer. Both are logs that record certain operations (not all) and are naturally somewhat repetitive (but the formats of the two records are different).

Finally: This article is mostly based on this article, just adding my own understanding

Guess you like

Origin blog.csdn.net/weixin_45933454/article/details/132423417