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).
 (root@localhost mysql3306.sock)[sysbench]>show tables;
+--------------------+
| Tables_in_sysbench |
+--------------------+
| sbtest1 |
| sbtest10 |
| sbtest2 |
| sbtest3 |
| sbtest4 |
| sbtest5 |
| sbtest6 |
| sbtest7 |
| sbtest8 |
| sbtest9 |
+--------------------+
rows in set (0.00 sec) (root@localhost mysql3306.sock)[sysbench]>show create table sbtest2\G
*************************** . row ***************************
Table: sbtest2
Create Table: CREATE TABLE `sbtest2` (
`id` int() NOT NULL AUTO_INCREMENT,
`k` int() NOT NULL DEFAULT '',
`c` char() NOT NULL DEFAULT '',
`pad` char() NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k_2` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf8
row in set (0.00 sec) (root@localhost mysql3306.sock)[sysbench]>select count(*) from sbtest2;
+----------+
| count(*) |
+----------+
| |
+----------+
row in set (0.07 sec) (root@localhost mysql3306.sock)[sysbench]>show variables like '%innodb_file_per_table%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_file_per_table | ON |
+-----------------------+-------+
row in set (0.00 sec)
Create the structure of table sbtest2 in database "tt” of target instance.
 (root@localhost mysql3306.sock)[tt]>CREATE TABLE `sbtest2` (
-> `id` int() NOT NULL AUTO_INCREMENT,
-> `k` int() NOT NULL DEFAULT '',
-> `c` char() NOT NULL DEFAULT '',
-> `pad` char() NOT NULL DEFAULT '',
-> PRIMARY KEY (`id`),
-> KEY `k_2` (`k`)
-> ) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf8;
Query OK, rows affected (0.02 sec) (root@localhost mysql3306.sock)[tt]>select count(*) from sbtest2;
+----------+
| count(*) |
+----------+
| |
+----------+
row in set (0.00 sec) (root@localhost mysql3306.sock)[tt]>show variables like '%innodb_file_per_table%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_file_per_table | ON |
+-----------------------+-------+
row in set (0.00 sec)
Detach the tablespace of table "sbtest2"  on target.
 (root@localhost mysql3306.sock)[zlm]>alter table sbtest2 discard tablespace;
Query OK, rows affected (0.00 sec) [root@zlm3 :: /data/mysql/mysql3306/data/tt]
#ls -l
total
-rw-r----- mysql mysql Jul : db.opt
-rw-r----- mysql mysql Jul : sbtest2.frm //The sbtest2.ibd file has been deleted.
Flush the "sbtest2" table on source.
 (root@localhost mysql3306.sock)[sysbench]>flush table sbtest2 for export;
Query OK, rows affected (0.00 sec) [root@zlm2 :: /data/mysql/mysql3306/data/sysbench]
#ls -l|grep sbtest2
-rw-r----- mysql mysql Jul : sbtest2.cfg
-rw-r----- mysql mysql Jul : sbtest2.frm
-rw-r----- mysql mysql Jul : sbtest2.ibd //A .cfg file has been created now. --05T08::.515902Z [Note] InnoDB: Sync to disk of `sysbench`.`sbtest2` started.
--05T08::.515929Z [Note] InnoDB: Stopping purge
--05T08::.516147Z [Note] InnoDB: Writing table metadata to './sysbench/sbtest2.cfg'
--05T08::.516276Z [Note] InnoDB: Table `sysbench`.`sbtest2` flushed to disk //error log shows the information after flush operation.
//table metadata has been written into the .cfg file.
Copy .ibd & .cfg file to target.
 [root@zlm2 :: /data/mysql/mysql3306/data/sysbench]
#scp sbtest2.{ibd,cfg} zlm3:/data/mysql/mysql3306/data/tt/
root@zlm3's password:
sbtest2.ibd % 29MB .0MB/s :
sbtest2.cfg % .6KB/s :
Release the lock resources on source instance.
 (root@localhost mysql3306.sock)[sysbench]>unlock tables;
Query OK, rows affected (0.00 sec) [root@zlm2 :: /data/mysql/mysql3306/data/sysbench]
#ls -l|grep sbtest2
-rw-r----- mysql mysql Jul : sbtest2.frm
-rw-r----- mysql mysql Jul : sbtest2.ibd --05T08::.256442Z [Note] InnoDB: Deleting the meta-data file './sysbench/sbtest2.cfg'
--05T08::.256458Z [Note] InnoDB: Resuming purge //The .cfg file will be deleted after execute "unlock tables;"
Check the files "sbtest2" table needs and give it the mysql privileges.
 [root@zlm3 :: /data/mysql/mysql3306/data/tt]
#ls -l
total
-rw-r----- mysql mysql Jul : db.opt
-rw-r----- root root Jul : sbtest2.cfg
-rw-r----- mysql mysql Jul : sbtest2.frm
-rw-r----- root root Jul : sbtest2.ibd //change the root.root to mysql.mysql [root@zlm3 :: /data/mysql/mysql3306/data/tt]
#chown mysql.mysql sbtest2.* [root@zlm3 :: /data/mysql/mysql3306/data/tt]
#ls -l
total
-rw-r----- mysql mysql Jul : db.opt
-rw-r----- mysql mysql Jul : sbtest2.cfg
-rw-r----- mysql mysql Jul : sbtest2.frm
-rw-r----- mysql mysql Jul : sbtest2.ibd
Import the tablespace of "sbtest2" table.
 (root@localhost mysql3306.sock)[tt]>alter table sbtest2 import tablespace;
Query OK, rows affected, warning (2.68 sec) (root@localhost mysql3306.sock)[tt]>show tables;
+--------------+
| Tables_in_tt |
+--------------+
| sbtest2 |
+--------------+
row in set (0.00 sec) (root@localhost mysql3306.sock)[tt]>select count(*) from sbtest2;
+----------+
| count(*) |
+----------+
| |
+----------+
row in set (0.06 sec) --05T08::.820441Z [Note] InnoDB: Importing tablespace for table 'sysbench/sbtest2' that was exported from host 'zlm2'
--05T08::.820441Z [Note] InnoDB: Phase I - Update all pages
--05T08::.859485Z [Note] InnoDB: Sync to disk
--05T08::.936351Z [Note] InnoDB: Sync to disk - done!
--05T08::.962775Z [Note] InnoDB: Phase III - Flush changes to disk
--05T08::.975519Z [Note] InnoDB: Phase IV - Flush complete
--05T08::.975722Z [Note] InnoDB: `tt`.`sbtest2` autoinc value set to //The error log shows details of this import operation.
If you detach an inexistent tablespace,it will show below errors.
 (root@localhost mysql3306.sock)[tt]>alter table sbtest2 discard tablespace;
Query OK, rows affected (0.01 sec) (root@localhost mysql3306.sock)[tt]>alter table sbtest2 discard tablespace;
Query OK, rows affected, warning (0.00 sec) (root@localhost mysql3306.sock)[tt]>show warnings;
+---------+------+-----------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------+
| Warning | | InnoDB: Tablespace is missing for table tt/sbtest2. |
+---------+------+-----------------------------------------------------+
row in set (0.00 sec) --05T08::.055225Z [ERROR] InnoDB: Cannot delete tablespace because it is not found in the tablespace memory cache.
--05T08::.055226Z [Warning] InnoDB: Cannot delete tablespace in DISCARD TABLESPACE: Tablespace not found //error log shows the ERROR & Warning because of the .ibd file has been deleted in first discard operation.
Copy those files to target again.
 [root@zlm2 :: /data/mysql/mysql3306/data/sysbench]
#scp sbtest2.{ibd,cfg} zlm3:/data/mysql/mysql3306/data/tt/
root@zlm3's password:
sbtest2.ibd % 29MB .0MB/s :
sbtest2.cfg: No such file or directory //Because of "unlock tables" operation,the .cfg file has gone now.
Import the "sbtest2" tablespace again.
 (root@localhost mysql3306.sock)[tt]>alter table sbtest2 import tablespace;
Query OK, rows affected, warning (2.34 sec) (root@localhost mysql3306.sock)[tt]>show warnings;
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | | InnoDB: IO Read error: (, No such file or directory) Error opening './tt/sbtest2.cfg', will attempt to import without schema verification |
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------+
row in set (0.00 sec) //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.
 

最新文章

  1. 【python】类(资料+疑惑)
  2. HTML5之语义标签
  3. C#操作Excel的函数
  4. Codeforces 721D [贪心]
  5. SQLSERVER复制优化之一《改变包大小》
  6. c# 了解委托
  7. PowerDesigner之PDM(物理概念模型)
  8. C#操作FTP, FTPHelper和SFTPHelper
  9. HBAO
  10. PHP AJAXFORM提交图片上传并显示图片源代码
  11. WinForm打印之页边距
  12. shell脚本编写汇集
  13. 删除字符串中多余的空白字符和空行(C语言实现)
  14. 【Perl学习笔记】1.perl的ref 函数
  15. Android -- 仿小红书欢迎界面
  16. iKcamp出品微信小程序教学共5章16小节汇总(含视频)
  17. 1、话说linux内核
  18. 了解甚少的GNU C的__attribute__ 机制
  19. 03-openldap服务端安装配置
  20. a标签下载;页面传参row对象先转换成字符串。

热门文章

  1. 我使用的brackets插件
  2. JS获取元素属性、样式getComputedStyle()和currentStyle方法兼容性问题
  3. Asp.Net Mvc: 应用BindAttribute
  4. 自定义input文件上传 file的提示文字及样式
  5. 一、简单gridview列表展示
  6. 阿里云短信验证~JAVA后台
  7. [Cuckoo SandBox]注入原理篇
  8. hdu 4123 树形DP+单调队列
  9. hibernate课程 初探一对多映射2-1 一对多映射简介
  10. 关于使用Github,gitignore不起作用的解决方法