利用.frm、.ibd恢复数据

时间:2023-03-09 19:46:28
利用.frm、.ibd恢复数据

我们知道启用innodb_file_per_table选项后,单个表(InnoDB引擎)的数据和索引放入单独的文件中(.ibd),建表语句保存在.frm文件中
本文假设192.168.85.132,3306实例无法启动(磁盘还能访问),需要将原实例下的replcrash.py_user表恢复到192.168.85.132,3308实例

一、.frm得到建表语句

mysqlfrm可以读取.frm文件并从该文件中找到表定义数据生成CREATE语句。在大多数情况下,生成的CREATE语句用于在另一个服务器上创建表或进行诊断等。

1.1、操作模式

默认模式,使用--basedir或者--server选项生成一个临时实例,该模式需要指定--port选项给临时实例使用,该端口不能与现有的实例冲突。在读取.frm文件后,临时实例将被关闭,所有的临时文件将被删除。
诊断模式,使用--diagnostic选项。byte-by-byte读取.frm文件,尽可能多的恢复信息。
诊断模式可能遇到的问题
• 不能解读character set/collation values,这会影响到多字节字符列大小
• 不能读取默认值
• 生成的create语句可能会有语法错误
当使用默认模式无法读取文件,或者服务器上没有安装MySQL实例时就使用诊断模式~~

1.2、mysqlfrm安装使用

、安装
# mysql-utilities依赖mysql-connector-python >= 2.0.
[root@ZST1 tools]# wget https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.1.7-1.el6.x86_64.rpm
[root@ZST1 tools]# wget https://dev.mysql.com/get/Downloads/MySQLGUITools/mysql-utilities-1.6.5-1.el6.noarch.rpm
# 安装
[root@ZST1 tools]# yum localinstall mysql-connector-python-2.1.-.el6.x86_64.rpm -y
[root@ZST1 tools]# yum localinstall mysql-utilities-1.6.-.el6.noarch.rpm -y 、使用
# default mode
# --basedir
[root@ZST1 ~]# mysqlfrm --basedir=/usr/local/mysql/ /data/mysql/mysql3306/data/replcrash/py_user.frm --port= --user=root
# --server
[root@ZST1 ~]# mysqlfrm --server=mydba:mysql5719@192.168.85.132: /data/mysql/mysql3308/data/replcrash/py_user.frm --port= --user=root
官方不推荐使用root用户Running spawned server # diagnostic mode
[root@ZST1 ~]# mysqlfrm --diagnostic /data/mysql/mysql3308/data/replcrash/py_user.frm

故障实例OFFLINE状态,建议使用--basedir得到建表语句

1.3、mysqlfrm逻辑(default mode)

在命令行末尾添加 -vvv 可以查看执行详细步骤

[root@ZST1 ~]# mysqlfrm --basedir=/usr/local/mysql/ /data/mysql/mysql3306/data/replcrash/py_user.frm --port= --user=root -vvv
# Checking read access to .frm files
# Creating a temporary datadir = /tmp/tmpktcZWc
# Spawning server with --user=root.
# Starting the spawned server on port ...
# Cloning the MySQL server located at /usr/local/mysql.
# Configuring new instance...
# Locating mysql tools...
# Location of files:
# mysqld: /usr/local/mysql/bin/mysqld
# mysqladmin: /usr/local/mysql/bin/mysqladmin
# Setting up empty database and mysql tables...
--28T09::.383542Z [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
--28T09::.528222Z [Warning] InnoDB: New log files created, LSN=
--28T09::.936974Z [Warning] InnoDB: Creating foreign key constraint system tables.
--28T09::.019189Z [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 925a5f84-ebb2-11e7-a13e-000c29c1025c.
--28T09::.024957Z [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
--28T09::.025657Z [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
# Starting new instance of the server...
# Startup command for new server:
/usr/local/mysql/bin/mysqld --no-defaults --basedir=/usr/local/mysql --datadir=/tmp/tmpktcZWc --pid-file=/tmp/tmpktcZWc/clone.pid --port= --server-id= --socket=/tmp/tmpktcZWc/mysql.sock --tmpdir=/tmp/tmpktcZWc --user=root
# Testing connection to new instance...
--28T09::.346999Z [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
--28T09::.369718Z [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
--28T09::.369837Z [Note] /usr/local/mysql/bin/mysqld (mysqld 5.7.) starting as process ...
--28T09::.387216Z [Note] InnoDB: PUNCH HOLE support available
--28T09::.387287Z [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
--28T09::.387309Z [Note] InnoDB: Uses event mutexes
--28T09::.387334Z [Note] InnoDB: GCC builtin __sync_synchronize() is used for memory barrier
--28T09::.387353Z [Note] InnoDB: Compressed tables use zlib 1.2.
--28T09::.387372Z [Note] InnoDB: Using Linux native AIO
--28T09::.388729Z [Note] InnoDB: Number of pools:
--28T09::.389559Z [Note] InnoDB: Using CPU crc32 instructions
--28T09::.396790Z [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = , chunk size = 128M
--28T09::.413527Z [Note] InnoDB: Completed initialization of buffer pool
--28T09::.419969Z [Note] InnoDB: page_cleaner coordinator priority: -
--28T09::.434005Z [Note] InnoDB: Highest supported file format is Barracuda.
--28T09::.503503Z [Note] InnoDB: Creating shared tablespace for temporary tables
--28T09::.503705Z [Note] InnoDB: Setting file './ibtmp1' size to MB. Physically writing the file full; Please wait ...
--28T09::.110625Z [Note] InnoDB: File './ibtmp1' size is now MB.
--28T09::.113767Z [Note] InnoDB: redo rollback segment(s) found. redo rollback segment(s) are active.
--28T09::.113804Z [Note] InnoDB: non-redo rollback segment(s) are active.
--28T09::.114361Z [Note] InnoDB: Waiting for purge to start
# trying again...
--28T09::.165192Z [Note] InnoDB: 5.7. started; log sequence number
--28T09::.165487Z [Note] Plugin 'FEDERATED' is disabled.
--28T09::.175080Z [Note] InnoDB: Loading buffer pool(s) from /tmp/tmpktcZWc/ib_buffer_pool
--28T09::.176769Z [Warning] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key
--28T09::.176801Z [Note] Server hostname (bind-address): '*'; port:
--28T09::.176922Z [Note] IPv6 is available.
--28T09::.176941Z [Note] - '::' resolves to '::';
--28T09::.176987Z [Note] Server socket created on IP: '::'.
--28T09::.190355Z [Note] InnoDB: Buffer pool(s) load completed at ::
--28T09::.211166Z [Note] Event Scheduler: Loaded events
--28T09::.211365Z [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.7.19' socket: '/tmp/tmpktcZWc/mysql.sock' port: MySQL Community Server (GPL)
--28T09::.211377Z [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check.
--28T09::.211381Z [Note] Beginning of list of non-natively partitioned tables
--28T09::.241025Z [Note] End of list of non-natively partitioned tables
# trying again...
# Success!
# Setting the root password...
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
# Connection Information:
# -uroot -proot --socket=/tmp/tmpktcZWc/mysql.sock
#...done.
--28T09::.297094Z [Note] Aborted connection to db: 'unconnected' user: 'root' host: 'localhost' (Got an error reading communication packets)
# Connecting to spawned server
done.
# Reading .frm files
#
# Reading the py_user.frm file.
# Changing engine for .frm file /tmp/tmpktcZWc/replcrash_temp/py_user.frm:
# Skipping to header at :
# General Data from .frm file:
{'IO_SIZE': ,
'MYSQL_VERSION_ID': ,
'avg_row_length': ,
'charset_low': ,
'create_options': ,
'db_create_pack': ,
'default_charset': ,
'default_part_eng': ,
'extra_size': ,
'frm_file_ver': ,
'frm_version': ,
'key_block_size': ,
'key_info_length': ,
'key_length': ,
'legacy_db_type': 'INNODB',
'length': ,
'max_rows': ,
'min_rows': ,
'rec_length': ,
'row_type': ,
'table_charset': ,
'tmp_key_length': }
# Engine string: InnoDB
# Server version in file: 5.7.
#
# CREATE statement for /data/mysql/mysql3306/data/replcrash/py_user.frm:
# CREATE TABLE `replcrash`.`py_user` (
`uid` int() NOT NULL AUTO_INCREMENT,
`name` varchar() DEFAULT NULL,
`add_time` datetime DEFAULT NULL,
`server_id` varchar() DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 # Shutting down spawned server
# Removing the temporary datadir
--28T09::.471827Z [Note] Giving client threads a chance to die gracefully
--28T09::.471874Z [Note] Shutting down slave threads
--28T09::.471891Z [Note] Forcefully disconnecting remaining clients
--28T09::.471908Z [Note] Event Scheduler: Purging the queue. events
--28T09::.473626Z [Note] Binlog end
--28T09::.473789Z [Note] Shutting down plugin 'ngram'
--28T09::.473820Z [Note] Shutting down plugin 'BLACKHOLE'
--28T09::.473839Z [Note] Shutting down plugin 'ARCHIVE'
--28T09::.473853Z [Note] Shutting down plugin 'partition'
--28T09::.473866Z [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
--28T09::.473880Z [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
--28T09::.473893Z [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
--28T09::.473905Z [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
--28T09::.473918Z [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
--28T09::.473931Z [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
--28T09::.473943Z [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
--28T09::.473956Z [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
--28T09::.473969Z [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
--28T09::.473981Z [Note] Shutting down plugin 'INNODB_SYS_TABLES'
--28T09::.474010Z [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
--28T09::.474024Z [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
--28T09::.474036Z [Note] Shutting down plugin 'INNODB_FT_CONFIG'
--28T09::.474156Z [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
--28T09::.474175Z [Note] Shutting down plugin 'INNODB_FT_DELETED'
--28T09::.474188Z [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
--28T09::.474214Z [Note] Shutting down plugin 'INNODB_METRICS'
--28T09::.474227Z [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
--28T09::.474241Z [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
--28T09::.474256Z [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
--28T09::.474269Z [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
--28T09::.474281Z [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
--28T09::.474303Z [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
--28T09::.474317Z [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
--28T09::.474330Z [Note] Shutting down plugin 'INNODB_CMPMEM'
--28T09::.474343Z [Note] Shutting down plugin 'INNODB_CMP_RESET'
--28T09::.474356Z [Note] Shutting down plugin 'INNODB_CMP'
--28T09::.474369Z [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
--28T09::.474382Z [Note] Shutting down plugin 'INNODB_LOCKS'
--28T09::.474395Z [Note] Shutting down plugin 'INNODB_TRX'
--28T09::.474407Z [Note] Shutting down plugin 'InnoDB'
--28T09::.474590Z [Note] InnoDB: FTS optimize thread exiting.
--28T09::.476410Z [Note] InnoDB: Starting shutdown...
--28T09::.577509Z [Note] InnoDB: Dumping buffer pool(s) to /tmp/tmpktcZWc/ib_buffer_pool
--28T09::.577612Z [ERROR] InnoDB: Cannot open '/tmp/tmpktcZWc/ib_buffer_pool.incomplete' for writing: No such file or directory
--28T09::.117642Z [Note] InnoDB: Shutdown completed; log sequence number
--28T09::.117712Z [Note] Shutting down plugin 'MEMORY'
--28T09::.117727Z [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
--28T09::.117761Z [Note] Shutting down plugin 'MRG_MYISAM'
--28T09::.117767Z [Note] Shutting down plugin 'MyISAM'
--28T09::.117783Z [Note] Shutting down plugin 'CSV'
--28T09::.117790Z [Note] Shutting down plugin 'sha256_password'
--28T09::.117794Z [Note] Shutting down plugin 'mysql_native_password'
--28T09::.117966Z [Note] Shutting down plugin 'binlog'
--28T09::.118884Z [Note] Unable to delete pid file: No such file or directory
--28T09::.118898Z [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
[root@ZST1 ~]#

• 检查读取.frm文件的权限
• 创建临时datadir
• 克隆生成新实例
• 启动、连接新实例
• 读取.frm文件,生成create语句
• 关闭新实例,删除临时datadir
mysqlfrm的使用参考:5.13 mysqlfrm — File reader for .frm files3.3.2 How do you recover the CREATE statement from a damaged or offline server?

二、恢复表数据

这里用到表空间传输,恢复环境使用mysqlfrm得到语句创建相同结构的数据表,然后discard表空间,拷贝故障实例下的ibd文件并修改权限,重新载入表空间,载入过程会有警告,但不影响数据的读取

# 将mysqlfrm得到建表语句拷贝到192.168.85.,3308上执行
mydba@192.168.85.132, [(none)]> use replcrash;
mydba@192.168.85.132, [replcrash]> drop table if exists py_user;
# 执行mysqlfrm得到的create语句
mydba@192.168.85.132, [replcrash]> CREATE TABLE `replcrash`.`py_user` (
-> `uid` int() NOT NULL AUTO_INCREMENT,
-> `name` varchar() DEFAULT NULL,
-> `add_time` datetime DEFAULT CURRENT_TIMESTAMP,
-> `server_id` varchar() DEFAULT NULL,
-> PRIMARY KEY (`uid`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, rows affected (0.17 sec) # discard 表空间
mydba@192.168.85.132, [replcrash]> alter table py_user discard tablespace;
Query OK, rows affected (0.86 sec) # 拷贝ibd文件,并修改权限
[root@ZST1 ~]# scp /data/mysql/mysql3306/data/replcrash/py_user.ibd 192.168.85.132:/data/mysql/mysql3308/data/replcrash/
[root@ZST1 ~]# chown -R mysql:mysql /data/mysql/mysql3308/data/replcrash/py_user.ibd # 载入表空间
mydba@192.168.85.132, [replcrash]> alter table py_user import tablespace;
Query OK, rows affected, warning (0.24 sec) mydba@192.168.85.132, [replcrash]> show warnings;
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | | InnoDB: IO Read error: (, No such file or directory) Error opening './replcrash/py_user.cfg', will attempt to import without schema verification |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------------+
row in set (0.01 sec) # 查询数据是否一致
mydba@192.168.85.132, [replcrash]> select * from py_user;
+-----+------------------------------+---------------------+-----------+
| uid | name | add_time | server_id |
+-----+------------------------------+---------------------+-----------+
| | U4F9ZKZTJ9GK6RFV4A3EEJ | -- :: | |
| | IB1FSWGFEH5CGIT4FWEZK81V | -- :: | |
| | 9N9TF1C8SEGKXNHBUE6CPOXD0IUU | -- :: | |
| | TEBD44BM3E0NZPSIVGI79Z | -- :: | |
| | XS8Z2W2XIDIB5Y4TMF78L9INM7 | -- :: | |
| | OP65BAJ6MNI1W02PNMJGHXX | -- :: | |
| | 19R173XU7QXRPG21413BYMWMY1 | -- :: | |
| | 768NGEAGK0VJ57MW98SG | -- :: | |
| | 9EDYG3U7LFL80WIDKSV5JTI | -- :: | |
+-----+------------------------------+---------------------+-----------+
rows in set (0.00 sec)

三、表空间传输

该功能在MySQL5.6后引入,针对InnoDB独立表空间,可以把一个表Copy到另一个实例

使用该功能前提:
• 使用innodb_file_per_table
• 在做表导出时(export->copy data),只允许read-only事务
• 导入导出的数据库data page size必须一致
• 在MySQL 5.7.4以前不能对分区表做分区迁移
• 对于使用外键的表,导入导出不支持,可以通过set foreign_key_check=0;强制忽略。但对于分区表,目前不能进行操作

3.1、普通表的表空间传输

前面重新载入表空间时出现了警告,是因为我们假设3306实例出现故障,数据库无法启动,因此不能按照正常的表空间传输步骤操作

# InnoDB表空间传输迁移数据
、迁出实例3306
mydba@192.168.85.132, [(none)]> use replcrash;
mydba@192.168.85.132, [replcrash]> select count(*) from py_user;
mydba@192.168.85.132, [replcrash]> show create table py_user; 、迁入实例3308
# 创建表
mydba@192.168.85.132, [(none)]> use replcrash;
mydba@192.168.85.132, [replcrash]> CREATE TABLE `py_user` (
`uid` int() NOT NULL AUTO_INCREMENT,
`name` varchar() DEFAULT NULL,
`add_time` datetime DEFAULT CURRENT_TIMESTAMP,
`server_id` varchar() DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB; # 查看相关文件
[root@ZST1 ~]# ll /data/mysql/mysql3308/data/replcrash/py_user*
-rw-r-----. mysql mysql Dec : /data/mysql/mysql3308/data/replcrash/py_user.frm
-rw-r-----. mysql mysql Dec : /data/mysql/mysql3308/data/replcrash/py_user.ibd
[root@ZST1 ~]# # discard 表空间,.ibd文件会被删除
mydba@192.168.85.132, [replcrash]> alter table py_user discard tablespace; # 再次查看相关文件
[root@ZST1 ~]# ll /data/mysql/mysql3308/data/replcrash/py_user*
-rw-r-----. mysql mysql Dec : /data/mysql/mysql3308/data/replcrash/py_user.frm
[root@ZST1 ~]# 、迁出实例3306
mydba@192.168.85.132, [replcrash]> flush tables py_user for export;
这个时候会加表锁,只能读不能写,同时会产生一个py_user.cfg文件
[root@ZST1 ~]# ll /data/mysql/mysql3306/data/replcrash/py_user*
-rw-r-----. mysql mysql Dec : /data/mysql/mysql3306/data/replcrash/py_user.cfg
-rw-r-----. mysql mysql Nov : /data/mysql/mysql3306/data/replcrash/py_user.frm
-rw-r-----. mysql mysql Dec : /data/mysql/mysql3306/data/replcrash/py_user.ibd
[root@ZST1 ~]# # 将.idb、.cfg拷贝到迁入实例对应目录
[root@ZST1 ~]# scp /data/mysql/mysql3306/data/replcrash/py_user.{cfg,ibd} 192.168.85.132:/data/mysql/mysql3308/data/replcrash/ # 解除锁定
mydba@192.168.85.132, [replcrash]> unlock tables; 、迁入实例3308
# 修改文件权限
[root@ZST1 ~]# chown mysql.mysql /data/mysql/mysql3308/data/replcrash/py_user.{ibd,cfg}
# 导入表空间文件
mydba@192.168.85.132, [replcrash]> alter table py_user import tablespace; 到这里整个innodb表就被迁移完成,比起逻辑(mysqldump)或物理备份(xtrabackup)方便很多

知晓基于.frm、.ibd恢复数据,再去阅读从完整备份恢复单个innodb表就简单了

3.2、分区表的表空间传输

# InnoDB表空间传输迁移数据(分区表)
、迁出实例3306
mydba@192.168.85.132, [(none)]> use replcrash;
mydba@192.168.85.132, [replcrash]> select count(*) from py_user_partition;
mydba@192.168.85.132, [replcrash]> show create table py_user_partition; 、迁入实例3308
# 创建表
mydba@192.168.85.132, [(none)]> use replcrash;
mydba@192.168.85.132, [replcrash]> CREATE TABLE `py_user_partition` (
`uid` int() NOT NULL AUTO_INCREMENT,
`name` varchar() DEFAULT NULL,
`add_time` datetime DEFAULT CURRENT_TIMESTAMP,
`server_id` varchar() DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB PARTITION BY KEY(uid) PARTITIONS ; # 查看相关文件
[root@ZST1 ~]# ll -h /data/mysql/mysql3308/data/replcrash/py_user_partition*
-rw-r-----. mysql mysql .5K Jan : /data/mysql/mysql3308/data/replcrash/py_user_partition.frm
-rw-r-----. mysql mysql 96K Jan : /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p0.ibd
-rw-r-----. mysql mysql 96K Jan : /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p1.ibd
-rw-r-----. mysql mysql 96K Jan : /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p2.ibd
-rw-r-----. mysql mysql 96K Jan : /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p3.ibd
[root@ZST1 ~]# # discard p0分区表空间,p0.ibd文件会被删除
mydba@192.168.85.132, [replcrash]> alter table py_user_partition discard partition p0 tablespace; # 再次查看相关文件
[root@ZST1 ~]# ll -h /data/mysql/mysql3308/data/replcrash/py_user_partition*
-rw-r-----. mysql mysql .5K Jan : /data/mysql/mysql3308/data/replcrash/py_user_partition.frm
-rw-r-----. mysql mysql 96K Jan : /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p1.ibd
-rw-r-----. mysql mysql 96K Jan : /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p2.ibd
-rw-r-----. mysql mysql 96K Jan : /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p3.ibd
[root@ZST1 ~]# # p0分区不能查询
mydba@192.168.85.132, [replcrash]> select * from py_user_partition partition(p0);
ERROR (HY000): Tablespace has been discarded for table 'py_user_partition'
# 其他分区可以查询
mydba@192.168.85.132, [replcrash]> select * from py_user_partition partition(p1);
Empty set (0.00 sec) 、迁出实例3306
mydba@192.168.85.132, [replcrash]> flush tables py_user_partition for export;
这个时候会加表锁,只能读不能写,同时每一个分区会产生一个.cfg文件
[root@ZST1 ~]# ll -h /data/mysql/mysql3306/data/replcrash/py_user_partition*
-rw-r-----. mysql mysql .5K Jan : /data/mysql/mysql3306/data/replcrash/py_user_partition.frm
-rw-r-----. mysql mysql Jan : /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p0.cfg
-rw-r-----. mysql mysql 192K Jan : /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p0.ibd
-rw-r-----. mysql mysql Jan : /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p1.cfg
-rw-r-----. mysql mysql 400K Jan : /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p1.ibd
-rw-r-----. mysql mysql Jan : /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p2.cfg
-rw-r-----. mysql mysql 192K Jan : /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p2.ibd
-rw-r-----. mysql mysql Jan : /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p3.cfg
-rw-r-----. mysql mysql 256K Jan : /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p3.ibd
[root@ZST1 ~]# # 将p0分区的.idb、.cfg拷贝到迁入实例对应目录
[root@ZST1 ~]# scp /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p0.{cfg,ibd} 192.168.85.132:/data/mysql/mysql3308/data/replcrash/ # 解除锁定
mydba@192.168.85.132, [replcrash]> unlock tables; 、迁入实例3308
# 修改文件权限
[root@ZST1 ~]# chown mysql.mysql /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p0.{ibd,cfg}
# 导入表空间文件
mydba@192.168.85.132, [replcrash]> alter table py_user_partition import partition p0 tablespace;
# 查看p0分区的数据
mydba@192.168.85.132, [replcrash]> select count(*) from py_user_partition partition(p0);
+----------+
| count(*) |
+----------+
| |
+----------+
row in set (0.00 sec)

上面是针对某个分区进行表空间传输,其他分区参考操作即可;在使用range分区时,如果只想恢复特定某个分区,带上partition_name非常方便
如果要针对整张分区表进行表空间传输,只需将关键字partition_name替换为all

mysql> alter table py_user_partition discard partition all tablespace;
mysql> alter table py_user_partition import partition all tablespace;