Loading... ## 背景 > * **在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 ``` * 突然,由于某原因数据库突然宕机并且无法启动了。来活了。。。 **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(11) NOT 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. 按照以上步骤,依次将所有的表空间恢复即可。 最后修改:2025 年 04 月 05 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏