mysql利用frm和idb文件恢復數據庫


一、將data目錄下的對應的數據庫目錄復制備份到另外一個地方

[root@orderer /]# mkdir /home/data_bak
[root@orderer /]# cp -r /home/mysql-5.7.26/data/hl_central_sms_deduction/ /home/data_bak/
[root@orderer /]# 

二、連接MYSQL,刪除原來的數據庫,新建一個跟同名的數據庫

mysql> create database hl_central_sms_deduction DEFAULT CHARSET utf8;
Query OK, 1 row affected (0.01 sec)

三、利用MySQL Utilitie工具提供的mysqlfrm命令,導出數據庫表結構SQL文件

安裝MySQL Utilitie工具,參考https://www.cnblogs.com/sky-cheng/p/12218112.html

[root@orderer hl_central_sms_deduction]# mysqlfrm --diagnostic sms_deduction_log_20191201.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 sms_deduction_log_20191201.frm:
# The .frm file is a TABLE.
# CREATE TABLE Statement:

CREATE TABLE `sms_deduction_log_20191201` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
  `phone` varchar(60) DEFAULT NULL, 
  `epid` varchar(60) DEFAULT NULL, 
  `message` varchar(3000) DEFAULT NULL, 
  `subcode` varchar(60) DEFAULT NULL, 
  `channel_id` varchar(150) DEFAULT NULL, 
  `push_url` varchar(3000) DEFAULT NULL, 
  `db_ip` varchar(150) DEFAULT NULL, 
  `db_name` varchar(150) DEFAULT NULL, 
  `created` timestamp DEFAULT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
  `sms_len` int(11) DEFAULT NULL, 
  `stype` char(4) CHARACTER SET <UNKNOWN> NOT NULL, 
  `dtype` varchar(80) CHARACTER SET <UNKNOWN> NOT NULL, 
  `msg_template` varchar(3000) DEFAULT NULL, 
  `province` varchar(150) DEFAULT NULL, 
  `qxt_fast_num` int(1) DEFAULT NULL, 
  `link_id` varchar(150) DEFAULT NULL, 
  `report_code` varchar(150) DEFAULT NULL, 
PRIMARY KEY `PRIMARY` (`id`),
KEY `pk_sms_deduction_log_20191201_phone` (`phone`,`epid`,`created`) USING BTREE
) ENGINE=InnoDB;

#...done.

將SQL語句粘貼到客戶端執行,報錯

`stype` char(4) CHARACTER SET <UNKNOWN> NOT NULL, `dtype` varchar(80) CHARACTER SET <UNKNOWN> NOT NULL, 
將這兩句修改為
`stype` char(4)  NOT NULL, 
  `dtype` varchar(80) NOT NULL, 
再執行上面的建表語句,成功。

此時,查看data目錄下的hl_central_sms_deduction目錄下多了兩個文件
[root@orderer hl_central_sms_deduction]# ll
??? 156
-rw-r----- 1 mysql mysql     61 1?  20 14:54 db.opt
-rw-r----- 1 mysql mysql  37856 1?  20 15:45 sms_deduction_log_20191201.frm
-rw-r----- 1 mysql mysql 114688 1?  20 15:45 sms_deduction_log_20191201.ibd

四、執行解除表空間綁定命令,對應的idb文件自動刪除

mysql> use hl_central_sms_deduction;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> alter table sms_deduction_log_20191201 discard tablespace;
Query OK, 0 rows affected (0.04 sec)

mysql> 

查看data目錄下的hl_central_sms_deduction目錄

[root@orderer hl_central_sms_deduction]# ll
??? 44
-rw-r----- 1 mysql mysql    61 1?  20 14:54 db.opt
-rw-r----- 1 mysql mysql 37856 1?  20 15:45 sms_deduction_log_20191201.frm
[root@orderer hl_central_sms_deduction]# 

此時,idb文件已自動刪除,

五、我們將備份出來的ibd文件復制回去,再綁定表空間

[root@orderer hl_central_sms_deduction]# cp ../../../databak/hl_central_sms_deduction/sms_deduction_log_20191201.ibd .

修改文件所有者權限給mysql

[root@orderer hl_central_sms_deduction]# chown mysql:mysql sms_deduction_log_20191201.ibd

綁定表空間

mysql> alter table sms_deduction_log_20191201 import tablespace;
Query OK, 0 rows affected, 1 warning (1.82 sec)

mysql> 

完畢后,查看表數據,已經恢復

mysql> select count(*) from sms_deduction_log_20191201;
+----------+
| count(*) |
+----------+
|    97746 |
+----------+
1 row in set (0.04 sec)

mysql> 

六、可能遇到的問題

執行ALTER TABLE table_name DISCARD TABLESPACE;時

報錯[Error Code: 1451, SQL State: 23000] Cannot delete or update a parent row: a foreign key constraint fails ()

這是由於有外鍵關聯

SET foreign_key_checks = 0; --先設置外鍵約束檢查關閉
SET foreign_key_checks = 1; --都執行完alter操作后再開啟外鍵約束檢查,以保持表結構完整性

先關閉外鍵約束,執行刪除操作,然后再開啟外鍵約束。

執行ALTER TABLE table_name IMPORT TABLESPACE;

報錯Error Code:1812. Tablespace is missing for table <table_name>

是因為copy的ibd文件沒有賦權,需要chown mysql:mysql table_name.ibd --賦權限


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM