zoukankan      html  css  js  c++  java
  • 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 --赋权限

  • 相关阅读:
    苹果全球营销高级副总裁Phil Schiller曾考虑炒掉长期创意代理商Media Arts Lab
    Amazon Seller Central is Temporarily Unavailable
    三星高管:我们手机卖的好是因为营销成功
    欧洲跨境电商增速将达21% 德国力压群雄
    苹果挖走了亚马逊搜索技术副总裁,或为提升应用商店搜索功能
    亚马逊CEO贝索斯致股东信:阐述公司未来计划
    Amazon.com 购物 信用卡预售期
    佛论婆媳关系
    Focalprice李培亮:梦想让人在我店里排队
    DX孟虎点评新兴市场:巴西俄罗斯火爆背后
  • 原文地址:https://www.cnblogs.com/sky-cheng/p/12218345.html
Copyright © 2011-2022 走看看