mysql独立表空间frm_MySQL 独立表空间恢复案例

生产库:

confulence库   jira库

联想服务器(IBM)

磁盘500G 没有raid

centos 6.8

mysql 5.6.33  innodb引擎  独立表空间

编译→制作rpm

/usr/bin/mysql

/var/lib/mysql

confulence   jira

所有软件和数据都在"/"

断电了,启动完成后“/” 只读

fsck   重启

结果:confulence库 在  , jira库不见了

求助:

这种情况怎么恢复?

我问:

有备份没

求助:

连二进制日志都没有,没有备份,没有主从

我说:

没招了,jira需要硬盘恢复了。

求助:

1、jira问题拉倒中关村了

2、能不能暂时把confulence库先打开用着

将生产库confulence,拷贝到1:1虚拟机上/var/lib/mysql,直接访问时访问不了的

问:有没有工具能直接读取ibd

我说:我查查,最后发现没有

我想出一个办法来:

create table xxx

alter table  confulence.t1 discard tablespace;

alter table confulence.t1 import tablespace;

虚拟机测试可行。

CREATE TABLE `city_new` (

`ID` int(11) NOT NULL AUTO_INCREMENT,

`Name` char(35) NOT NULL DEFAULT '',

`CountryCode` char(3) NOT NULL DEFAULT '',

`District` char(20) NOT NULL DEFAULT '',

`Population` int(11) NOT NULL DEFAULT '0',

PRIMARY KEY (`ID`),

KEY `CountryCode` (`CountryCode`),

KEY `idx_popu` (`Population`)

) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1;

面临的问题,confulence库中一共有107张表。

1、创建107和和原来一模一样的表。

他有2016年的历史库,我让他去他同时电脑上 mysqldump备份confulence库

mysqldump -uroot -ppassw0rd -B  confulence --no-data >test.sql

拿到你的测试库,进行恢复

到这步为止,表结构有了。

2、表空间删除。

select concat('alter table ',table_schema,'.'table_name,' discard tablespace;') from information_schema.tables where table_schema='confluence' into outfile '/tmp/discad.sql';

source /tmp/discard.sql

执行过程中发现,有20-30个表无法成功。主外键关系

ERROR 1215 (HY000): Cannot add foreign key constraint

很绝望,一个表一个表分析表结构,很痛苦。

set foreign_key_checks=0 跳过外键检查。

把有问题的表表空间也删掉了。

3、拷贝生产中confulence库下的所有表的ibd文件拷贝到准备好的环境中

select concat('alter table ',table_schema,'.'table_name,' import tablespace;') from information_schema.tables where table_schema='confluence' into outfile '/tmp/discad.sql';

4、验证数据

表都可以访问了,数据挽回到了出现问题时刻的状态(2-8)

案例模仿:

案例最新:

经过测试跨虚拟机也是可以成功的,如果需要foreign key的检查用set忽略 先把表结构导进去再说;

#第一个里程碑:导出建表的语句

CREATE TABLE `city` (

`ID` int(11) NOT NULL AUTO_INCREMENT,

`Name` char(35) NOT NULL DEFAULT '',

`CountryCode` char(3) NOT NULL DEFAULT '',

`District` char(20) NOT NULL DEFAULT '',

`Population` int(11) NOT NULL DEFAULT '0',

PRIMARY KEY (`ID`),

KEY `CountryCode` (`CountryCode`),

CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES `country` (`Code`)

) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1

#第二个里程碑:将表数据备份出来 防止万一

[root@db01-sa world]# mysqldump -uroot -p123 world city >/server/scripts/backup_world_city.sql

#第三个里程碑:检查查看备份语句是否正常

[root@db01-sa world]# cat /server/scripts/backup_world_city.sql

#第四个里程碑:

mysql> drop table world.city;

Query OK, 0 rows affected (0.05 sec)

mysql> show tables;

+-----------------+

| Tables_in_world |

+-----------------+

| country         |

| countrylanguage |

| people          |

| people_bak      |

+-----------------+

4 rows in set (0.00 sec)

[root@db01-sa world]# ll

total 1248

-rw-r----- 1 root  root    8710 Jun 26 23:11 city.frm.bak

-rw-r----- 1 root  root  589824 Jun 26 22:49 city.ibd.bak

#第五个里程碑:

CREATE TABLE `city` (

`ID` int(11) NOT NULL AUTO_INCREMENT,

`Name` char(35) NOT NULL DEFAULT '',

`CountryCode` char(3) NOT NULL DEFAULT '',

`District` char(20) NOT NULL DEFAULT '',

`Population` int(11) NOT NULL DEFAULT '0',

PRIMARY KEY (`ID`),

KEY `CountryCode` (`CountryCode`),

CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES `country` (`Code`)

) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1;

[root@db01-sa world]# \rm -fr city.ibd

[root@db01-sa world]# cp city.ibd.bak city.ibd

[root@db01-sa world]# chown -R mysql.mysql city.ibd

mysql> use world;

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> show tables;

ERROR 2006 (HY000): MySQL server has gone away

No connection. Trying to reconnect...

Connection id:    1

Current database: world

+-----------------+

| Tables_in_world |

+-----------------+

| city            |

| country         |

| countrylanguage |

| people          |

| people_bak      |

+-----------------+

5 rows in set (0.01 sec)

mysql> select * from city;

ERROR 1146 (42S02): Table 'world.city' doesn't exist

mysql> alter table world.city import tablespace;#导入表空间

Query OK, 0 rows affected, 2 warnings (0.10 sec)

mysql> select * from city;

+------+---------------------+-------------+------------------------+------------+

| ID   | Name                               | CountryCode | District               | Population |

+------+---------------------+-------------+------------------------+------------+

|    1 | Kabul                              | AFG         | Kabol                  |    1780000 |

|    2 | Qandahar                           | AFG         | Qandahar               |     237500 |

|    3 | Herat                              | AFG         | Herat                  |     186800 |

|    4 | Mazar-e-Sharif                     | AFG         | Balkh                  |     127800 |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值