mysql数据库丢失_-MySQL LSN:数据库崩溃恢复的导航灯塔 - node circle,#publish-mermaid-1-1792401881175
一、MySQL LSN 基础概念
1.1 LSN 的定义与特性
日志序列号(Log Sequence Number,简称 LSN)是 InnoDB 存储引擎中一个至关重要的概念。它是一个不断增长的 8 字节无符号整数,用于唯一标识重做日志(Redo Log)和 undo 日志中的每一条记录。LSN 具有单调递增的特性,意味着每次对数据库的修改(无论是数据变更还是日志记录)都会产生一个新的、更大的 LSN 值。这个特性使得 LSN 成为了数据库内部事件发生顺序的精确记录者。
1.2 LSN 的存储位置
LSN 并非只存在于日志文件中,它被广泛地记录在多个关键位置,形成了一个分布式的“检查点”系统。这些位置包括:
重做日志文件:每条重做日志记录都带有其自身的 LSN。undo 日志文件:undo 日志同样被赋予 LSN,用于记录回滚操作。数据页的页头:每个数据页的页头中都存储了该页的最新 LSN,表示页上所有变更的最新状态。系统表空间(System Tablespace):在 SYS_TABLESPACES 等系统表中,也记录了相关的 LSN 信息,用于全局状态管理。 拼多多帮助力入口
#publish-mermaid-0-1792401880891{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-0-1792401880891 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-0-1792401880891 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-0-1792401880891 .error-icon{fill:#552222;}#publish-mermaid-0-1792401880891 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-0-1792401880891 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-0-1792401880891 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-0-1792401880891 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-0-1792401880891 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-0-1792401880891 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-0-1792401880891 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-0-1792401880891 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-0-1792401880891 .marker.cross{stroke:#333333;}#publish-mermaid-0-1792401880891 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-0-1792401880891 p{margin:0;}#publish-mermaid-0-1792401880891 g.classGroup text{fill:#9370DB;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#publish-mermaid-0-1792401880891 g.classGroup text .title{font-weight:bolder;}#publish-mermaid-0-1792401880891 .cluster-label text{fill:#333;}#publish-mermaid-0-1792401880891 .cluster-label span{color:#333;}#publish-mermaid-0-1792401880891 .cluster-label span p{background-color:transparent;}#publish-mermaid-0-1792401880891 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-0-1792401880891 .cluster text{fill:#333;}#publish-mermaid-0-1792401880891 .cluster span{color:#333;}#publish-mermaid-0-1792401880891 .nodeLabel,#publish-mermaid-0-1792401880891 .edgeLabel{color:#131300;}#publish-mermaid-0-1792401880891 .noteLabel .nodeLabel,#publish-mermaid-0-1792401880891 .noteLabel .edgeLabel{color:black;}#publish-mermaid-0-1792401880891 .edgeLabel .label rect{fill:#ECECFF;}#publish-mermaid-0-1792401880891 .label text{fill:#131300;}#publish-mermaid-0-1792401880891 .labelBkg{background:#ECECFF;}#publish-mermaid-0-1792401880891 .edgeLabel .label span{background:#ECECFF;}#publish-mermaid-0-1792401880891 .classTitle{font-weight:bolder;}#publish-mermaid-0-1792401880891 .node rect,#publish-mermaid-0-1792401880891 .node circle,#publish-mermaid-0-1792401880891 .node ellipse,#publish-mermaid-0-1792401880891 .node polygon,#publish-mermaid-0-1792401880891 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1;}#publish-mermaid-0-1792401880891 .divider{stroke:#9370DB;stroke-width:1;}#publish-mermaid-0-1792401880891 g.clickable{cursor:pointer;}#publish-mermaid-0-1792401880891 g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#publish-mermaid-0-1792401880891 g.classGroup line{stroke:#9370DB;stroke-width:1;}#publish-mermaid-0-1792401880891 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#publish-mermaid-0-1792401880891 .classLabel .label{fill:#9370DB;font-size:10px;}#publish-mermaid-0-1792401880891 .relation{stroke:#333333;stroke-width:1;fill:none;}#publish-mermaid-0-1792401880891 .dashed-line{stroke-dasharray:3;}#publish-mermaid-0-1792401880891 .dotted-line{stroke-dasharray:1 2;}#publish-mermaid-0-1792401880891 [id$="-compositionStart"],#publish-mermaid-0-1792401880891 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-0-1792401880891 [id$="-compositionEnd"],#publish-mermaid-0-1792401880891 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-0-1792401880891 [id$="-dependencyStart"],#publish-mermaid-0-1792401880891 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-0-1792401880891 [id$="-dependencyEnd"],#publish-mermaid-0-1792401880891 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-0-1792401880891 [id$="-extensionStart"],#publish-mermaid-0-1792401880891 .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-0-1792401880891 [id$="-extensionEnd"],#publish-mermaid-0-1792401880891 .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-0-1792401880891 [id$="-aggregationStart"],#publish-mermaid-0-1792401880891 .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-0-1792401880891 [id$="-aggregationEnd"],#publish-mermaid-0-1792401880891 .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-0-1792401880891 [id$="-lollipopStart"],#publish-mermaid-0-1792401880891 .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-0-1792401880891 [id$="-lollipopEnd"],#publish-mermaid-0-1792401880891 .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-0-1792401880891 .edgeTerminals{font-size:11px;line-height:initial;}#publish-mermaid-0-1792401880891 .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-0-1792401880891 .edgeLabel[data-look="neo"]{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-0-1792401880891 .edgeLabel[data-look="neo"] p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-0-1792401880891 .edgeLabel[data-look="neo"] rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-0-1792401880891 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-0-1792401880891 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-0-1792401880891 .node .neo-node{stroke:#9370DB;}#publish-mermaid-0-1792401880891 [data-look="neo"].node rect,#publish-mermaid-0-1792401880891 [data-look="neo"].cluster rect,#publish-mermaid-0-1792401880891 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792401880891 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-0-1792401880891 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-0-1792401880891 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792401880891 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-0-1792401880891 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792401880891 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-0-1792401880891 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792401880891 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792401880891 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}hashasstoresmanages11111111LSN+int value+increment()RedoLog+LSN lsn+apply()UndoLog+LSN lsn+rollback()DataPage+LSN page_lsn+update_data()SystemTablespace+LSN global_lsn+update_global_state()
上图展示了 LSN 与重做日志、undo 日志、数据页以及系统表空间之间的关联关系,LSN 作为核心标识贯穿于整个 InnoDB 的日志和存储体系中。
二、LSN 在恢复流程中的核心作用
2.1 恢复流程概览
当 MySQL 实例意外崩溃后重启时,InnoDB 会自动执行一个称为“崩溃恢复”的过程。这个过程严格遵循一个三阶段的流程,而 LSN 是驱动整个流程的核心指针。整个恢复过程可以概括为:分析(Analysis)阶段确定需要恢复的范围,重做(Redo)阶段应用日志,回滚(Undo)阶段清理未完成的事务。
#publish-mermaid-1-1792401881175{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1-1792401881175 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1-1792401881175 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1-1792401881175 .error-icon{fill:#552222;}#publish-mermaid-1-1792401881175 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1-1792401881175 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1-1792401881175 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1-1792401881175 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1-1792401881175 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1-1792401881175 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1-1792401881175 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1-1792401881175 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1-1792401881175 .marker.cross{stroke:#333333;}#publish-mermaid-1-1792401881175 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1-1792401881175 p{margin:0;}#publish-mermaid-1-1792401881175 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#publish-mermaid-1-1792401881175 .cluster-label text{fill:#333;}#publish-mermaid-1-1792401881175 .cluster-label span{color:#333;}#publish-mermaid-1-1792401881175 .cluster-label span p{background-color:transparent;}#publish-mermaid-1-1792401881175 .label text,#publish-mermaid-1-1792401881175 span{fill:#333;color:#333;}#publish-mermaid-1-1792401881175 .node rect,#publish-mermaid-1-1792401881175 .node circle,#publish-mermaid-1-1792401881175 .node ellipse,#publish-mermaid-1-1792401881175 .node polygon,#publish-mermaid-1-1792401881175 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1-1792401881175 .rough-node .label text,#publish-mermaid-1-1792401881175 .node .label text,#publish-mermaid-1-1792401881175 .image-shape .label,#publish-mermaid-1-1792401881175 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1-1792401881175 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1-1792401881175 .rough-node .label,#publish-mermaid-1-1792401881175 .node .label,#publish-mermaid-1-1792401881175 .image-shape .label,#publish-mermaid-1-1792401881175 .icon-shape .label{text-align:center;}#publish-mermaid-1-1792401881175 .node.clickable{cursor:pointer;}#publish-mermaid-1-1792401881175 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1-1792401881175 .arrowheadPath{fill:#333333;}#publish-mermaid-1-1792401881175 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1-1792401881175 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1-1792401881175 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1-1792401881175 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1-1792401881175 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1-1792401881175 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1-1792401881175 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1-1792401881175 .cluster text{fill:#333;}#publish-mermaid-1-1792401881175 .cluster span{color:#333;}#publish-mermaid-1-1792401881175 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-1-1792401881175 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1-1792401881175 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1-1792401881175 .icon-shape,#publish-mermaid-1-1792401881175 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1-1792401881175 .icon-shape p,#publish-mermaid-1-1792401881175 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1-1792401881175 .icon-shape .label rect,#publish-mermaid-1-1792401881175 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1-1792401881175 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1-1792401881175 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1-1792401881175 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1-1792401881175 [data-look="neo"].node rect,#publish-mermaid-1-1792401881175 [data-look="neo"].cluster rect,#publish-mermaid-1-1792401881175 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792401881175 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-1-1792401881175 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1-1792401881175 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792401881175 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1-1792401881175 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792401881175 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-1-1792401881175 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792401881175 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792401881175 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}数据库崩溃重启实例分析阶段重做阶段回滚阶段数据库正常启动
上图描绘了崩溃恢复的宏观流程,LSN 在每个阶段都扮演着决定性的角色,确保恢复的顺序和完整性。
2.2 第一步:分析阶段 (Analysis Phase)
恢复过程的第一步是“分析”阶段。InnoDB 会读取日志文件,并比较日志中的 LSN 与数据页中记录的 LSN。其核心目标是确定两个关键的 LSN 值:
检查点 LSN (Checkpoint LSN):这是上一次成功将内存中的脏页刷新到磁盘时,对应的日志 LSN。它标志着数据库在崩溃前的一个稳定状态。当前 LSN (Current LSN):这是日志文件当前的写入位置,代表了所有已记录但可能尚未应用到磁盘的变更。
通过比较这两个 LSN,InnoDB 可以确定哪些日志记录是“已提交但未刷新到磁盘”的,哪些是“活跃事务”产生的。分析阶段结束后,InnoDB 就知道了需要应用哪些重做日志。
数据页日志文件InnoDB 引擎数据页日志文件InnoDB 引擎#publish-mermaid-2-1792401881308{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-2-1792401881308 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-2-1792401881308 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-2-1792401881308 .error-icon{fill:#552222;}#publish-mermaid-2-1792401881308 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-2-1792401881308 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-2-1792401881308 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-2-1792401881308 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-2-1792401881308 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-2-1792401881308 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-2-1792401881308 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-2-1792401881308 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-2-1792401881308 .marker.cross{stroke:#333333;}#publish-mermaid-2-1792401881308 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-2-1792401881308 p{margin:0;}#publish-mermaid-2-1792401881308 .actor{stroke:#9370DB;fill:#ECECFF;stroke-width:1;}#publish-mermaid-2-1792401881308 rect.actor.outer-path[data-look="neo"]{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792401881308 rect.note[data-look="neo"]{stroke:#aaaa33;fill:#fff5ad;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792401881308 text.actor>tspan{fill:black;stroke:none;}#publish-mermaid-2-1792401881308 .actor-line{stroke:#9370DB;}#publish-mermaid-2-1792401881308 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#publish-mermaid-2-1792401881308 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#publish-mermaid-2-1792401881308 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#publish-mermaid-2-1792401881308 [id$="-arrowhead"] path{fill:#333;stroke:#333;}#publish-mermaid-2-1792401881308 .sequenceNumber{fill:white;}#publish-mermaid-2-1792401881308 [id$="-sequencenumber"]{fill:#333;}#publish-mermaid-2-1792401881308 [id$="-crosshead"] path{fill:#333;stroke:#333;}#publish-mermaid-2-1792401881308 .messageText{fill:#333;stroke:none;}#publish-mermaid-2-1792401881308 .labelBox{stroke:#9370DB;fill:#ECECFF;filter:none;}#publish-mermaid-2-1792401881308 .labelText,#publish-mermaid-2-1792401881308 .labelText>tspan{fill:black;stroke:none;}#publish-mermaid-2-1792401881308 .loopText,#publish-mermaid-2-1792401881308 .loopText>tspan{fill:black;stroke:none;}#publish-mermaid-2-1792401881308 .sectionTitle,#publish-mermaid-2-1792401881308 .sectionTitle>tspan{fill:black;stroke:none;}#publish-mermaid-2-1792401881308 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:#9370DB;fill:#9370DB;}#publish-mermaid-2-1792401881308 .note{stroke:#aaaa33;fill:#fff5ad;}#publish-mermaid-2-1792401881308 .noteText,#publish-mermaid-2-1792401881308 .noteText>tspan{fill:black;stroke:none;font-weight:normal;}#publish-mermaid-2-1792401881308 .activation0{fill:#f4f4f4;stroke:#666;}#publish-mermaid-2-1792401881308 .activation1{fill:#f4f4f4;stroke:#666;}#publish-mermaid-2-1792401881308 .activation2{fill:#f4f4f4;stroke:#666;}#publish-mermaid-2-1792401881308 .actorPopupMenu{position:absolute;}#publish-mermaid-2-1792401881308 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#publish-mermaid-2-1792401881308 .actor-man circle,#publish-mermaid-2-1792401881308 line{fill:#ECECFF;stroke-width:2px;}#publish-mermaid-2-1792401881308 g rect.rect{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));stroke:#9370DB;}#publish-mermaid-2-1792401881308 .node .neo-node{stroke:#9370DB;}#publish-mermaid-2-1792401881308 [data-look="neo"].node rect,#publish-mermaid-2-1792401881308 [data-look="neo"].cluster rect,#publish-mermaid-2-1792401881308 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792401881308 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-2-1792401881308 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-2-1792401881308 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792401881308 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-2-1792401881308 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792401881308 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-2-1792401881308 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792401881308 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792401881308 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}确定需要应用的重做日志范围读取 Checkpoint LSN读取各页的 Page LSN比较Log LSN与Page LSN进入重做阶段
上图展示了分析阶段的交互过程,通过对比不同位置的 LSN,InnoDB 精确地定位了恢复的起点。
2.3 第二步:重做阶段 (Redo Phase)
在重做阶段,InnoDB 会从分析阶段确定的 LSN 位置开始,顺序地读取重做日志,并将日志中记录的变更重新应用到相应的数据页上。这个过程会一直持续到日志文件的末尾。重做阶段确保了所有在崩溃前已提交的事务都被正确地持久化到磁盘,使得数据库达到崩溃前的最终一致性状态。
#publish-mermaid-3-1792401881388{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-3-1792401881388 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-3-1792401881388 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-3-1792401881388 .error-icon{fill:#552222;}#publish-mermaid-3-1792401881388 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-3-1792401881388 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-3-1792401881388 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-3-1792401881388 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-3-1792401881388 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-3-1792401881388 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-3-1792401881388 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-3-1792401881388 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-3-1792401881388 .marker.cross{stroke:#333333;}#publish-mermaid-3-1792401881388 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-3-1792401881388 p{margin:0;}#publish-mermaid-3-1792401881388 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#publish-mermaid-3-1792401881388 .cluster-label text{fill:#333;}#publish-mermaid-3-1792401881388 .cluster-label span{color:#333;}#publish-mermaid-3-1792401881388 .cluster-label span p{background-color:transparent;}#publish-mermaid-3-1792401881388 .label text,#publish-mermaid-3-1792401881388 span{fill:#333;color:#333;}#publish-mermaid-3-1792401881388 .node rect,#publish-mermaid-3-1792401881388 .node circle,#publish-mermaid-3-1792401881388 .node ellipse,#publish-mermaid-3-1792401881388 .node polygon,#publish-mermaid-3-1792401881388 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-3-1792401881388 .rough-node .label text,#publish-mermaid-3-1792401881388 .node .label text,#publish-mermaid-3-1792401881388 .image-shape .label,#publish-mermaid-3-1792401881388 .icon-shape .label{text-anchor:middle;}#publish-mermaid-3-1792401881388 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-3-1792401881388 .rough-node .label,#publish-mermaid-3-1792401881388 .node .label,#publish-mermaid-3-1792401881388 .image-shape .label,#publish-mermaid-3-1792401881388 .icon-shape .label{text-align:center;}#publish-mermaid-3-1792401881388 .node.clickable{cursor:pointer;}#publish-mermaid-3-1792401881388 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-3-1792401881388 .arrowheadPath{fill:#333333;}#publish-mermaid-3-1792401881388 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-3-1792401881388 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-3-1792401881388 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-3-1792401881388 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-3-1792401881388 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-3-1792401881388 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-3-1792401881388 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-3-1792401881388 .cluster text{fill:#333;}#publish-mermaid-3-1792401881388 .cluster span{color:#333;}#publish-mermaid-3-1792401881388 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-3-1792401881388 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-3-1792401881388 rect.text{fill:none;stroke-width:0;}#publish-mermaid-3-1792401881388 .icon-shape,#publish-mermaid-3-1792401881388 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-3-1792401881388 .icon-shape p,#publish-mermaid-3-1792401881388 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-3-1792401881388 .icon-shape .label rect,#publish-mermaid-3-1792401881388 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-3-1792401881388 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-3-1792401881388 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-3-1792401881388 .node .neo-node{stroke:#9370DB;}#publish-mermaid-3-1792401881388 [data-look="neo"].node rect,#publish-mermaid-3-1792401881388 [data-look="neo"].cluster rect,#publish-mermaid-3-1792401881388 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792401881388 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-3-1792401881388 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-3-1792401881388 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792401881388 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-3-1792401881388 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792401881388 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-3-1792401881388 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792401881388 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792401881388 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}重做阶段日志未结束日志结束从 Checkpoint LSN 开始读取重做日志应用变更到数据页更新数据页的 Page LSN读取下一条日志重做阶段完成
上图详细描述了重做阶段的循环过程,LSN 是驱动这个循环的“进度条”,确保每一步都按序执行。
2.4 第三步:回滚阶段 (Undo Phase)
重做阶段完成后,数据库的状态已经与崩溃前一致,但其中可能包含一些在崩溃时尚未提交的事务。回滚阶段的目标就是撤销这些未提交事务的影响。InnoDB 会扫描 undo 日志,根据其中的 LSN 和事务信息,将相关数据页恢复到事务开始前的状态。这个过程也被称为“回滚”或“清理”。
#publish-mermaid-4-1792401881510{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-4-1792401881510 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-4-1792401881510 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-4-1792401881510 .error-icon{fill:#552222;}#publish-mermaid-4-1792401881510 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-4-1792401881510 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-4-1792401881510 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-4-1792401881510 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-4-1792401881510 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-4-1792401881510 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-4-1792401881510 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-4-1792401881510 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-4-1792401881510 .marker.cross{stroke:#333333;}#publish-mermaid-4-1792401881510 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-4-1792401881510 p{margin:0;}#publish-mermaid-4-1792401881510 defs [id$="-barbEnd"]{fill:#333333;stroke:#333333;}#publish-mermaid-4-1792401881510 g.stateGroup text{fill:#9370DB;stroke:none;font-size:10px;}#publish-mermaid-4-1792401881510 g.stateGroup text{fill:#333;stroke:none;font-size:10px;}#publish-mermaid-4-1792401881510 g.stateGroup .state-title{font-weight:bolder;fill:#131300;}#publish-mermaid-4-1792401881510 g.stateGroup rect{fill:#ECECFF;stroke:#9370DB;}#publish-mermaid-4-1792401881510 g.stateGroup line{stroke:#333333;stroke-width:1;}#publish-mermaid-4-1792401881510 .transition{stroke:#333333;stroke-width:1;fill:none;}#publish-mermaid-4-1792401881510 .stateGroup .composit{fill:white;border-bottom:1px;}#publish-mermaid-4-1792401881510 .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px;}#publish-mermaid-4-1792401881510 .state-note{stroke:#aaaa33;fill:#fff5ad;}#publish-mermaid-4-1792401881510 .state-note text{fill:black;stroke:none;font-size:10px;}#publish-mermaid-4-1792401881510 .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#publish-mermaid-4-1792401881510 .edgeLabel .label rect{fill:#ECECFF;opacity:0.5;}#publish-mermaid-4-1792401881510 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-4-1792401881510 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-4-1792401881510 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-4-1792401881510 .edgeLabel .label text{fill:#333;}#publish-mermaid-4-1792401881510 .label div .edgeLabel{color:#333;}#publish-mermaid-4-1792401881510 .stateLabel text{fill:#131300;font-size:10px;font-weight:bold;}#publish-mermaid-4-1792401881510 .node circle.state-start{fill:#333333;stroke:#333333;}#publish-mermaid-4-1792401881510 .node .fork-join{fill:#333333;stroke:#333333;}#publish-mermaid-4-1792401881510 .node circle.state-end{fill:#9370DB;stroke:white;stroke-width:1.5;}#publish-mermaid-4-1792401881510 .end-state-inner{fill:white;stroke-width:1.5;}#publish-mermaid-4-1792401881510 .node rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-4-1792401881510 .node polygon{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-4-1792401881510 [id$="-barbEnd"]{fill:#333333;}#publish-mermaid-4-1792401881510 .statediagram-cluster rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-4-1792401881510 .cluster-label,#publish-mermaid-4-1792401881510 .nodeLabel{color:#131300;}#publish-mermaid-4-1792401881510 .statediagram-cluster rect.outer{rx:5px;ry:5px;}#publish-mermaid-4-1792401881510 .statediagram-state .divider{stroke:#9370DB;}#publish-mermaid-4-1792401881510 .statediagram-state .title-state{rx:5px;ry:5px;}#publish-mermaid-4-1792401881510 .statediagram-cluster.statediagram-cluster .inner{fill:white;}#publish-mermaid-4-1792401881510 .statediagram-cluster.statediagram-cluster-alt .inner{fill:#f0f0f0;}#publish-mermaid-4-1792401881510 .statediagram-cluster .inner{rx:0;ry:0;}#publish-mermaid-4-1792401881510 .statediagram-state rect.basic{rx:5px;ry:5px;}#publish-mermaid-4-1792401881510 .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#f0f0f0;}#publish-mermaid-4-1792401881510 .note-edge{stroke-dasharray:5;}#publish-mermaid-4-1792401881510 .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#publish-mermaid-4-1792401881510 .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#publish-mermaid-4-1792401881510 .statediagram-note text{fill:black;}#publish-mermaid-4-1792401881510 .statediagram-note .nodeLabel{color:black;}#publish-mermaid-4-1792401881510 .statediagram .edgeLabel{color:red;}#publish-mermaid-4-1792401881510 [id$="-dependencyStart"],#publish-mermaid-4-1792401881510 [id$="-dependencyEnd"]{fill:#333333;stroke:#333333;stroke-width:1;}#publish-mermaid-4-1792401881510 .statediagramTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-4-1792401881510 [data-look="neo"].statediagram-cluster rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1;}#publish-mermaid-4-1792401881510 [data-look="neo"].statediagram-cluster rect.outer{rx:5px;ry:5px;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792401881510 .node .neo-node{stroke:#9370DB;}#publish-mermaid-4-1792401881510 [data-look="neo"].node rect,#publish-mermaid-4-1792401881510 [data-look="neo"].cluster rect,#publish-mermaid-4-1792401881510 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792401881510 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-4-1792401881510 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-4-1792401881510 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792401881510 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-4-1792401881510 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792401881510 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-4-1792401881510 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792401881510 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792401881510 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}事务开始事务提交正常结束事务回滚恢复完成ActiveTransactionCommittedRolledback扫描 Undo Log应用 Undo 记录继续扫描回滚完成ScanUndoLogApplyUndo
上图使用状态图展示了事务的生命周期以及回滚阶段如何通过扫描和应用 Undo Log 来清理未提交的事务,恢复数据一致性。
三、LSN 的具体应用场景
3.1 崩溃恢复
如前所述,LSN 是崩溃恢复的核心。没有 LSN,InnoDB 将无法判断哪些变更已经持久化,哪些还需要重做,整个恢复过程将变得混乱且不可靠。LSN 提供了一个精确的、全局的时间线,确保了恢复的原子性和一致性。
3.2 增量备份与恢复
在数据库备份与恢复策略中,LSN 也扮演着关键角色。全量备份会记录一个特定的 LSN 点。当需要进行增量备份时,备份工具会从全量备份的 LSN 点开始,只备份在此之后产生的重做日志。在恢复时,系统首先恢复全量备份,然后从全量备份的 LSN 点开始,应用增量备份期间的所有重做日志,直到恢复到目标 LSN 点。这使得数据库能够快速地从备份中恢复,而无需重放所有历史日志。
#publish-mermaid-5-1792401881736{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-5-1792401881736 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-5-1792401881736 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-5-1792401881736 .error-icon{fill:#552222;}#publish-mermaid-5-1792401881736 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-5-1792401881736 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-5-1792401881736 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-5-1792401881736 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-5-1792401881736 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-5-1792401881736 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-5-1792401881736 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-5-1792401881736 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-5-1792401881736 .marker.cross{stroke:#333333;}#publish-mermaid-5-1792401881736 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-5-1792401881736 p{margin:0;}#publish-mermaid-5-1792401881736 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#publish-mermaid-5-1792401881736 .cluster-label text{fill:#333;}#publish-mermaid-5-1792401881736 .cluster-label span{color:#333;}#publish-mermaid-5-1792401881736 .cluster-label span p{background-color:transparent;}#publish-mermaid-5-1792401881736 .label text,#publish-mermaid-5-1792401881736 span{fill:#333;color:#333;}#publish-mermaid-5-1792401881736 .node rect,#publish-mermaid-5-1792401881736 .node circle,#publish-mermaid-5-1792401881736 .node ellipse,#publish-mermaid-5-1792401881736 .node polygon,#publish-mermaid-5-1792401881736 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-5-1792401881736 .rough-node .label text,#publish-mermaid-5-1792401881736 .node .label text,#publish-mermaid-5-1792401881736 .image-shape .label,#publish-mermaid-5-1792401881736 .icon-shape .label{text-anchor:middle;}#publish-mermaid-5-1792401881736 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-5-1792401881736 .rough-node .label,#publish-mermaid-5-1792401881736 .node .label,#publish-mermaid-5-1792401881736 .image-shape .label,#publish-mermaid-5-1792401881736 .icon-shape .label{text-align:center;}#publish-mermaid-5-1792401881736 .node.clickable{cursor:pointer;}#publish-mermaid-5-1792401881736 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-5-1792401881736 .arrowheadPath{fill:#333333;}#publish-mermaid-5-1792401881736 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-5-1792401881736 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-5-1792401881736 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-5-1792401881736 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-5-1792401881736 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-5-1792401881736 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-5-1792401881736 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-5-1792401881736 .cluster text{fill:#333;}#publish-mermaid-5-1792401881736 .cluster span{color:#333;}#publish-mermaid-5-1792401881736 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-5-1792401881736 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-5-1792401881736 rect.text{fill:none;stroke-width:0;}#publish-mermaid-5-1792401881736 .icon-shape,#publish-mermaid-5-1792401881736 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-5-1792401881736 .icon-shape p,#publish-mermaid-5-1792401881736 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-5-1792401881736 .icon-shape .label rect,#publish-mermaid-5-1792401881736 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-5-1792401881736 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-5-1792401881736 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-5-1792401881736 .node .neo-node{stroke:#9370DB;}#publish-mermaid-5-1792401881736 [data-look="neo"].node rect,#publish-mermaid-5-1792401881736 [data-look="neo"].cluster rect,#publish-mermaid-5-1792401881736 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792401881736 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-5-1792401881736 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-5-1792401881736 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792401881736 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-5-1792401881736 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792401881736 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-5-1792401881736 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792401881736 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792401881736 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}全量备份记录备份LSN增量备份从备份LSN开始收集新产生的Redo Log恢复流程恢复全量备份从备份LSN开始应用增量Redo Log恢复到目标LSN
上图展示了基于 LSN 的增量备份与恢复流程,LSN 精确地定义了备份的边界和恢复的范围。
四、LSN 的管理与实践
4.1 LSN 与 InnoDB 参数
InnoDB 的重做日志大小和数量配置直接影响 LSN 的管理。innodb_log_file_size 和 innodb_log_files_in_group 参数决定了重做日志文件的总大小和数量。更大的日志文件可以容纳更多的 LSN 范围,减少日志切换的频率,但在崩溃恢复时可能需要更长的时间来重放日志。
4.2 监控 LSN
DBA 可以通过 SHOW ENGINE INNODB STATUS 命令查看当前的 LSN 信息,例如 Log sequence number 字段。监控 LSN 的增长速度可以帮助评估系统的写入负载和日志文件的使用情况,是数据库性能监控的重要指标之一。
五、总结
日志序列号(LSN)是 MySQL InnoDB 存储引擎中一个看似简单却至关重要的概念。它不仅是日志记录的编号,更是数据库恢复机制的“大脑”和“时钟”。通过在日志、数据页和系统状态中广泛分布,LSN 提供了一个统一、精确的时间戳,确保了在崩溃后,数据库能够按照正确的顺序、精确地恢复到一致的状态。深入理解 LSN 的工作原理,对于任何希望精通 MySQL 高可用性和数据恢复技术的开发者或 DBA 来说,都是不可或缺的一步。,mysql数据库丢失