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.
 

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. maven 如何将自己的jar包添加到本地仓库

    1 准备一个需要添加到本地仓库的jar包 我这里准备了一个名为mail.jar 的jar包,放到E:\Install Files目录下面 2 下面演示如何将准备的jar包添加到本地仓库 1 语法 mv ...

  2. Js中parseFloat()方法所产的精度不一致问题

    <script language="javascript"> function checkForm(){ var Sum="0.11"; var S ...

  3. 创建Graphics对象与Pen对象

    Graphics对象表示GDI+绘图表面,是用于创建图形图像的对象,所以要通过GDI+创建绘图,必须先创建Graphics对象,然后才可以使用GDI+的笔.刷等结合颜色.字体等对象进行绘制线条形状.填 ...

  4. Geek to Live: Set up your personal Wikipedia

    http://lifehacker.com/163707/geek-to-live--set-up-your-personal-wikipedia Filed to: Wikipedia Captur ...

  5. CSS选择器备忘录

    CSS选择器备忘录 基本选择器 Selector Meaning Example 通用选择器 匹配任何元素 * 标签选择器 CSS1中称之为元素选择器,匹配为指定标签的所有元素 div 伪元素选择器 ...

  6. Android Studio 导入 AOSP 源码

    有了 AOSP 源码,接下来就是如何看了,可以直接文本看,可以用 Source Insight,我当然选择 Android Studio,Android Studio 是我熟悉且十分强大的工具.问题来 ...

  7. springboot文件上传: 单个文件上传 和 多个文件上传

    单个文件上传 //文件上传统一处理 @RequestMapping(value = "/upload",method=RequestMethod.POST) @ResponseBo ...

  8. Spring Cloud入门程序

    本文手把手教你,做出第一个Spring Cloud程序,Eureka的简单入门使用 1.创建Spring Starter Project工程 点击next,添加项目名 2.引入Spring Cloud ...

  9. 必须夸夸Sublime,大文件打开

    今天有个问题的事情日志文件67.8M大文件打开问题开始: 1.vscode必须挨批:直接就给个错误the file cannt be displayed in the editor because i ...

  10. 笨办法学Python(十)

    习题 10: 那是什么? 在习题 9 中我你接触了一些新东西.我让你看到两种让字符串扩展到多行的方法.第一种方法是在月份之间用 \n (back-slash n )隔开.这两个字符的作用是在该位置上放 ...