How MySQL Records Binlog

Source: Internet
Author: User

--mysql How to record Binlog -------------------------------2014/07/08 contents of the Binlog file log event  MySQL's Binlog file is a record of various modifications to the database that represent the data structure of the modification operation as log event. Different log event corresponding to the modification operation. Several log event types are commonly used: Query event, Row event, Xid event, and more. Where the query event corresponds to an SQL statement, it is much more in the DDL operation and in the Binlog of the stmt format. The row event is a base class, and its derived classes have row insert event, row Update event, row Delete event three, which correspond to the increment, change, and delete operations of row format Binlog. The Xid event corresponds to a commit operation that supports transactions, and for a commit operation that does not support transactions, the record is in the form of query event. Other things like format log event, Rotate event, and more, you can check out the official MySQL documentation for more information. The type of log event has been increasing, such as the new checkpoint event in Innosql. MySQL itself has an interface in order to add a log event, but adding a log event requires implementing several necessary method functions, such as print, write, Get_code_type, and so on. The content of the Binlog file is a collection of various log events.   Generate Log EventThe transaction logs the event of the operation to the thread's Cache_mngr when executing DDL, or update. 1, the DDL operation will record the event in the operation function of the DB, these functions are: mysql_upgrade_db,mysql_alter_db,mysql_rm_db and so on. Other DDL operations call the function Write_bin_log to record binlog.      2, DML statements that perform modification operations do different things depending on the binlog format. If the Binlog format is stmt, the function that logs the event is binlog_query, and what the function does is:1, construct a query event according to the DML operation statement:query_log_event qinfo (This,query_arg,query_len, ...)2, writes the event to the cachemysql_bin_log.write (&qinfo)the functions that call Binlog_query are: Mysql_insert,mysql_delete,mysql_update and so on. 3, if the Binlog format is row, the process of modifying the DML statement that records the event calls Binlog_log_row, and each row is modified to record a row event, so the call Binlog_log_rowAre the engine-layer interface functions that modify a single row: Ha_write_row, Ha_update_row, Ha_delete_row.   The function Binlog_log_row calls three different functions according to the specific type of the row operation: Binlog_write_row, Binlog_update_row, Binlog_delete_row. The function of these three functions is to construct the corresponding row event and write the row's data to the row event. the specific process is:Binlog_write_row1,binlog_prepare_pending_rows_event (table, server_id, cols, ...);2,ev->add_row_data (Row_data, Len); Binlog_update_row1,ev= binlog_prepare_pending_rows_event (table, server_id, cols, ...);2,ev->add_row_data (Before_row, before_size)Ev->add_row_data (After_row, after_size) Binlog_delete_row1,ev= binlog_prepare_pending_rows_event (table, server_id, cols, ...);2,ev->add_row_data (Row_data, Len);The newly constructed row event is written to the thread's cache the next time the event is logged.   Io_cacheThe process of writing Binlog is cache write, the cache structure used is io_cache.   Cache_mngr,cache_dataThe transaction generates log event during execution, and these log event records are in a buffer associated with the thread, and each transaction thread has such a cache block (provided the transaction opens the function of recording binlog), the name of this buffer is binlog_cache_ Mngr, the structure is as follows:class Binlog_cache_mngr {binlog_cache_data Stmt_cache;//Binlog buffers used by storage engines that do not support transactionsBinlog_cache_data Trx_cache; Binlog buffers used by the storage engine that supports transactions.....   }The log event is stored in the CACHE_MNGR cache structure Cache_data, Cache_data has a io_cache cache space, and the log event is actually written to this io_cache binlog_cache_ The structure of data is as follows:class binlog_cache_data{Io_cache Cache_log;.....   }after a transaction executes a modification, the process of writing the corresponding log event to the thread buffer is:1), get the thread's Cache_mngr pointer:cache_mngr= (binlog_cache_mngr*) thd_get_ha_data (THD, Binlog_hton);2), get the pointer to the Cache_data buffer:cache_data= Cache_mngr->get_binlog_cache_data (Use_trans_cache (THD, is_transactional));3), write the event to Io_cache:Io_cache *file= &cache_data->cache_log;event->write (file);  Write Binlog    a transaction writes the resulting log event to an external Binlog file during the commit phase. The log event is written to the external Binlog file serially by different transactions, so all log event for a transaction is contiguous in the Binlog file, and the log event for any other transaction is not inserted in the middle. MySQL as a whole is a plug-in structure, Binlog is also as an engine plug-in is called by the upper layer, when the transaction commits the upper MySQL will call the office has the engine's commit interface, the Binlog commit interface is called first, and then call the other engine's submission interface. So when a transaction commits, it writes Binlog and then commits the underlying engine (such as the write redo log and the dirty page of the InnoDB commit process)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.