Preface
 
    There're many ways in backing up or migrating data from one server to another one.Logically,we can use mysqldump,mydumper,mypump to do that kind of job.Physically,we can use Xtrabackup even cold copy way.What I'm gonna introduce is a special method to transmit data between MySQL servers which called “transportable tablespace”.
 
Introduction
 
    What's transportable tablespace?It is supported only on innodb engine and based on export/import grammer of "alter table ... ;" clause since MySQL 5.6.6 version.As we all know,innodb supports putting data of tables in their own tablespaces instead of shared system tablespace by setting parameter "innodb_file_per_table=1".It's different from the conception of oracle database.Business tables in oracle can be stored togerther with each other in the same tablespace while MySQL oblying the rule of "one table one ibd".That is,these ".ibd" files is what we really need to transport.
 
Scenarios
  • Transport a single table to report server without influencing loads on product.
  • Transport a single table to slave server for correcting the replication errors about the table.
  • Transport a single table to better storages such as ssd device for special purpose.
  • Restore a big table efficiently and swiftly as mysqldump needs to reinsert data and rebuild indexes.
 
Limitations
  • "innodb_file_per_table" should be set to "on"(the same to slave server if in replication structure).
  • Page size on instance of target server should be same as the one on source server.
  • It doesn't support partition table and tables which contains fulltext indexes.
  • "foreign_key_checks" should be set to "0" if there's a paraent-child relationship in a table.
  • It doesn't check the foreign key constraints when importing,so all relevant tables should be exported at the same time.
  • Target instance must has the same version of series with the source instance.
  • it's recommended to set "lower_case_table" to "1" to avoid import problems.
Example
 
Check the table which you want to transport first(eg. ”sbtest2” in database "sysbench" here).
  1. (root@localhost mysql3306.sock)[sysbench]>show tables;
  2. +--------------------+
  3. | Tables_in_sysbench |
  4. +--------------------+
  5. | sbtest1 |
  6. | sbtest10 |
  7. | sbtest2 |
  8. | sbtest3 |
  9. | sbtest4 |
  10. | sbtest5 |
  11. | sbtest6 |
  12. | sbtest7 |
  13. | sbtest8 |
  14. | sbtest9 |
  15. +--------------------+
  16. rows in set (0.00 sec)
  17.  
  18. (root@localhost mysql3306.sock)[sysbench]>show create table sbtest2\G
  19. *************************** . row ***************************
  20. Table: sbtest2
  21. Create Table: CREATE TABLE `sbtest2` (
  22. `id` int() NOT NULL AUTO_INCREMENT,
  23. `k` int() NOT NULL DEFAULT '',
  24. `c` char() NOT NULL DEFAULT '',
  25. `pad` char() NOT NULL DEFAULT '',
  26. PRIMARY KEY (`id`),
  27. KEY `k_2` (`k`)
  28. ) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf8
  29. row in set (0.00 sec)
  30.  
  31. (root@localhost mysql3306.sock)[sysbench]>select count(*) from sbtest2;
  32. +----------+
  33. | count(*) |
  34. +----------+
  35. | |
  36. +----------+
  37. row in set (0.07 sec)
  38.  
  39. (root@localhost mysql3306.sock)[sysbench]>show variables like '%innodb_file_per_table%';
  40. +-----------------------+-------+
  41. | Variable_name | Value |
  42. +-----------------------+-------+
  43. | innodb_file_per_table | ON |
  44. +-----------------------+-------+
  45. row in set (0.00 sec)
Create the structure of table sbtest2 in database "tt” of target instance.
  1. (root@localhost mysql3306.sock)[tt]>CREATE TABLE `sbtest2` (
  2. -> `id` int() NOT NULL AUTO_INCREMENT,
  3. -> `k` int() NOT NULL DEFAULT '',
  4. -> `c` char() NOT NULL DEFAULT '',
  5. -> `pad` char() NOT NULL DEFAULT '',
  6. -> PRIMARY KEY (`id`),
  7. -> KEY `k_2` (`k`)
  8. -> ) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf8;
  9. Query OK, rows affected (0.02 sec)
  10.  
  11. (root@localhost mysql3306.sock)[tt]>select count(*) from sbtest2;
  12. +----------+
  13. | count(*) |
  14. +----------+
  15. | |
  16. +----------+
  17. row in set (0.00 sec)
  18.  
  19. (root@localhost mysql3306.sock)[tt]>show variables like '%innodb_file_per_table%';
  20. +-----------------------+-------+
  21. | Variable_name | Value |
  22. +-----------------------+-------+
  23. | innodb_file_per_table | ON |
  24. +-----------------------+-------+
  25. row in set (0.00 sec)
Detach the tablespace of table "sbtest2"  on target.
  1. (root@localhost mysql3306.sock)[zlm]>alter table sbtest2 discard tablespace;
  2. Query OK, rows affected (0.00 sec)
  3.  
  4. [root@zlm3 :: /data/mysql/mysql3306/data/tt]
  5. #ls -l
  6. total
  7. -rw-r----- mysql mysql Jul : db.opt
  8. -rw-r----- mysql mysql Jul : sbtest2.frm //The sbtest2.ibd file has been deleted.
Flush the "sbtest2" table on source.
  1. (root@localhost mysql3306.sock)[sysbench]>flush table sbtest2 for export;
  2. Query OK, rows affected (0.00 sec)
  3.  
  4. [root@zlm2 :: /data/mysql/mysql3306/data/sysbench]
  5. #ls -l|grep sbtest2
  6. -rw-r----- mysql mysql Jul : sbtest2.cfg
  7. -rw-r----- mysql mysql Jul : sbtest2.frm
  8. -rw-r----- mysql mysql Jul : sbtest2.ibd //A .cfg file has been created now.
  9.  
  10. --05T08::.515902Z [Note] InnoDB: Sync to disk of `sysbench`.`sbtest2` started.
  11. --05T08::.515929Z [Note] InnoDB: Stopping purge
  12. --05T08::.516147Z [Note] InnoDB: Writing table metadata to './sysbench/sbtest2.cfg'
  13. --05T08::.516276Z [Note] InnoDB: Table `sysbench`.`sbtest2` flushed to disk
  14.  
  15. //error log shows the information after flush operation.
  16. //table metadata has been written into the .cfg file.
Copy .ibd & .cfg file to target.
  1. [root@zlm2 :: /data/mysql/mysql3306/data/sysbench]
  2. #scp sbtest2.{ibd,cfg} zlm3:/data/mysql/mysql3306/data/tt/
  3. root@zlm3's password:
  4. sbtest2.ibd % 29MB .0MB/s :
  5. sbtest2.cfg % .6KB/s :
Release the lock resources on source instance.
  1. (root@localhost mysql3306.sock)[sysbench]>unlock tables;
  2. Query OK, rows affected (0.00 sec)
  3.  
  4. [root@zlm2 :: /data/mysql/mysql3306/data/sysbench]
  5. #ls -l|grep sbtest2
  6. -rw-r----- mysql mysql Jul : sbtest2.frm
  7. -rw-r----- mysql mysql Jul : sbtest2.ibd
  8.  
  9. --05T08::.256442Z [Note] InnoDB: Deleting the meta-data file './sysbench/sbtest2.cfg'
  10. --05T08::.256458Z [Note] InnoDB: Resuming purge
  11.  
  12. //The .cfg file will be deleted after execute "unlock tables;"
Check the files "sbtest2" table needs and give it the mysql privileges.
  1. [root@zlm3 :: /data/mysql/mysql3306/data/tt]
  2. #ls -l
  3. total
  4. -rw-r----- mysql mysql Jul : db.opt
  5. -rw-r----- root root Jul : sbtest2.cfg
  6. -rw-r----- mysql mysql Jul : sbtest2.frm
  7. -rw-r----- root root Jul : sbtest2.ibd
  8.  
  9. //change the root.root to mysql.mysql
  10.  
  11. [root@zlm3 :: /data/mysql/mysql3306/data/tt]
  12. #chown mysql.mysql sbtest2.*
  13.  
  14. [root@zlm3 :: /data/mysql/mysql3306/data/tt]
  15. #ls -l
  16. total
  17. -rw-r----- mysql mysql Jul : db.opt
  18. -rw-r----- mysql mysql Jul : sbtest2.cfg
  19. -rw-r----- mysql mysql Jul : sbtest2.frm
  20. -rw-r----- mysql mysql Jul : sbtest2.ibd
Import the tablespace of "sbtest2" table.
  1. (root@localhost mysql3306.sock)[tt]>alter table sbtest2 import tablespace;
  2. Query OK, rows affected, warning (2.68 sec)
  3.  
  4. (root@localhost mysql3306.sock)[tt]>show tables;
  5. +--------------+
  6. | Tables_in_tt |
  7. +--------------+
  8. | sbtest2 |
  9. +--------------+
  10. row in set (0.00 sec)
  11.  
  12. (root@localhost mysql3306.sock)[tt]>select count(*) from sbtest2;
  13. +----------+
  14. | count(*) |
  15. +----------+
  16. | |
  17. +----------+
  18. row in set (0.06 sec)
  19.  
  20. --05T08::.820441Z [Note] InnoDB: Importing tablespace for table 'sysbench/sbtest2' that was exported from host 'zlm2'
  21. --05T08::.820441Z [Note] InnoDB: Phase I - Update all pages
  22. --05T08::.859485Z [Note] InnoDB: Sync to disk
  23. --05T08::.936351Z [Note] InnoDB: Sync to disk - done!
  24. --05T08::.962775Z [Note] InnoDB: Phase III - Flush changes to disk
  25. --05T08::.975519Z [Note] InnoDB: Phase IV - Flush complete
  26. --05T08::.975722Z [Note] InnoDB: `tt`.`sbtest2` autoinc value set to
  27.  
  28. //The error log shows details of this import operation.
If you detach an inexistent tablespace,it will show below errors.
  1. (root@localhost mysql3306.sock)[tt]>alter table sbtest2 discard tablespace;
  2. Query OK, rows affected (0.01 sec)
  3.  
  4. (root@localhost mysql3306.sock)[tt]>alter table sbtest2 discard tablespace;
  5. Query OK, rows affected, warning (0.00 sec)
  6.  
  7. (root@localhost mysql3306.sock)[tt]>show warnings;
  8. +---------+------+-----------------------------------------------------+
  9. | Level | Code | Message |
  10. +---------+------+-----------------------------------------------------+
  11. | Warning | | InnoDB: Tablespace is missing for table tt/sbtest2. |
  12. +---------+------+-----------------------------------------------------+
  13. row in set (0.00 sec)
  14.  
  15. --05T08::.055225Z [ERROR] InnoDB: Cannot delete tablespace because it is not found in the tablespace memory cache.
  16. --05T08::.055226Z [Warning] InnoDB: Cannot delete tablespace in DISCARD TABLESPACE: Tablespace not found
  17.  
  18. //error log shows the ERROR & Warning because of the .ibd file has been deleted in first discard operation.
Copy those files to target again.
  1. [root@zlm2 :: /data/mysql/mysql3306/data/sysbench]
  2. #scp sbtest2.{ibd,cfg} zlm3:/data/mysql/mysql3306/data/tt/
  3. root@zlm3's password:
  4. sbtest2.ibd % 29MB .0MB/s :
  5. sbtest2.cfg: No such file or directory
  6.  
  7. //Because of "unlock tables" operation,the .cfg file has gone now.
Import the "sbtest2" tablespace again.
  1. (root@localhost mysql3306.sock)[tt]>alter table sbtest2 import tablespace;
  2. Query OK, rows affected, warning (2.34 sec)
  3.  
  4. (root@localhost mysql3306.sock)[tt]>show warnings;
  5. +---------+------+--------------------------------------------------------------------------------------------------------------------------------------------+
  6. | Level | Code | Message |
  7. +---------+------+--------------------------------------------------------------------------------------------------------------------------------------------+
  8. | Warning | | InnoDB: IO Read error: (, No such file or directory) Error opening './tt/sbtest2.cfg', will attempt to import without schema verification |
  9. +---------+------+--------------------------------------------------------------------------------------------------------------------------------------------+
  10. row in set (0.00 sec)
  11.  
  12. //There's a warning about importing without .cfg file which won't impact the result.
Summary
  • Transportable Tablespace(TT) of innodb provids a different way in backing up and restoring a single table between servers.
  • TT merely supports innodb engine which can store data in tablespaces of their own by setting "innodb_file_per_table=1".
  • TT supports importing tablespace without .cfg file what brings about us much convenience in crash recovery.
  • Notice that there will be shared read locks on the tables after execute "flush table ... for export;" what really influences the tables need to be write.
 

MySQL另类的备份恢复方法——innodb可传输表空间的更多相关文章

  1. 从完整备份恢复单个innodb表

    现在大多数同学在线上采取的备份策略都是xtrabackup全备+binlog备份,那么当某天某张表意外的删除那么如何从xtrabackup全备中恢复呢?从mysql 5.6版本开始,支持可移动表空间( ...

  2. mysql InnoDB引擎 共享表空间和独立表空间(转载)

    PS:innodb这种引擎,与MYISAM引擎的区别很大.特别是它的数据存储格式等.对于innodb的数据结构,首先要解决两个概念性的问题: 共享表空间以及独占表空间. 1.什么是共享表空间和独占表空 ...

  3. 从MySQL全备文件中恢复单个库或者单个表

    从MySQL全备文件中恢复单个库或者单个表 提取建库语句 sed -n '/^-- Current Database: db_cms/,/^-- Current Database: `/p' back ...

  4. InnoDB 引擎独立表空间 innodb_file_per_table

    使用过MySQL的同学,刚开始接触最多的莫过于MyISAM表引擎了,这种引擎的数据库会分别创建三个文件:表结构.表索引.表数据空间.我们可以将某个数据库目录直接迁移到其他数据库也可以正常工作.然而当你 ...

  5. InnoDB 引擎独立表空间

    InnoDB 引擎独立表空间   使用过MySQL的同学,刚开始接触最多的莫过于MyISAM表引擎了,这种引擎的数据库会分别创建三个文件:表结构.表索引.表数据空间.我们可以将某个数据库目录直接迁移到 ...

  6. mysql5.6之 传输表空间迁移表或恢复误删除的表

    一,简单说明: 1),传输表空间的限制:  1,mysql 版本 5.6.6 及其以上,并且版本建议源和目标版本建议都是GA版并且大版本一样  2,表引擎为innodb并且开启独立表空间  innod ...

  7. MySQL 5.7新特性之在线收缩undo表空间

    1. MySQL 5.5时代的undo log 在MySQL5.5以及之前,大家会发现随着数据库上线时间越来越长,ibdata1文件(即InnoDB的共享表空间,或者系统表空间)会越来越大,这会造成2 ...

  8. 用备份控制文件做不完全恢复下的完全恢复(全备<老>--备份控制文件<次新>--删除表空间andy--日志文件<新>)

    为什么会使用备份的控制文件? 实际工作中主要有两种情况:第一种:当前控制文件全部损坏,而数据文件备份,控制文件备份及当前日志处于不同SCN版本,它们之间又增加过表空间(数据文件).第二种:当前控制文件 ...

  9. [20170623]利用传输表空间恢复部分数据.txt

    [20170623]利用传输表空间恢复部分数据.txt --//昨天我测试使用传输表空间+dblink,上午补充测试发现表空间设置只读才能执行impdp导入原数据,这个也很好理解.--//这样的操作模 ...

随机推荐

  1. C语言实现通用链表初步(二)

    接着上次的内容,我们继续! 还是无头单向非循环链表.假如要删除某个节点,如何实现? //删除成功返回0,失败返回-1 int slist_del(struct node_info *node, str ...

  2. pat1045. Favorite Color Stripe (30)

    1045. Favorite Color Stripe (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  3. struts2的java.lang.NoSuchMethodException异常处理

    1. 你有没有试试看 其它的方法能不能用,要是都是这种情况的话,可能是你的Action类没有继承structs里面的DispatchAction或者其它的类.还有你注意下方法的参数列表,类型顺序要正确 ...

  4. ANDROID_HOME is not set and "android" command not in your PATH解决

    使用nodejs安装cordova后在项目里面添加平台时出现错误: 原因就是没有配环境变量 使用phonegap开发不仅要配JDK环境变量,还要配ADT环境变量,出现这个错误很显示就是没配ADT环境变 ...

  5. 动态行转列 pivot实现

    declare @sql varchar(8000)    begin              set @sql=''  --初始化变量@sql              select  @sql= ...

  6. Asp.Net中ObjectDataSource控件传参绑定数据

    最近在实习,在上头交付的任务中,由于需要使用Asp.Net的ListView控件,因此必然得就使用了ObjectDataSource控件,由于在使用过程中,需要网页中的参数发送到后台后,运行该参数进行 ...

  7. 浅谈 ECMAScript 和 JavaScript

    ES5与ES3基本保持兼容,较大的语法修正和新功能加入,将由JavaScript.next完成. 什么是ECMAScript?http://baike.baidu.com/link?url=G1T8n ...

  8. Azure 4月新公布

    Azure 4 月新发布:Linux 上的 Azure Service Fabric 公共预览版正式发布:Azure 物联网套件新增设备管理功能:计量名称变更 Linux 上的 Azure Servi ...

  9. php的yii框架开发总结9

    这一篇讲解怎么实现的自动发邮件的功能,我在网上查了很多资料,很多都是用定时检测来实现的,我试过,效率太低,网站也卡了. 后来就写了一个.bat文件来实现刷新页面,用了windows的定时任务定时来运行 ...

  10. 原生Js在各大浏览器上、火狐、ie、谷歌、360等出现的不兼容问题。

    1 document.getElementsByName("name")  在Ie低版本,360普通版本,以及火狐低版本不支持. 2 element.innerText 在低版本的 ...