mysql restore database through .frm and .ibd files

 Problem background: Due to the forced shutdown of mysql in the service, some data tables and data are lost as shown below. Only the .frm .ibd file is my problem file. The table structure and table data directory cannot be found in D:XXXX\mysql-5.7.24-winx64. \data\mydata

 Recover table structure from frm file

Back up the original data first to avoid errors during the process

  • First back up the .frm and .ibd files of the previous data and create a database with the same name as before
show variables like 'datadir'; //查看data路径
create database mydata; //创建数据库

eg: 

  • Enter the database and create a table with the same name. Since you don’t know the table structure, create 1 column first.
use mydata; //使用刚刚创建的数据库
create table mydata (id int); //创建和丢失的表一样的表

eg.

  • replace file

Replace the current xxxx.frm file with the file named xxxx.frm in the previously copied file.

  •  It is recommended to restart the server and use the command to shut down

  • View the table structure (an error will be thrown at this time)

 desc f_comments;//查看表结构

    eg.

          When we look at the error log, we will see this sentence
       
:

       Throw error message:

        Display f_comments original table has 9 columns

  •  Delete table f_comments and create 9 list structure
drop table f_comments;
create table info (id int,id2 int,id3 int,id4 int,id5 int,id6 int,id7 int,id8 int,id9 int);

eg.

 

  •  Copy again and replace the f_comments.frm file in the mysql data directory with the backup f_comments.frm file.
     
  • Modify the parameters in the mysql configuration file my.conf and restart the server

Location:

       eg:
        
Then restart the server 

Enter the database, view the table structure, and record the statements that create the table structure.
 

 show create table f_comments;

eg. Create table statement and continue to scroll down. 


In this way, the table structure will be restored. 

Recover table data from ibd file
 

  • Comment out the parameter innodb_force_recovery=6 in my.conf, restore the default parameters, and restart the mysql service
  • Delete the f_comments table, copy the above create table structure statement, and re-create the f_comments table
drop table f_comments;
 CREATE TABLE `f_comments` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `time` varchar(255) DEFAULT NULL,
  `hospital_num` varchar(255) DEFAULT NULL,
  `shu_name` varchar(255) DEFAULT NULL,
  `manager` varchar(255) DEFAULT NULL,
  `project` varchar(255) DEFAULT NULL,
  `zhu_name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

eg.
 

  • Delete current ibd file
alter table f_comments discard tablespace; //删除f_comments.ibd文件

eg

  • Replace idb file
  • Load idb file
alter table f_comments import tablespace;

eg

 

Finish:

 Reference: .frm and .ibd file recovery database_frm ibd recovery data_u010009053's blog-CSDN blog

 

Guess you like

Origin blog.csdn.net/T3165919332/article/details/132532327