[2021-06-18]mysql 表使用discard和import的方式迁移

如果想把一个数据库的表迁移到另外一个数据库,或者被迁移的数据库由于某种原因无法打开,那么可以使用discard和import的方式迁移表数据。

1、创建一个新表t4

db01 [(none)]>use gtid
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
db01 [gtid]>create table t4(id int);
Query OK, 0 rows affected (0.01 sec)

db01 [gtid]>insert into t4 values (10);
Query OK, 1 row affected (0.00 sec)

db01 [gtid]>commit;
Query OK, 0 rows affected (0.00 sec)

2、检查ibd文件

[root@QXY gtid]# ls -ltr
total 272
-rw-r-----. 1 mysql mysql    61 Jun 18 11:06 db.opt
-rw-r-----. 1 mysql mysql  8556 Jun 18 11:07 t1.frm
-rw-r-----. 1 mysql mysql 98304 Jun 18 14:25 t1.ibd
-rw-r-----. 1 mysql mysql  8556 Jun 18 15:36 t2.frm
-rw-r-----. 1 mysql mysql     0 Jun 18 15:36 t2.MYD
-rw-r-----. 1 mysql mysql  1024 Jun 18 15:36 t2.MYI
-rw-rw-rw-. 1 mysql mysql     9 Jun 18 16:16 t1.txt
-rw-r-----. 1 mysql mysql  8556 Jun 18 17:15 t3.frm
-rw-r-----. 1 mysql mysql  8556 Jun 18 17:24 t4.frm
-rw-r-----. 1 mysql mysql 98304 Jun 18 17:24 t4.ibd

3、使用discard清除t4.ibd

db01 [gtid]>alter table t4 discard tablespace; 
Query OK, 0 rows affected (0.01 sec)

4、再次查看ibd文件

[root@QXY gtid]# ls -ltr
total 272
-rw-r-----. 1 mysql mysql    61 Jun 18 11:06 db.opt
-rw-r-----. 1 mysql mysql  8556 Jun 18 11:07 t1.frm
-rw-r-----. 1 mysql mysql 98304 Jun 18 14:25 t1.ibd
-rw-r-----. 1 mysql mysql  8556 Jun 18 15:36 t2.frm
-rw-r-----. 1 mysql mysql     0 Jun 18 15:36 t2.MYD
-rw-r-----. 1 mysql mysql  1024 Jun 18 15:36 t2.MYI
-rw-rw-rw-. 1 mysql mysql     9 Jun 18 16:16 t1.txt
-rw-r-----. 1 mysql mysql  8556 Jun 18 17:15 t3.frm
-rw-r-----. 1 mysql mysql  8556 Jun 18 17:24 t4.frm

t4.ibd文件已经被清除

5、在bbb数据库创建一张表结构一样的T4表

db01 [gtid]>use bbb
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
db01 [bbb]>create table t4(id int);
Query OK, 0 rows affected (0.01 sec)

db01 [bbb]>insert into t4 values (100);
Query OK, 1 row affected (0.00 sec)

db01 [bbb]>commit;
Query OK, 0 rows affected (0.00 sec)

6、把bbb数据库下的t4.ibd文件copy到gtid目录,并修改权限

[root@QXY gtid]# cp ../bbb/t4.ibd .
[root@QXY gtid]# ls -ltr
total 272
-rw-r-----. 1 mysql mysql    61 Jun 18 11:06 db.opt
-rw-r-----. 1 mysql mysql  8556 Jun 18 11:07 t1.frm
-rw-r-----. 1 mysql mysql 98304 Jun 18 14:25 t1.ibd
-rw-r-----. 1 mysql mysql  8556 Jun 18 15:36 t2.frm
-rw-r-----. 1 mysql mysql     0 Jun 18 15:36 t2.MYD
-rw-r-----. 1 mysql mysql  1024 Jun 18 15:36 t2.MYI
-rw-rw-rw-. 1 mysql mysql     9 Jun 18 16:16 t1.txt
-rw-r-----. 1 mysql mysql  8556 Jun 18 17:15 t3.frm
-rw-r-----. 1 mysql mysql  8556 Jun 18 17:24 t4.frm
-rw-r-----. 1 root  root  98304 Jun 18 17:25 t4.ibd
[root@QXY gtid]# chown mysql:mysql t4.ibd 
[root@QXY gtid]# 

7、在gtid数据库中import 表t4

db01 [gtid]>alter table t4 import tablespace;
Query OK, 0 rows affected, 1 warning (0.02 sec)

db01 [gtid]>select * from t4;
+------+
| id   |
+------+
|  100 |
+------+
1 row in set (0.00 sec)

db01 [gtid]>

这种方式需要表结构一样,因为表结构元数据是存放在frm文件中的,discard并不会删除frm文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值