0
点赞
收藏
分享

微信扫一扫

.ibd迁移数据

概述

从MySQL 5.6版本开始,引入了传输表空间这个功能,可以把一张表从一个数据库移到另一个数据库或者另一台机器上。在做数据迁移时,非常方便,尤其是针对一张数据量很大的表来说。相比mysqldump的方式,表空间传输要快很多,而且更加灵活。
当然如果想使用表空间传输的功能,也必须满足以下几个条件:

(1) MySQL版本必须是5.6及以上的版本。

(2)使用独立表空间方式,本次实验使用的5.7.20,默认开启innodb_file_per_table。

(3)源库与目标库之间的page size必须一致。

(4)当表做导出操作时,该表只能进行只读操作。

测试目标

目标:把test库下面的EMP这张表的数据传输到test2库下EMP表中,test库下EMP表中的数据如下:

root@db 23:02:  [test]> select * from EMP;
+-------+-------+-------+------+---------------------+------+------+--------+
| EMPNO | ENAME | JOB   | MGR  | HIREDATE            | SAL  | COMM | DEPTNO |
+-------+-------+-------+------+---------------------+------+------+--------+
|  7876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100 | NULL |     20 |
|  7900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 |  950 | NULL |     30 |
+-------+-------+-------+------+---------------------+------+------+--------+
2 rows in set (0.00 sec)

测试过程

首先,创建test2库,并在test2库下创建一张与test库下EMP表结构一样的EMP表:

root@db 23:08:  [(none)]> create database test2;
Query OK, 1 row affected (0.01 sec)

root@db 23:09:  [(none)]> use test2;
Database changed

root@db 23:09:  [test2]> CREATE TABLE `EMP` (
    ->   `EMPNO` int(10) NOT NULL,
    ->   `ENAME` varchar(10) DEFAULT NULL,
    ->   `JOB` varchar(9) DEFAULT NULL,
    ->   `MGR` int(10) DEFAULT NULL,
    ->   `HIREDATE` datetime DEFAULT NULL,
    ->   `SAL` int(10) DEFAULT NULL,
    ->   `COMM` int(10) DEFAULT NULL,
    ->   `DEPTNO` int(10) DEFAULT NULL,
    ->   PRIMARY KEY (`EMPNO`),
    ->   KEY `FK_DEPTNO` (`DEPTNO`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.07 sec)

root@db 23:09:  [test2]> desc EMP;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| EMPNO    | int(10)     | NO   | PRI | NULL    |       |
| ENAME    | varchar(10) | YES  |     | NULL    |       |
| JOB      | varchar(9)  | YES  |     | NULL    |       |
| MGR      | int(10)     | YES  |     | NULL    |       |
| HIREDATE | datetime    | YES  |     | NULL    |       |
| SAL      | int(10)     | YES  |     | NULL    |       |
| COMM     | int(10)     | YES  |     | NULL    |       |
| DEPTNO   | int(10)     | YES  | MUL | NULL    |       |
+----------+-------------+------+-----+---------+-------+
8 rows in set (0.00 sec)


在test2数据目录下,能看到生成了 EMP表的文件信息:

[root@source test2]# pwd
/data/mysql/test2
[root@source test2]# 
[root@source test2]# ll
total 128
-rw-r----- 1 mysql mysql     65 Jul  5 23:09 db.opt
-rw-r----- 1 mysql mysql   8780 Jul  5 23:09 EMP.frm
-rw-r----- 1 mysql mysql 114688 Jul  5 23:09 EMP.ibd

 

接下来,需要卸载test2库下EMP表的表空间:

alter table EMP discard tablespace;

root@db 23:13:  [test2]> alter table EMP discard tablespace;
Query OK, 0 rows affected (0.01 sec)

[root@source test2]# ll
total 16
-rw-r----- 1 mysql mysql   65 Jul  5 23:09 db.opt
-rw-r----- 1 mysql mysql 8780 Jul  5 23:09 EMP.frm

结果发现数据目录test2库下EMP.ibd文件被删除了。

注意:
卸载表空间时要注意,一定要在目标库test2下操作,而且是在数据库命令行下,执行alter table table_name discard tablespace命令完成的。绝对不是在系统层面进行rm操作,这里一定要切记。

然后在test库下执行表空间导出的操作:

flush table EMP for export;

锁表

root@db 23:16:  [test]> flush table EMP for export;
Query OK, 0 rows affected (0.00 sec)

将test库下的EMP.ibd文件复制到test2库下,并修改mysql权限:

[root@source test]# pwd
/data/mysql/test
[root@source test]# 
[root@source test]# ll
total 32304
-rw-r----- 1 mysql mysql     8648 Jul  5 21:06 BONUS.frm
-rw-r----- 1 mysql mysql    98304 Jul  5 21:06 BONUS.ibd
-rw-r----- 1 mysql mysql       65 Jul  5 21:06 db.opt
-rw-r----- 1 mysql mysql     8624 Jul  5 21:06 DEPT.frm
-rw-r----- 1 mysql mysql    98304 Jul  5 21:06 DEPT.ibd
-rw-r----- 1 mysql mysql     8780 Jul  5 23:34 EMP.frm
-rw-r----- 1 mysql mysql    98304 Jul  5 23:34 EMP.ibd
-rw-r----- 1 mysql mysql     8626 Jul  5 21:06 SALGRADE.frm
-rw-r----- 1 mysql mysql    98304 Jul  5 21:06 SALGRADE.ibd
-rw-r----- 1 mysql mysql     8632 Jul  5 21:41 sbtest.frm
-rw-r----- 1 mysql mysql 32505856 Jul  5 21:41 sbtest.ibd
[root@source test]# 
[root@source test]# cp EMP.ibd /data/mysql/test2
[root@source test]# 
[root@source test]# cd /data/mysql/test2
[root@source test2]# 
[root@source test2]# ll
total 220
-rw-r----- 1 mysql mysql    65 Jul  5 23:09 db.opt
-rw-r----- 1 mysql mysql  8780 Jul  5 23:09 EMP.frm
-rw-r----- 1 root  root  98304 Jul  6 08:42 EMP.ibd
[root@source test2]# 
[root@source test2]# chown mysql:mysql EMP.ibd
[root@source test2]# 
[root@source test2]# ll
total 220
-rw-r----- 1 mysql mysql    65 Jul  5 23:09 db.opt
-rw-r----- 1 mysql mysql  8780 Jul  5 23:09 EMP.frm
-rw-r----- 1 mysql mysql 98304 Jul  6 08:42 EMP.ibd

 

权限授完之后,因为test库目前处于只读操作,为了不影响test库的使用,所以需要执行解锁操作:UNLOCK TABLES;
执行flush table EMP for export会锁表,要UNLOCK TABLES或者退出会话,就可以解锁。

在test2库下,执行表空间导入操作:

alter table EMP import tablespace;

root@db 08:59:  [test2]> alter table EMP import tablespace;
Query OK, 0 rows affected, 2 warnings (0.06 sec)

最终,发现己经成功把test库下EMP的数据传输到test2库下的EMP表中。

root@db 08:59:  [test2]> select * from EMP;
+-------+-------+-------+------+---------------------+------+------+--------+
| EMPNO | ENAME | JOB   | MGR  | HIREDATE            | SAL  | COMM | DEPTNO |
+-------+-------+-------+------+---------------------+------+------+--------+
|  7876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100 | NULL |     20 |
|  7900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 |  950 | NULL |     30 |
+-------+-------+-------+------+---------------------+------+------+--------+
2 rows in set (0.00 sec)

举报

相关推荐

0 条评论