{"id":2183,"date":"2020-07-30T00:24:30","date_gmt":"2020-07-29T16:24:30","guid":{"rendered":"http:\/\/39.97.231.10\/?p=2183"},"modified":"2020-07-30T00:24:30","modified_gmt":"2020-07-29T16:24:30","slug":"mysql-redo-log-%e6%a0%bc%e5%bc%8f%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=2183","title":{"rendered":"MySQL redo log \u683c\u5f0f\u89e3\u6790"},"content":{"rendered":"<div>\n<div>\n\u672c\u6587\u7b80\u5355\u4ecb\u7ecdMySQL redo log \u6587\u4ef6\u683c\u5f0f\uff0c\u672c\u6587\u6d89\u53ca\u7684MySQL \u7248\u672c\u4e3a8.0.20\u3002\n<\/div>\n<\/div>\n<div>\n<div>\n<h4>\u4e00\u3001\u57fa\u7840\u77e5\u8bc6<\/h4>\n<h5>1.1 \u5b57\u8282\u5e8f<\/h5>\n<p>\u5728\u5177\u4f53\u4ecb\u7ecdredo log\u6587\u4ef6\u683c\u5f0f\u4e4b\u524d\uff0c\u5148\u4e86\u89e3\u4e00\u4e0b InnoDB \u6570\u503c\u7c7b\u578b\u7684\u5b57\u8282\u5e8f\uff0cInnoDB\u91c7\u7528\u5927\u7aef\u5b57\u8282\u5e8f\u5b58\u50a8\u6570\u503c\uff0c\u8fd9\u4e2a\u4e0eBinlog\u4f7f\u7528\u7684\u5c0f\u7aef\u5b57\u8282\u5e8f\u4e0d\u540c\uff0c\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6570\u503c0x12345678\uff0c\u5360\u75284\u4e2a\u5b57\u8282\uff0c\u4e0d\u540c\u7684\u5b57\u8282\u5e8f\u5176\u5b58\u50a8\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5927\u7aef\u5b57\u8282\u5e8f\uff1a12 34 56 78<\/li>\n<li>\u5c0f\u7aef\u5b57\u8282\u5e8f\uff1a78 56 34 12<\/li>\n<\/ul>\n<p>\u663e\u7136\u5927\u7aef\u5b57\u8282\u5e8f\u66f4\u7b26\u5408\u4eba\u7684\u9605\u8bfb\u4e60\u60ef\uff0c\u7136\u800c\u76ee\u524d\u4f7f\u7528\u5e7f\u6cdb\u7684x86\u67b6\u6784CPU\uff0c\u6bd4\u5982Intel\u3001AMD\uff0c\u90fd\u662f\u5c0f\u7aef\u5b57\u8282\u5e8f\u3002<\/p>\n<h5>1.2 redo log \u76f8\u5173\u53c2\u6570<\/h5>\n<ul>\n<li>innodb_log_files_in_group\uff0credo log\u6587\u4ef6\u4e2a\u6570\uff0c\u9ed8\u8ba4\u503c2\uff0c\u53d6\u503c\u8303\u56f42~100\u3002<\/li>\n<li>innodb_log_file_size\uff0c\u5355\u4e2aredo log\u6587\u4ef6\u5927\u5c0f\uff0c\u9ed8\u8ba4\u503c\u4e3a48M\uff0c\u6700\u5c0f\u503c4M\uff0c\u6700\u5927\u503c\u4e3a512G\/innodb_log_files_in_group<\/li>\n<li>innodb_log_buffer_size\uff0credo log \u5185\u5b58\u7f13\u5b58\u5927\u5c0f\uff0c\u9ed8\u8ba4\u503c16M\u3002<\/li>\n<li>innodb_log_write_ahead_size\uff0c\u5b9a\u4e49\u4e86\u9884\u5199\u5165\u7684\u5927\u5c0f\uff0c\u9ed8\u8ba4\u503c8KB\uff0c\u6700\u5c0f\u503c512B\uff0c\u6700\u5927\u503c\u4e0e\u9875\u5927\u5c0f\u76f8\u540c\u3002<\/li>\n<li>innodb_log_checksums\uff0c\u662f\u5426\u4e3aredo log\u542f\u7528checksum\u6821\u9a8c\u3002<\/li>\n<\/ul>\n<h4>\u4e8c\u3001redo log\u683c\u5f0f<\/h4>\n<h5>2.1 redo log \u6574\u4f53\u683c\u5f0f<\/h5>\n<p>MySQL redo log\uff0c\u9ed8\u8ba4\u914d\u7f6e\u4e0b\u6709\u4e24\u4e2a\u6587\u4ef6\uff0c\u5206\u522b\u662fib_logfile0\u548cib_logfile1\uff0c\u8fd9\u4e24\u4e2a\u6587\u4ef6\u6709\u5b8c\u5168\u76f8\u540c\u7684\u683c\u5f0f\u3002redo log \u6587\u4ef6\u6700\u5c0f\u5355\u4f4d\u662f512\u5b57\u8282\u7684\u4e00\u4e2a\u5757\uff0c\u6bcf\u4e2a\u5757\u7684\u6700\u540e4\u4e2a\u5b57\u8282\uff0c\u5b58\u50a8\u8fd9\u4e2a\u5757\u7684checksum\u6821\u9a8c\u503c\u3002redo log \u6587\u4ef6\u524d4\u4e2a\u5757\uff0c\u4e5f\u5c31\u662f\u524d2048\u4e2a\u5b57\u8282\u4e3a\u6587\u4ef6\u5934\uff0c\u6587\u4ef6\u5934\u5b58\u50a8\u4e86redo log\u6587\u4ef6\u5143\u6570\u636e\u4fe1\u606f\u548ccheckpoint\u4fe1\u606f\u3002\u6574\u4f53\u6765\u770bredo log\u6587\u4ef6\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre>   0 --------------------------------\n            log file header block\n512 ---------------------------------\n            checkpoint block1\n1024--------------------------------\n            5.7\u4fdd\u7559\uff0c8.0\u7528\u4e8e\u52a0\u5bc6\n1536--------------------------------\n            checkpoint block2\n2048--------------------------------\n            redo log record ...\nxxxxx--------------------------------\n<\/pre>\n<p>\u5757\u5927\u5c0f\u53ca\u6587\u4ef6\u5934\u5927\u5c0f\u5728\u6e90\u7801\u4e2d\u5b9a\u4e49\u4e3a\uff1a<br \/>\n#define OS_FILE_LOG_BLOCK_SIZE 512<br \/>\nconstexpr uint32_t LOG_FILE_HDR_SIZE = 4 * OS_FILE_LOG_BLOCK_SIZE;<\/p>\n<h5>2.2 log file header block<\/h5>\n<p>log file header block\uff0c\u662fredo log\u6587\u4ef6\u7684\u7b2c\u4e00\u4e2a\u5757\uff0c\u5176\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre>0--------------------------------\n            log_head_format\n4---------------------------------\n            \u672a\u4f7f\u7528\n8--------------------------------\n            start_lsn\n16--------------------------------\n            log_head_creator\n48--------------------------------\n            \u672a\u4f7f\u7528\n508--------------------------------\n            checksum\n512--------------------------------\n<\/pre>\n<ul>\n<li>log_head_format\uff0c\u5360\u75284\u4e2a\u5b57\u8282\uff0credo log \u683c\u5f0f\u7248\u672c\u53f7\uff0c\u5f53\u524d\u6700\u65b0\u7248\u672c\u53f7\u4e3a4\uff0c\u5982\u4e0b\uff1a<br \/>\nLOG_HEADER_FORMAT_5_7_9 = 1,<br \/>\nLOG_HEADER_FORMAT_8_0_1 = 2,<br \/>\nLOG_HEADER_FORMAT_8_0_3 = 3,<br \/>\nLOG_HEADER_FORMAT_8_0_19 = 4,<br \/>\nLOG_HEADER_FORMAT_CURRENT = LOG_HEADER_FORMAT_8_0_19<\/li>\n<li>start_lsn\uff0c\u521d\u59cb\u503c\u4e3a16*512\uff0c\u8fd9\u4e2a\u503c\u5728\u521d\u59cb\u5316\u53ca\u5207\u6362redo log \u6587\u4ef6\u65f6\u5199\u5165<\/li>\n<li>log_head_creator\uff0c\u5360\u752832\u4e2a\u5b57\u8282\uff0c\u503c\u4e3aMySQL 8.0.20<\/li>\n<li>checksum\uff0c\u8be5block\u5757\u7684checksum\u503c<\/li>\n<\/ul>\n<p>\u5173\u4e8e log file header block \u7ed3\u6784\u586b\u5145\u89c1\u6e90\u7801\u51fd\u6570\uff1alog_files_header_fill()\uff0clog0chkp.cc<\/p>\n<h5>2.3 checkpoint block<\/h5>\n<p>redo log \u524d4\u4e2a\u5757\u4e2d\uff0c\u6709\u4e24\u4e2acheckpoint\u5757\uff0c\u8fd9\u4e24\u4e2acheckpoint\u5757\u6709\u5b8c\u5168\u76f8\u540c\u7684\u7ed3\u6784\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>0--------------------------------\n            checkpoint_no\n8---------------------------------\n            checkpoint_lsn\n16--------------------------------\n            lsn_offset\n24--------------------------------\n            innodb_log_buffer_size\n32--------------------------------\n            \u672a\u4f7f\u7528\n508--------------------------------\n            checksum\n512--------------------------------\n<\/pre>\n<ul>\n<li>checkpoint_no\u00a0\uff0c\u6bcf\u6b21checkpoint\u5b8c\u6210\u540e\uff0c\u9012\u589e1\u3002<\/li>\n<li>checkpoint_lsn\uff0clsn\u503c\uff0c\u5d29\u6e83\u6062\u590d\u4ece\u8fd9\u4e2a\u4f4d\u7f6e\u5f00\u59cb\u3002<\/li>\n<li>lsn_offset\uff0clsn\u5728redo log\u6587\u4ef6\u5185\u7684\u504f\u79fb\uff0c\u901a\u8fc7\u51fd\u6570log_files_real_offset_for_lsn()\u8ba1\u7b97\u83b7\u5f97\u3002<\/li>\n<li>innodb_log_buffer_size\uff0c\u53c2\u6570innodb_log_buffer_size\u7684\u5927\u5c0f\uff0c\u5b98\u65b9\u6587\u6863\u4e5f\u6ca1\u6709\u8bf4\u660e\u8be5\u5b57\u6bb5\u6709\u4ec0\u4e48\u7528\u3002<\/li>\n<li>checksum\u503c\uff0c\u8be5block\u5757\u7684checksum\u503c\u3002<\/li>\n<\/ul>\n<p>checkpoint \u5757\u76f8\u5173\u51fd\u6570\uff1a<\/p>\n<ul>\n<li>log_create_first_checkpoint()<\/li>\n<li>log_files_write_checkpoint()<\/li>\n<\/ul>\n<p>\u6ce8\u610f\uff1a<br \/>\n\u867d\u7136\u6bcf\u4e2aredo log\u6587\u4ef6\u90fd\u6709\u76f8\u540c\u7684\u6587\u4ef6\u5934\u7ed3\u6784\uff08\u8d77\u59cb4\u4e2a\u5757\uff09,\u4f46\u662fcheckpoint\u7684\u4e24\u4e2a\u5757\uff0c\u53ea\u6709\u7b2c\u4e00\u4e2aredo log file\uff08ib_logfile0\uff09\u6709\u6548\uff0c\u5176\u4ed6redo log file \u7684checkpoint\u5757\u586b\u51450\u3002\u5728\u5d29\u6e83\u6062\u590d\u65f6\uff0c\u4e5f\u53ea\u8bfbib_logfile0\u6587\u4ef6\u7684checkpoint\u4fe1\u606f\u3002<\/p>\n<h5>2.4 redo log data block<\/h5>\n<p>redo log \u6587\u4ef6\u524d4\u4e2a\u5757\u4ee5\u5916\u7684\u6570\u636e\u5757\uff0c\u90fd\u662f\u7528\u4e8e\u5b58\u50a8redo log \u8bb0\u5f55\u7528\u7684\uff0c\u79f0\u4e4b\u4e3aredo log data block\uff0c\u5b83\u4eec\u6709\u76f8\u540c\u7684\u683c\u5f0f\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>0--------------------------------\n            hdr_no\n4---------------------------------\n            data_len\n6--------------------------------\n            first_rec_group\n8--------------------------------\n            checkpoint_no\n12--------------------------------\n            data part\n508--------------------------------\n            checksum\n512--------------------------------\n<\/pre>\n<ul>\n<li>hdr_no\uff0c\u5360\u75284\u4e2a\u5b57\u8282\uff0c\u8868\u793a\u5757\u53f7\uff0c\u503c\u5fc5\u987b\u5927\u4e8e0\uff0c\u5176\u6700\u9ad8\u4f4d\u662fflush\u6807\u5fd7\u4f4d\uff0c\u8bbe\u7f6e\u4e3a1\uff0c\u8868\u793a\u8fd9\u4e2a\u5757\u5df2\u7ecf\u5237\u5230\u78c1\u76d8\u3002\u6700\u5927\u5141\u8bb8\u7684\u5757\u53f7\u4e3a\uff1aLOG_BLOCK_MAX_NO = 0x3FFFFFFFUL + 1;<\/li>\n<li>data_len\uff0c\u5360\u75282\u4e2a\u5b57\u8282\uff0c\u8868\u793a\u5f53\u524d\u5757\u5199\u5165\u7684\u5b57\u8282\u6570\uff0c\u5305\u62ec\u5757\u5934\u768412\u4e2a\u5b57\u8282\u3002\u8fd92\u4e2a\u5b57\u8282\u7684\u6700\u9ad8\u4f4d\u7528\u6765\u5b58\u50a8\u5f53\u524d\u5757\u662f\u5426\u52a0\u5bc6\u3002<\/li>\n<li>first_rec_group\uff0c\u5360\u75282\u4e2a\u5b57\u8282\uff0c\u7528\u6765\u5b58\u50a8mtr log \u7b2c\u4e00\u4e2a\u8bb0\u5f55\u5f00\u59cb\u7684\u504f\u79fb\u3002\u5982\u679c\u8fd9\u4e2a\u503c\u4e0d\u4e3a0\uff0crecover\u53ef\u4ee5\u4ece\u8fd9\u4e2a\u504f\u79fb\u70b9\u5f00\u59cb\u89e3\u6790\u65e5\u5fd7\u3002<\/li>\n<li>checkpoint_no\uff0c\u5360\u75284\u5b57\u8282\uff0c\u5f53\u6700\u65b0\u7684log block\u88ab\u5199\u5165\u65f6\u7684log_sys-&gt;next_checkpoint_no\u7684\u4f4e4\u5b57\u8282\u3002<\/li>\n<\/ul>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u7b80\u5355\u4ecb\u7ecdMySQL redo log \u6587\u4ef6\u683c\u5f0f\uff0c\u672c\u6587\u6d89\u53ca\u7684MySQL \u7248\u672c\u4e3a8.0.20\u3002 \u4e00\u3001\u57fa\u7840\u77e5\u8bc6  &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=2183\"> <span class=\"screen-reader-text\">MySQL redo log \u683c\u5f0f\u89e3\u6790<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[90,88],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"\u672c\u6587\u7b80\u5355\u4ecb\u7ecdMySQL redo log \u6587\u4ef6\u683c\u5f0f\uff0c\u672c\u6587\u6d89\u53ca\u7684MySQL \u7248\u672c\u4e3a8.0.20\u3002 \u4e00\u3001\u57fa\u7840\u77e5\u8bc6 &hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/2183"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2183"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/2183\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2183"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}