transportable tablespace

实验环境:
源端:192.168.99.111
目标端:192.168.99.112
源端test1.actor–>目标端test2.actor

transportable tablespace的应用场景、限制及注意事项,具体请参考官方手册
https://dev.mysql.com/doc/refman/5.7/en/tablespace-copying.html#innodb-transportable-tablespace-limitations

Example 1: Copying an InnoDB Table to Another Instance

1、准备环境

源端:

root@localhost:mysql3306.sock [sakila]>use test1;
Database changed
root@localhost:mysql3306.sock [test1]>show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| test1           |
+-----------------+
1 row in set (0.00 sec)

root@localhost:mysql3306.sock [test1]>CREATE TABLE `actor` (
    ->   `actor_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
    ->   `first_name` varchar(45) NOT NULL,
    ->   `last_name` varchar(45) NOT NULL,
    ->   `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    ->   PRIMARY KEY (`actor_id`),
    ->   KEY `idx_actor_last_name` (`last_name`)
    -> ) ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.01 sec)

root@localhost:mysql3306.sock [test1]>show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| actor           |
| test1           |
+-----------------+
2 rows in set (0.00 sec)

root@localhost:mysql3306.sock [test1]>select * from actor;
Empty set (0.00 sec)

root@localhost:mysql3306.sock [test1]>insert into actor select * from sakila.actor;
Query OK, 200 rows affected (0.00 sec)
Records: 200  Duplicates: 0  Warnings: 0
目标端:

root@localhost:mysql3306.sock [(none)]>show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| menagerie          |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| test1              |
| world              |
| world_x            |
+--------------------+
9 rows in set (0.08 sec)

root@localhost:mysql3306.sock [(none)]>create database test2;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    3
Current database: *** NONE ***

Query OK, 1 row affected (0.01 sec)

root@localhost:mysql3306.sock [(none)]>show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| menagerie          |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| test1              |
| test2              |
| world              |
| world_x            |
+--------------------+
10 rows in set (0.00 sec)

root@localhost:mysql3306.sock [(none)]>use test2;
Database changed
root@localhost:mysql3306.sock [test2]>CREATE TABLE `actor` (
    ->   `actor_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
    ->   `first_name` varchar(45) NOT NULL,
    ->   `last_name` varchar(45) NOT NULL,
    ->   `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    ->   PRIMARY KEY (`actor_id`),
    ->   KEY `idx_actor_last_name` (`last_name`)
    -> ) ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)

root@localhost:mysql3306.sock [test2]>show tables;
+-----------------+
| Tables_in_test2 |
+-----------------+
| actor           |
+-----------------+
1 row in set (0.00 sec)

2、在目标端DISCARD TABLESPACE

[root@centos7 data]# cd /data/mysql/mysql3306/data/test2
[root@centos7 test2]# ll
total 128
-rw-r----- 1 mysql mysql   8694 Jun 19 14:51 actor.frm
-rw-r----- 1 mysql mysql 114688 Jun 19 14:51 actor.ibd
-rw-r----- 1 mysql mysql     61 Jun 19 14:49 db.opt
[root@centos7 test2]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

root@localhost:mysql3306.sock [(none)]>use test2;
No connection. Trying to reconnect...
Connection id:    5
Current database: *** NONE ***

Database changed
root@localhost:mysql3306.sock [test2]>show tables;
+-----------------+
| Tables_in_test2 |
+-----------------+
| actor           |
+-----------------+
1 row in set (0.00 sec)

root@localhost:mysql3306.sock [test2]>alter table actor discard tablespace;
Query OK, 0 rows affected (0.01 sec)

root@localhost:mysql3306.sock [test2]>quit
Bye
[root@centos7 test2]# ll
total 16
-rw-r----- 1 mysql mysql 8694 Jun 19 14:51 actor.frm
-rw-r----- 1 mysql mysql   61 Jun 19 14:49 db.opt

3、在源端FLUSH TABLES … FOR EXPORT

root@localhost:mysql3306.sock [(none)]>use test1;
Database changed
root@localhost:mysql3306.sock [test1]>flush tables actor for export;
Query OK, 0 rows affected (0.01 sec)

注意:执行完此命令后不要退出连接,另打开一个连接执行scp操作

4、从源端把.ibd和.cfg copy到目标端

[root@centos7 ~]# cd /data/mysql/mysql3306/data/test1
[root@centos7 test1]# ll
total 240
-rw-r----- 1 mysql mysql    649 Jun 19 15:05 actor.cfg
-rw-r----- 1 mysql mysql   8694 Jun 19 14:41 actor.frm
-rw-r----- 1 mysql mysql 114688 Jun 19 14:55 actor.ibd
-rw-r----- 1 mysql mysql     61 Jun 18 22:02 db.opt
-rw-r----- 1 mysql mysql   8556 Jun 18 22:02 test1.frm
-rw-r----- 1 mysql mysql  98304 Jun 18 22:02 test1.ibd
[root@centos7 test1]# scp /data/mysql/mysql3306/data/test1/actor.{cfg,ibd} 192.168.99.112:/data/mysql/mysql3306/data/test2/
The authenticity of host '192.168.99.112 (192.168.99.112)' can't be established.
ECDSA key fingerprint is a5:2a:57:b4:91:c7:f3:b5:d0:0d:b3:fb:ef:c7:31:b8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.99.112' (ECDSA) to the list of known hosts.
root@192.168.99.112's password: 
actor.cfg                                                                                                                                                    100%  649     0.6KB/s   00:00    
actor.ibd                                                                                                                                                    100%  112KB 112.0KB/s   00:00    

5、在源端执行unlock tables释放锁

root@localhost:mysql3306.sock [(none)]>use test1;
Database changed
root@localhost:mysql3306.sock [test1]>unlock tables;
Query OK, 0 rows affected (0.00 sec)

6、在目标端import tablespace

[root@centos7 test2]# ll
total 132
-rw-r----- 1 root  root     649 Jun 19 15:08 actor.cfg
-rw-r----- 1 mysql mysql   8694 Jun 19 14:51 actor.frm
-rw-r----- 1 root  root  114688 Jun 19 15:08 actor.ibd
-rw-r----- 1 mysql mysql     61 Jun 19 14:49 db.opt
[root@centos7 test2]# chown -R mysql:mysql *
[root@centos7 test2]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

root@localhost:mysql3306.sock [(none)]>use test2;
Database changed
root@localhost:mysql3306.sock [test2]>alter table actor import tablespace;
Query OK, 0 rows affected, 1 warning (0.04 sec)

root@localhost:mysql3306.sock [test2]>select count(*) from actor;
+----------+
| count(*) |
+----------+
|      200 |
+----------+
1 row in set (0.00 sec)

对于一个普通的InnoDB表进行传输表空间操作到此就算结束了,很简单实用,在MySQL5.7版本开始支持分区表,更多示例请参考MySQL官方手册
https://dev.mysql.com/doc/refman/5.7/en/innodb-transportable-tablespace-examples.html

xtrabackup是一种备份工具,可用于对MySQL数据库进行全量或增量备份。在备份过程中,xtrabackup会创建一个完整的数据库备份,包括所有的表和数据。如果需要从xtrabackup备份中恢复单张表,可以使用可移动表空间(Transportable Tablespace)功能来实现。从MySQL 5.6版本开始,支持可移动表空间功能,该功能允许将单个表从一个数据库实例移动到另一个实例。 要从xtrabackup备份中恢复单张表,可以按照以下步骤进行操作: 1. 使用xtrabackup备份整个MySQL数据库。 2. 将备份文件解压到指定的目录,并确保解压后的备份文件包含了所有表的数据和结构。 3. 使用MySQL的物理备份工具innobackupex来恢复备份文件。这个工具可以将备份文件还原到指定的目录。 4. 在还原过程中,可以选择只恢复需要的表。可以将需要恢复的表复制到新的数据库实例中,或者使用可移动表空间(Transportable Tablespace)功能将表从备份中移动到现有的数据库实例中。 5. 如果备份后的数据中有插入操作,但在恢复过程中没有出现,请使用binlog进行恢复。binlog是MySQL的二进制日志,可以用于恢复备份后的插入操作。 请注意,以上步骤仅适用于使用xtrabackup进行备份的MySQL数据库,且需要使用MySQL 5.6版本或更高版本支持可移动表空间功能。有关更详细的步骤和示例,请参考中提供的链接。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [从xtrabackup备份恢复单表](https://blog.csdn.net/yimenglin/article/details/106902717)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值