#生成测试数据
server1:
mysql> CREATE TABLE test (c1 INT PRIMARY KEY)engine = innodb;
Query OK, 0 rows affected (0.02 sec)
mysql> insert into test values(1),(2),(3);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
server2:
mysql> CREATE TABLE test (c1 INT PRIMARY KEY)engine = innodb;
Query OK, 0 rows affected (0.02 sec)
mysql> insert into test values(1),(3);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
#接着把server2的表空间给discard掉:
root@zjy:/var/lib/mysql/dba_test# ls -lh test.*
-rw-rw---- 1 mysql mysql 8.4K 8月 21 01:37 test.frm
-rw-rw---- 1 mysql mysql 96K 8月 21 01:37 test.ibd
#discard 表空间
mysql> ALTER TABLE test DISCARD TABLESPACE;
Query OK, 0 rows affected (0.00 sec)
#表空间没了
root@zjy:/var/lib/mysql/dba_test# ls -lh test.*
-rw-rw---- 1 mysql mysql 8.4K 8月 21 01:37 test.frm
#然后把server1的表空间复制给server2:
mysql> FLUSH TABLES test FOR EXPORT; #必须要生成cfg metadata文件
Query OK, 0 rows affected (0.00 sec)
root@zjy:/var/lib/mysql3/dba_test# ls -lh test.*
-rw-rw---- 1 mysql mysql 330 8月 21 01:40 test.cfg
-rw-rw---- 1 mysql mysql 8.4K 8月 21 01:36 test.frm
-rw-rw---- 1 mysql mysql 96K 8月 21 01:36 test.ibd
#需要把ibd和cfg文件复制到server2上
root@zjy:/var/lib/mysql3/dba_test# cp test.cfg /var/lib/mysql/dba_test/
root@zjy:/var/lib/mysql3/dba_test# cp test.ibd /var/lib/mysql/dba_test/
#最后修改server2文件权限和import表空间:
root@zjy:/var/lib/mysql/dba_test# chown -R mysql.mysql *
mysql> ALTER TABLE test IMPORT TABLESPACE;
Query OK, 0 rows affected (0.02 sec)
#值从1,3变成了1,2,3。数据已成功迁移过来
mysql> select * from test;
+----+
| c1 |
+----+
| 1 |
| 2 |
| 3 |
+----+
#最后解锁server1上的锁:
mysql> unlock tables;