深圳全飞鸿

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 570|回复: 2
打印 上一主题 下一主题

ALTER TABLE DISCARD/IMPORT TABLESPACE

[复制链接]

800

主题

1379

帖子

7708

积分

版主

Rank: 7Rank: 7Rank: 7

积分
7708
跳转到指定楼层
楼主
发表于 2019-6-16 17:00:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
ALTER TABLE tbl_name DISCARD TABLESPACE;
For an InnoDB table that is created with its own tablespace in an .ibd file, that file can be discarded and imported. To discard the .ibd file, use this statement:

ALTER TABLE tbl_name DISCARD TABLESPACE;
This deletes the current .ibd file, so be sure that you have a backup first. Attempting to access the table while the tablespace file is discarded results in an error.

To import the backup .ibd file back into the table, copy it into the database directory, and then issue this statement:

ALTER TABLE tbl_name IMPORT TABLESPACE;
The tablespace file must have been created on the server into which it is imported later.

Note
The ALTER TABLE ... IMPORT TABLESPACE feature does not enforce foreign key constraints on imported data.

回复

使用道具 举报

800

主题

1379

帖子

7708

积分

版主

Rank: 7Rank: 7Rank: 7

积分
7708
沙发
 楼主| 发表于 2019-6-16 17:10:39 | 只看该作者
我们在恢复表的时候,要保证row_format和ibd文件的一致,如果ibd文件是compact的话,需要建表的时候,设置成row_format=compact,

我们使用:show table status like 't_dict'\G,查看表的属性
回复 支持 反对

使用道具 举报

800

主题

1379

帖子

7708

积分

版主

Rank: 7Rank: 7Rank: 7

积分
7708
板凳
 楼主| 发表于 2019-6-16 17:11:45 | 只看该作者
如果你有.ibd文件的一个干净的备份,你可以按如下操作从被起源的地方恢复它到MySQL安装中:相当快速

1. 发出这个ALTER TABLE语句:

2. ALTER TABLE tbl_name DISCARD TABLESPACE;
警告:这个语句删除当前.ibd文件。

3. 把备份的.ibd文件放回到恰当的数据库目录。

4. 发出这个ALTER TABLE语句:

5. ALTER TABLE tbl_name IMPORT TABLESPACE;
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|nagomes  

GMT+8, 2025-5-5 13:02 , Processed in 0.026855 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表