暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MySQL严重故障场景数据恢复

呆呆的私房菜 2025-03-20
21
    Whoami5年+金融、政府、医疗领域工作经验的DBA
    Certificate:PGCM、OCP、YCP
    Skill:Oracle、Mysql、PostgreSQL、国产数据库
    Platform:CSDN、墨天轮、公众号(呆呆的私房菜)
    复制
    阅读本文可以了解严重Mysql严重损坏场景下如何进行恢复,帮助DBA们在极端情况下进行修复。

    01

    概述
    • 在MYSQL发生严重故障时,定位分析后数据库仍无法正常启动的情况下,但是数据库又没有备份的时候,如何快速恢复数据并恢复业务访问呢?
    • 1. 初始化一个新的数据库实例,恢复相关业务库表结构;
    • 2. 拷贝源端数据文件到目标库中,通过下线和上线.ibd文件方式恢复。

    • 话不多说,来实操下吧。
      -- 假设源端的业务表如下
      mysql> create table t1 (id int primary key, info text);
      Query OK, 0 rows affected (0.04 sec)


      mysql> insert into t1 values (1'this is test1'), (2'this is test2'), (3'this is test3');
      Query OK, 3 rows affected (0.02 sec)
      Records: 3  Duplicates: 0  Warnings: 0
      复制
      • 突然,由于某原因数据库突然宕机并且无法启动了。DBA开始来活了。。。

      02

      故障恢复
      • 1. 编写一份新的数据库配置文件,修改数据文件目录,重新初始化一个新的空实例。
        cp /etc/my.cnf etc/my.cnf.bak
        vi etc/my.cnf   -- 调整相关路径
        mysqld --defaults-file=/etc/my.cnf --user=mysql &
        复制
        • 2. 下载mysql utilities工具集

        https://downloads.mysql.com/archives/utilities/

          tar xzf mysql-utilities-1.6.5.tar.gz
          cd mysql-utilities-1.6.5
          python setup.py build
          python setup.py install
          复制
          • 3. 解析mysql表结构
            mysqlfrm --diagnostic t1.frm
            # WARNING: Cannot generate character set or collation names without the --server option.
            # CAUTION: The diagnostic mode is a best-effort parse of the .frm file. As such, it may not identify all of the components of the table correctly. This is especially true for damaged files. It will also not read the default values for the columns and the resulting statement may not be syntactically correct.
            # Reading .frm file for t1.frm:
            # The .frm file is a TABLE.
            # CREATE TABLE Statement:


            CREATE TABLE `t1` (
              `id` int(11) NOT NULL,
              `info` text DEFAULT NULL,
            PRIMARY KEY `PRIMARY` (`id`)
            ) ENGINE=InnoDB;


            #...done.
            复制
            • 4. 提取出建表语句,在新实例上创建表
              CREATE TABLE `t1` (
                `id` int(11NOT NULL,
                `info` text DEFAULT NULL,
              PRIMARY KEY `PRIMARY` (`id`)
              ) ENGINE=InnoDB;
              复制
              • 5. 离线和上线.ibd文件
                -- 新的数据库实例上下线空表的.ibd文件
                alter table t1 discard tablespace;


                -- 拷贝源端t1表的.ibd文件到新实例的数据目录下,并确认文件属组
                chown mysql:mysql t1.ibd


                -- 新实例上线.ibd文件
                alter table t1 import tablespace;


                -- 此时,数据已经恢复
                mysql> select * from t1;
                +----+---------------+
                | id | info          |
                +----+---------------+
                |  1 | this is test1 |
                |  2 | this is test2 |
                |  3 | this is test3 |
                +----+---------------+
                3 rows in set (0.00 sec)
                复制
                • 6. 按照以上步骤,依次将所有的表空间恢复即可。


                本文内容就到这啦,相信读完本篇你也掌握了极端故障场景下MYSQL的故障恢复了吧!希望本篇内容能给你带来帮助。我们下篇再见!

                点击上方公众号,关注我吧!


                文章转载自呆呆的私房菜,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                评论