Preface
 
    We all know that Xtrabackup is a backup tool of percona for innodb or Xtradb.It's usually used to back up whole databases physically.But how to restore a single innodb table from a full Xtrabackup after dropping it by accident?We can also make use of the feature of transportable tablespace to accomplish it.
    I've demonstrated how to restore a table on a server to another using transportable tablespace in my yesterday's blog.Today,we'll use another tool called "mysqlfrm" to fetch the structure of table in .frm files.
 
Introduce
 
    mysqlfrm is a tool designed to diagnose information in .frm file when in recovery scenario.mysqlfrm provides two modes of operatins.By default,it creates a new instance referencing the base directory using "--basedir" it also need a port specified by "--port" which ought to be diffrent from the one used in the original instance.The other mode is connecting to the already exist instance using "--server".The new instance will be shutdown and all temperary files will be deleted after it reads data in .frm files.Further more,there're two exclusions when using mysqlfrm,one is foreign key constraints,the other one is auto increment number sequences.
 
Example
 
Install mysqlfrm tool.
 [root@zlm1 :: ~]
#yum install mysql-utilities Installed:
mysql-utilities.noarch :1.3.-.el7 Dependency Installed:
mysql-connector-python.noarch :1.1.-.el7 Complete!
Generate a Xtrabackup backup.
 [root@zlm1 :: ~]
#innobackupex --defaults-file=/data/mysql/mysql3306/my3306.cnf --host=localhost --user=root --password=Passw0rd --sock=/tmp/mysql3306.sock /data/backup
:: innobackupex: Starting the backup operation IMPORTANT: Please check that the backup run completes successfully.
At the end of a successful backup run innobackupex
prints "completed OK!". //Omitts the intermedia output. :: Finished backing up non-InnoDB tables and files
:: [] Writing xtrabackup_binlog_info
:: [] ...done
:: Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point (for incremental): ''
xtrabackup: Stopping log copying thread.
. :: >> log scanned up to () :: Executing UNLOCK TABLES
:: All tables unlocked
:: [] Copying ib_buffer_pool to /data/backup/--06_10--/ib_buffer_pool
:: [] ...done
:: Backup created in directory '/data/backup/2018-07-06_10-09-22'
MySQL binlog position: filename 'mysql-bin.000071', position '', GTID of the last change '2a4b3562-2ab6-11e8-be7a-080027de0e0e:1-12715494'
:: [] Writing backup-my.cnf
:: [] ...done
:: [] Writing xtrabackup_info
:: [] ...done
xtrabackup: Transaction log of lsn () to () was copied.
:: completed OK! [root@zlm1 :: ~]
#cd /data/backup [root@zlm1 :: /data/backup]
#ls -l
total
drwxr-x--- root root Jul : --06_10-- [root@zlm1 :: /data/backup]
#cd --06_10--/ [root@zlm1 :: /data/backup/--06_10--]
#ls -l
total
drwxr-x--- root root Jul : aaron8219
-rw-r----- root root Jul : backup-my.cnf
drwxr-x--- root root Jul : -help
-rw-r----- root root Jul : ib_buffer_pool
-rw-r----- root root Jul : ibdata1
drwxr-x--- root root Jul : mysql
drwxr-x--- root root Jul : performance_schema
drwxr-x--- root root Jul : sys
drwxr-x--- root root Jul : sysbench
-rw-r----- root root Jul : xtrabackup_binlog_info
-rw-r----- root root Jul : xtrabackup_checkpoints
-rw-r----- root root Jul : xtrabackup_info
-rw-r----- root root Jul : xtrabackup_logfile
drwxr-x--- root root Jul : zlm
Prepare the backup.
 [root@zlm1 :: /data/backup/--06_10--]
#innobackupex --defaults-file=/data/mysql/mysql3306/my3306.cnf --host=localhost --user=root --password=Passw0rd --sock=/tmp/mysql3306.sock --apply-log /data/backup/--06_10--/
:: innobackupex: Starting the apply-log operation IMPORTANT: Please check that the apply-log run completes successfully.
At the end of a successful apply-log run innobackupex
prints "completed OK!". //Omitts the intermedia output. InnoDB: Database was not shutdown normally!
InnoDB: Starting crash recovery.
InnoDB: xtrabackup: Last MySQL binlog file position , file name mysql-bin.
InnoDB: Removed temporary tablespace data file: "ibtmp1"
InnoDB: Creating shared tablespace for temporary tables
InnoDB: Setting file './ibtmp1' size to MB. Physically writing the file full; Please wait ...
InnoDB: File './ibtmp1' size is now MB.
InnoDB: redo rollback segment(s) found. redo rollback segment(s) are active.
InnoDB: non-redo rollback segment(s) are active.
InnoDB: Waiting for purge to start
InnoDB: 5.7. started; log sequence number
xtrabackup: starting shutdown with innodb_fast_shutdown =
InnoDB: page_cleaner: 1000ms intended loop took 10865ms. The settings might not be optimal. (flushed= and evicted=, during the time.)
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number
:: completed OK!
Drop two tables to mimic misoperation.
 root@localhost:mysql3306.sock [(none)]>show tables from zlm;
+----------------+
| Tables_in_zlm |
+----------------+
| customer |
| goods |
| semi_sync_test |
| test_flashbk |
| test_myisam |
+----------------+
rows in set (0.00 sec) root@localhost:mysql3306.sock [(none)]>show tables from sysbench;
+--------------------+
| Tables_in_sysbench |
+--------------------+
| sbtest1 |
| sbtest10 |
| sbtest2 |
| sbtest3 |
| sbtest4 |
| sbtest5 |
| sbtest6 |
| sbtest7 |
| sbtest8 |
| sbtest9 |
+--------------------+
rows in set (0.00 sec) root@localhost:mysql3306.sock [(none)]>drop table zlm.test_flashbk,sysbench.sbtest1;
Query OK, rows affected (0.11 sec)
Diagnose .frm file from Xtrabackup using mysqlfrm.
 [root@zlm1 :: /data/backup/--06_10--]
#mysqlfrm --basedir=/usr/local/mysql --port= --diagnostic /data/backup/--06_10--/sysbench/sbtest1.frm /data/backup/--06_10--/zlm/zlm:test_flashbk.frmsbtest1.frm /data/backup/--06_10--/zlm/zlm:tes
# WARNING The --port option is not used in the --diagnostic mode.
# WARNING: Cannot generate character set or collation names without the --server option.
# CAUTION: The diagnostic mode is a best-effort parse of the .frm file. As such, it may not identify all of the components of the table correctly. This is especially true for damaged files. It will also not read the default values for the columns and the resulting statement may not be syntactically correct.
# Reading .frm file for /data/backup/--06_10--/sysbench/sbtest1.frm:
# The .frm file is a TABLE.
# CREATE TABLE Statement: CREATE TABLE `sysbench`.`sbtest1` (
`id` int() NOT NULL AUTO_INCREMENT,
`k` int() NOT NULL,
`c` char() NOT NULL,
`pad` char() NOT NULL,
PRIMARY KEY `PRIMARY` (`id`),
KEY `k_1` (`k`)
) ENGINE=InnoDB; # Reading .frm file for /data/backup/--06_10--/zlm/test_flashbk.frm:
# The .frm file is a TABLE.
# CREATE TABLE Statement: CREATE TABLE `zlm`.`test_flashbk` (
`id` bigint() NOT NULL AUTO_INCREMENT,
`name` varchar() NOT NULL,
PRIMARY KEY `PRIMARY` (`id`)
) ENGINE=InnoDB; #...done. //If you want to get the information of character set,"--server" is indispensable.
//you can either use <dbname>:<tablename>.frm or just <tablename>.frm.
//"--port" can be omitted.
Create vacant table using above create statement.
 root@localhost:mysql3306.sock [(none)]>CREATE TABLE `sysbench`.`sbtest1` (
-> `id` int() NOT NULL AUTO_INCREMENT,
-> `k` int() NOT NULL,
-> `c` char() NOT NULL,
-> `pad` char() NOT NULL,
-> PRIMARY KEY `PRIMARY` (`id`),
-> KEY `k_1` (`k`)
-> ) ENGINE=InnoDB;
ERROR (): Column length too big for column 'c' (max = ); use BLOB or TEXT instead
root@localhost:mysql3306.sock [(none)]>CREATE TABLE `zlm`.`test_flashbk` (
-> `id` bigint() NOT NULL AUTO_INCREMENT,
-> `name` varchar() NOT NULL,
-> PRIMARY KEY `PRIMARY` (`id`)
-> ) ENGINE=InnoDB;
Query OK, rows affected (0.02 sec) //We get an error when creating table sysbench.sbtest1 beause of the overload value of char.
Check the structure of  sbtest2 table in sysbench.
 root@localhost:mysql3306.sock [(none)]>show create table sysbench.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 '', //In the counterpart table,the value is 120.
`pad` char() NOT NULL DEFAULT '', //In the counterpart table,the value is 60.
PRIMARY KEY (`id`),
KEY `k_2` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf8
row in set (0.00 sec)
Change the create statement reference to the value in sbtest2.
 when creating table sysbench.sbtest1 beause of the overload value of char.

 Check the structure of  sbtest2 table in sysbench.
root@localhost:mysql3306.sock [(none)]>show create table sysbench.sbtest2\G
*************************** . row ***************************
Table: sbtest2
Create Table: CREATE TABLE `sbtest2` (
`id` int() NOT NULL AUTO_INCREMENT,
`k` int() NO
Add a write lock on these two tables.
 root@localhost:mysql3306.sock [(none)]>lock tables sysbench.sbtest1 write;
Query OK, rows affected (0.00 sec) root@localhost:mysql3306.sock [(none)]>lock tables zlm.test_flashbk write;
Query OK, rows affected (0.00 sec) root@localhost:mysql3306.sock [(none)]>alter table sysbench.sbtest1 discard tablespace;
ERROR (HY000): Table 'sbtest1' was not locked with LOCK TABLES //when locks another table,the lock on previous table will be released.
root@localhost:mysql3306.sock [(none)]>lock tables sysbench.sbtest1 write; //This time,lock one and discard one in order.
Query OK, rows affected (0.00 sec) root@localhost:mysql3306.sock [(none)]>alter table sysbench.sbtest1 discard tablespace;
Query OK, rows affected (0.00 sec) root@localhost:mysql3306.sock [(none)]>lock tables zlm.test_flashbk write;
Query OK, rows affected (0.00 sec) root@localhost:mysql3306.sock [(none)]>alter table zlm.test_flashbk discard tablespace;
Query OK, rows affected (0.00 sec)
Copy .ibd files from Xtrabackup and change privilege.
 [root@zlm1 :: /data/backup/--06_10--]
#cp sysbench/sbtest1.ibd /data/mysql/mysql3306/data/sysbench [root@zlm1 :: /data/backup/--06_10--]
#cp zlm/test_flashbk.ibd /data/mysql/mysql3306/data/zlm [root@zlm1 :: /data/backup/--06_10--]
#chown -R mysql.mysql /data/mysql/mysql3306/data [root@zlm1 :: /data/backup/--06_10--]
#ls -l /data/mysql/mysql3306/data/sysbench | grep sbtest1.ibd
-rw-r----- mysql mysql Jul : sbtest1.ibd [root@zlm1 :: /data/backup/--06_10--]
#ls -l /data/mysql/mysql3306/data/zlm | grep test_flashbk.ibd
-rw-r----- mysql mysql Jul : test_flashbk.ibd
Import tablespaces and check data of tables.
 root@localhost:mysql3306.sock [(none)]>alter table sysbench.sbtest1 import tablespace;
Query OK, rows affected, warning (1.73 sec) root@localhost:mysql3306.sock [(none)]>show warnings;
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | | InnoDB: IO Read error: (, No such file or directory) Error opening './sysbench/sbtest1.cfg', will attempt to import without schema verification |
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------+
row in set (0.00 sec) root@localhost:mysql3306.sock [(none)]>alter table zlm.test_flashbk import tablespace;
Query OK, rows affected, warning (1.01 sec) root@localhost:mysql3306.sock [(none)]>show warnings;
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | | InnoDB: IO Read error: (, No such file or directory) Error opening './zlm/test_flashbk.cfg', will attempt to import without schema verification |
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------+
row in set (0.00 sec) root@localhost:mysql3306.sock [(none)]>select count(*) from sysbench.sbtest1;
+----------+
| count(*) |
+----------+
| |
+----------+
row in set (0.25 sec) root@localhost:mysql3306.sock [(none)]>select count(*) from zlm.test_flashbk;
+----------+
| count(*) |
+----------+
| |
+----------+
row in set (0.10 sec) //The warnings show that tthe message about missing of .cfg file what rally doesn't matter.
//The .cfg file is usually create by executing "flush table ... for export;"
//We can benifit in crash recover scenario with the support of ignoring the missing of .cfg in transportable tablespace feature.
Summary
  • mysqlfrm is a tool of mysql-utilities which is specialized in analyzing .frm files in order to gain the missing structure of tables.
  • mysqlfrm provides two modes of operation:1. connecting to server with "--server"(defaut mode);2. create a new instance with "--basedir".
  • With the help of parameter "--diagnostic",we can even get information from a .frm file without installing a MySQL server on the host.
  • We cannot get character set and collation information on tables if we forget to use "--server" option.
  • Of course,in order to use transportable tablespace properly,the parameter "innodb_file_per_table=1" is necessary.
  • In my case above,the structure of table about char datatype changed accidently which I'm still baffled with.
  • Also,we can restore these table to any other server like transportable tablespace does.
 

最新文章

  1. CodeForces - 453A Little Pony and Expected Maximum
  2. iOS 架构模式MVVM
  3. Python基础语法,基本数据类型及相关操作
  4. Azure操作手册集合
  5. PHP后台代码 及 iOS客户端--AF实现上传视频
  6. Android Studio-目录结构
  7. android判断pad还是手机
  8. thinkphp 常用
  9. linux-i386(ubuntu)下编译安装gsoap_2.8.17过程记录
  10. 如何判断一个数是否为素数(zt)
  11. UVA11080- Place the Guards(二分图染色)
  12. .NET Core 实现 Redis 批量查询指定格式的Key
  13. 安卓TabLayout+ViewPager实现切页
  14. MT【305】丹德林双球
  15. Introduction to boundary integral equations in BEM
  16. 《剑指offer》-数据流中的中位数
  17. 【Java】 剑指offer(59-2) 队列的最大值
  18. python魔法方法-单目运算及一般算数运算
  19. redis点
  20. Java并发-UncaughtExceptionHandler捕获线程异常信息并重新启动线程

热门文章

  1. HUD 5593——ZYB&#39;s Tree——————【树形dp】
  2. Day3下
  3. phpstorm 配置 webserver ,配置根目录
  4. c#实体转化
  5. 利用自定义特性实现List的多属性排序
  6. 微信小程序电商实战-入门篇
  7. JavaScript常用字符串操作方法
  8. npm下载某个版本
  9. AOSP 源码下载
  10. Linux CentOS下部署Java Web项目