redo log与undo log

Preface:

    This together with fellow sufferers, the interview when talking to not be too Hi. The interviewer estimated that the direct and died. Understanding of MySQL, the more are finding it very clear talk understand the differences and connections between these two, the harder it.

    In the write their own blog make a clearer point of view, or to record what I understand of them.

 

1. The characteristics of the transaction to achieve

    What is a transaction? This problem is estimated each person's answers are not the same. I simply understood as: the transaction is a collection of operations, the set operation is atomic, succeed or fail, the intermediate state is not present.

    So what are the characteristics of it matters? This we all can be very Shunliu answer them: the ACID

    So ACID is how to achieve it? The dry down is estimated to be a large, actually I can not clearly answer this question, the answer can only copy of the book:

        Isolation of (I): database locks for

        Persistent (D): redo log to achieve

        Atoms of (A) , consistency (C): undo log implemented

 

2.redo log

    redo log redo log file is

    In our directory% MYSQL_DATA_DIR%, we can see that there will be two files named ib_logfile0 ib_logfile1, these two files are stored is the redo log.

    

    1) Record content

        redo log is an exclusive function of the InnoDB storage engine, it is mainly the records storage engine transaction log (insert, update, delete, etc. will have a physical record data change operation).    

    2) redo log with different binary log?

        According to the binlog introduced before, we know, binlog major transaction log records the data change operation, then the two are recorded transaction log, What's the difference whether it?

difference Produced by different storage engines Different forms of recorded content Different point in time written to disk
binlog Any storage engine will produce changes to the database Logical log records After the transaction is complete once the write
redo log InnoDB database changes generated will produce Modify the physical log record page It was written at the transaction in progress

        Said here about what is called the physical page modify log?

        We all know that on disk for InnoDB, the smallest storage format for the page, change the actual data is modified corresponding to the data pages on disk data storage, then the page where the modification of the physical log is when the update operation occurs, redo log data will record changes that occur in a page one position, what the value of modification.

 

    3) redo log storage format

        redo log stored in the log block, each log block is 512 bytes, the format is as follows:

    Each block includes a header, tailer and body.  

    Composition 4) redo log in

        redolog consists of two parts: log buffer (log buffer memory); redo log file (redo log file on disk)

        When the transaction occurs operations, related operations will be recorded in the log buffer (memory), and then some when the opportunity to be flushed to disk (redo log file)

    5) log buffer refresh timing disk

        * When the transaction commits

        * Log buffer when half of the memory space is used

        * Log when checkpoint

    6)LSN

        Introduce a concept LSN: Acronym Log Sequence Number, the number indicates the current redo log, it records the total amount of the redo log (in bytes).

        LSN not only in the redo log, there is also the total of each page, the header of each page, there is a value FIL_PAGE_LSN called, represents the LSN when the page was last refreshed size.

    7)checkpoint

        Originally I wanted to write, but to write this blog quite perfect

        https://blog.csdn.net/weixin_33712987/article/details/85967332 

 

    8) How to use the redo log to recover data

        We can first command show engine innodb status; look InnoDB state information

...
    
---
LOG
---
Log sequence number 4400601352
Log flushed up to   4400601352
Pages flushed up to 4400601352
Last checkpoint at  4400601315
0 pending log flushes, 0 pending chkp writes
15 log i/o's done, 0.09 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 8585216
Dictionary memory allocated 1013188
Buffer pool size   512
Free buffers       256
Database pages     256
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
    
...

    Note: If no status information, you can perform an update operation

    We focus a look at a few parameters in the LOG:

    Log sequence number: LSN value has been written in the log buffer 
    Log flushed up to: LSN value has been flushed to the redo logfile 
    Last checkpoint at: LSN value at the last checkpoint

 

    You can see the last checkpoint <log flushed, so then the next restart MySQL service and they will check these two values, found that two values ​​are not equal, the refresh operation is performed from the checkpoint at the Last redo logfile after the data corresponding to the page re-written from the logfile to disk.

 

3.undo log

    When a transaction fails or rolls back, we need to undo log, the data is rolled back to what it was before the transaction began.

    1) storage location

        undo an undo segment is stored in an internal database, located in the shared space table.

    2) the logical OR physical log log?

        undo log is the logical logs, the database will be restored to the original logic of the way.

    3) How to perform logical logs?

        When InnoDB rollback actually perform the reverse operation and the previous operation.

        For insert operations, recorded in the undo log in reverse a delete operation;

        Similarly: delete operation, then insert a record operation;

        update operations, an update operation is opposite to the recording, reducing the value of each column back

 

    4) undo log realization MVCC

        When reading a data, if the data is locked by another data, query the current transaction before the bank records are locked by undo log, and so achieve MVCC.

    5) undo log format    

        In InnoDB, includes two formats:

        insert undo log: refers to undo log generated in an insert operation;

        update undo log: produced in the delete and update operations undo log;

    6) How to achieve atomicity?

        Atomic transaction operations, operation of the group, either completed or rolled back if an error occurs during execution, we need to revert to the state of affairs start time.

        The principle of the atom undo log is: before the operation data, the data is first backed up to undo log (located in the undo segment), and data modification. When performing rollback, with the content of the undo log data restored to the initial state.

 

to sum up:

    MySQL caching mechanisms:

    When modifying database data, InnoDB first data read from the disk into memory, modify the physical copying of data (the modify data while the data recorded to the memory changes in the redo log; information recorded before the data to be modified undo log in).

    After the transaction is committed, it will be modified in-memory data (called dirty pages) brush back to disk (write ahead logging).

    If the transaction is rolled back, then the logical undo log information in the log replay, the data is restored to the initial state;

 

    If at this time the database is down, dirty pages and brush will not come to disk, then the next time the database is started, it checks LSN, found that less than last checkpoint LSN has been flushed to the LSN redo log file, and start from the checkpoint LSN refresh data to disk (recorded by redo log in)

 

recommend:

    I found a blog written by someone else than my good, uncomfortable. . .

    https://www.cnblogs.com/better-farther-world2099/p/9290966.html 

    https://blog.csdn.net/weixin_33712987/article/details/85967332

Published 122 original articles · won praise 119 · views 120 000 +

Guess you like

Origin blog.csdn.net/qq_26323323/article/details/103936312