表空间迁移
1.目的是不需要逻辑备份与恢复,直接使用拷贝表文件,
例如:school库下面的一个student表
#下载dbsake工具。
~]# curl -s get.dbsake.net > dbsake
~]# chmod u+x dbsake
#查看student表的结构。
~]# ./dbsake frmdump /data/mysql_data/school/student.frm
--
-- Table structure for table `student`
-- Created with MySQL Version 5.7.17
--
CREATE TABLE `student` (
`sno` int(11) NOT NULL AUTO_INCREMENT COMMENT '学号',
`sname` varchar(20) NOT NULL COMMENT '姓名',
`sage` tinyint(3) unsigned NOT NULL COMMENT '年龄',
`ssex` enum('f','m') NOT NULL DEFAULT 'm' COMMENT '性别',
PRIMARY KEY (`sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.在另外一个数据库实例里创建school库。
(root@localhost) [(none)] create database school charset utf8mb4;
(root@localhost) [(none)] use school;
#创建student表。
(root@localhost) [school] CREATE TABLE `student` (
`sno` int(11) NOT NULL AUTO_INCREMENT COMMENT '学号',
`sname` varchar(20) NOT NULL COMMENT '姓名',
`sage` tinyint(3) unsigned NOT NULL COMMENT '年龄',
`ssex` enum('f','m') NOT NULL DEFAULT 'm' COMMENT '性别',
PRIMARY KEY (`sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (1 min 40.06 sec)
#由于创建表后,ibdata01文件里有student的统计信息了,因此只需要把student的idb文件导入进来,但是前提要删除student的新的表空间。
(root@localhost) [school] alter table student discard tablespace;
Query OK, 0 rows affected (0.07 sec)
(root@localhost) [school] show tables;
+------------------+
| Tables_in_school |
+------------------+
| student |
+------------------+
1 row in set (0.00 sec)
#没有数据了。
(root@localhost) [school] select * from student;
ERROR 1814 (HY000): Tablespace has been discarded for table 'student'
#批量清理某个库下面的所有表空间的sql文件。
(root@localhost) [(none)] select concat('alter table ',table_schema,'.',table_name ,' discard tablespace;' ) from information_schema.tables where table_schema = 'school' into outfile '/tmp/school_table_discard.sql';
#执行清理school库下面所有表空间的sql文件
(root@localhost) [(none)] source /tmp/school_table_discard.sql;
#需要导入迁移的student的表ibd文件复制到新的实例的school库里。
~]# cp /tmp/student.ibd /data/mysql_data/school
~]# chown -R mysql.mysql *
#导入student表空间到school库,实现了表空间数据迁移。
(root@localhost) [school] alter table student import tablespace;
Query OK, 0 rows affected, 1 warning (0.08 sec)
(root@localhost) [school] select * from student;
+-----+---------+------+------+
| sno | sname | sage | ssex |
+-----+---------+------+------+
| 1 | zhang3 | 18 | m |
| 2 | zhang4 | 18 | m |
| 3 | li4 | 18 | m |
| 4 | wang5 | 19 | f |
| 5 | zh4 | 18 | m |
| 6 | zhao4 | 18 | m |
| 7 | ma6 | 19 | f |
| 8 | oldboy | 20 | m |
| 9 | oldgirl | 20 | f |
| 10 | oldp | 25 | m |
+-----+---------+------+------+
10 rows in set (0.00 sec)
#批量导入ibd文件数据行跟索引数据信息的sql文件。
(root@localhost) [(none)] select concat('alter table ',table_schema,'.',table_name ,' import tablespace;' ) from information_schema.tables where table_schema = 'school' into outfile '/tmp/school_table_discard.sql';
#批量执行导入ibd文件的sql
(root@localhost) [(none)] source /tmp/school_table_discard.sql;