前提 innodb_file_per_table 爲on
環境說明:
將testdb庫底下t1表遷移到testdb2庫下
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
| testdb2 |
+--------------------+
8 rows in set (0.00 sec)
mysql> use testdb;
Database changed
mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| t1 |
+------------------+
1 row in set (0.00 sec)
mysql> select count(*) from t1;
+----------+
| count(*) |
+----------+
| 32768 |
+----------+
1 row in set (0.02 sec)
mysql> show create table t1;
+-------+----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`id` int(11) DEFAULT NULL,
`name` varchar(200) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
步驟:
1、在testdb2上創建一張相同的表;
mysql> use testdb2;
Database changed
mysql> CREATE TABLE `t1` (
-> `id` int(11) DEFAULT NULL,
-> `name` varchar(200) DEFAULT NULL
-> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.02 sec)
2、在testdb2上執行:ALTER TABLE t1 DISCARD TABLESPACE;
mysql> ALTER TABLE t1 DISCARD TABLESPACE;
Query OK, 0 rows affected (0.03 sec)
3、在testdb上執行:FLUSH TABLES t1 FOR EXPORT;生成.cfg文件;
mysql> FLUSH TABLES t1 FOR EXPORT;
Query OK, 0 rows affected (0.00 sec)
4、將.ibd文件和.cfg文件拷貝到testdb2;
[root@mytest02 testdb]# cp t1.cfg t1.ibd /var/lib/mysql/testdb2/
5、在testdb執行UNLOCK TABLES;解鎖
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
6、在testdb2執行ALTER TABLE t1 IMPORT TABLESPACE;
mysql> ALTER TABLE t1 IMPORT TABLESPACE;
Query OK, 0 rows affected (0.06 sec)
7、測試
mysql> select count(*) from t1;
+----------+
| count(*) |
+----------+
| 32768 |
+----------+
1 row in set (0.02 sec)
遷移成功。