12月24, 2020
收藏本站

快速迁移InnoDB大表(物理迁移ibd)

问题:如何快速迁移InnoDB大表?mysqldump和复制ibd恢复速度对比。
实验环境:

名称
源数据库 vm-1(192.168.223.100)
目标数据库 vm-2(192.168.223.101)
mysql版本 5.6.50-log(最低要求版本没验证)
待迁移表 test.history
表行数量 1182759
文件大小 124M
  1. 源数据库vm-1导出表结构并刷新表
    vm-1 mysql> use test;
    vm-1 mysql> show create table history\G
    vm-1 mysql> flush table history for export;
  2. 拷贝history的.ibd和.cfg文件到目标数据库vm-2
    [root@vm-1 ~]# scp history.ibd history.cfg root@192.168.223.101:/root/
  3. vm-1解锁
    vm-1 mysql> unlock tables;
  4. 目标库vm-2初始化表
    vm-2 mysql> create table history(...)engine=innodb;
  5. 分离初始化的表空间
    vm-2 mysql> alter table history discard tablespace;
  6. 移动新表空间到vm-2的数据库目录下
    [root@vm-2 ~]# mv /root/history.* /var/lib/mysql/test/
  7. 修改文件所有者(不然导入不了)
    [root@vm-2 ~]# chown mysql:mysql /var/lib/mysql/test/history.*
  8. 导入表空间
    vm-2 mysql> alter table history import tablespace;
  9. 验证源、目标数据库表行数是否一致

整个过程3s左右;而通过mysqldump,再source的方式,则需要20s左右。

注意:

  1. 需要执行flush table xxx for export并拷贝.cfg文件,有的教程直接拷贝.ibd没执行flush,导入的时候会出现下面问题
    InnoDB: IO Read error: (2, No such file or directory) Error opening './test_innodb/history.cfg', will attempt to import without schema verification
  2. 源数据库要开启innodb_file_per_table
    否则执行flush table xxx for export会提示InnoDB: Table '"test"."t_test"' in system tablespace,该表使用的是系统表空间system tablespace
  3. 有时加入cfg文件,还会报ERROR 2013 (HY000): Lost connection to MySQL server during query,那就把.cfg文件删掉,直接执行import tablespace

Comments