A few months back, Michael wrote about reconnecting orphaned *.ibd files using MySQL 5.6. I will show you the same procedure, this time for partitioned tables. An InnoDB partition is also a self-contained tablespace in itself so you can use the same method described in the previous post.
To begin with, I have an example table with a few orphaned partitions and we will reconnect each partition one by one to the original table.
|
1 |
mysql [localhost] {msandbox} (recovery) > SHOW CREATE TABLE t1 G<br>*************************** 1. row ***************************<br>Table: t1<br>Create Table: CREATE TABLE `t1` (<br>[...]<br>KEY `h_date` (`h_date`)<br>) ENGINE=InnoDB DEFAULT CHARSET=latin1<br>/*!50100 PARTITION BY RANGE (year(h_date))<br>(PARTITION p0 VALUES LESS THAN (2006) ENGINE = InnoDB,<br>PARTITION p1 VALUES LESS THAN (2010) ENGINE = InnoDB,<br>PARTITION px VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */<br>1 row in set (0.00 sec)<br><br>mysql [localhost] {msandbox} (recovery) > SELECT COUNT(*) FROM t1;<br>+----------+<br>| COUNT(*) |<br>+----------+<br>| 0 |<br>+----------+<br>1 row in set (0.00 sec)<br><br>-rw-rw----. 1 revin revin 176M Oct 8 08:41 t1#P#p0.ibd<br>-rw-rw----. 1 revin revin 612M Oct 8 08:41 t1#P#p1.ibd<br>-rw-rw----. 1 revin revin 932M Oct 8 08:42 t1#P#px.ibd |
The first step is to create a dummy table and remove partitioning so that we can reattach individual partitions to this table.
|
1 |
mysql [localhost] {msandbox} (recovery) > CREATE TABLE t1_t LIKE t1;<br>Query OK, 0 rows affected (0.02 sec)<br><br>mysql [localhost] {msandbox} (recovery) > ALTER TABLE t1_t REMOVE PARTITIONING;<br>Query OK, 0 rows affected (0.01 sec)<br>Records: 0 Duplicates: 0 Warnings: 0<br><br>mysql [localhost] {msandbox} (recovery) > ALTER TABLE t1_t DISCARD TABLESPACE;<br>Query OK, 0 rows affected (0.05 sec) |
|
1 |
mysql [localhost] {msandbox} (recovery) > ALTER TABLE t1_t IMPORT TABLESPACE;<br>Query OK, 0 rows affected, 1 warning (7.34 sec) |
|
1 |
mysql [localhost] {msandbox} (recovery) > ALTER TABLE t1 EXCHANGE PARTITION px WITH TABLE t1_t;<br>Query OK, 0 rows affected (6.42 sec)<br> <br>mysql [localhost] {msandbox} (recovery) > SELECT COUNT(*) FROM t1;<br>+----------+<br>| COUNT(*) |<br>+----------+<br>| 8523686 |<br>+----------+<br>1 row in set (2.50 sec) |
|
1 |
mysql [localhost] {msandbox} (recovery) > SHOW CREATE TABLE t2 G<br>*************************** 1. row ***************************<br> Table: t2<br>Create Table: CREATE TABLE `t2` (<br>[...]<br> KEY `h_date` (`h_date`)<br>) ENGINE=InnoDB DEFAULT CHARSET=latin1<br>/*!50100 PARTITION BY RANGE (year(h_date))<br>SUBPARTITION BY HASH (u_id)<br>SUBPARTITIONS 2<br>(PARTITION p0 VALUES LESS THAN (2006) ENGINE = InnoDB,<br> PARTITION p1 VALUES LESS THAN (2010) ENGINE = InnoDB,<br> PARTITION px VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */<br>1 row in set (0.00 sec)<br><br>mysql [localhost] {msandbox} (recovery) > SELECT COUNT(*) FROM t2;<br>+----------+<br>| COUNT(*) |<br>+----------+<br>| 0 |<br>+----------+<br>1 row in set (0.94 sec)<br><br>-rw-rw----. 1 revin revin 92M Oct 8 08:44 t2#P#p0#SP#p0sp0.ibd<br>-rw-rw----. 1 revin revin 92M Oct 8 08:44 t2#P#p0#SP#p0sp1.ibd<br>-rw-rw----. 1 revin revin 304M Oct 8 08:44 t2#P#p1#SP#p1sp0.ibd<br>-rw-rw----. 1 revin revin 316M Oct 8 08:44 t2#P#p1#SP#p1sp1.ibd<br>-rw-rw----. 1 revin revin 480M Oct 8 08:45 t2#P#px#SP#pxsp0.ibd<br>-rw-rw----. 1 revin revin 460M Oct 8 08:45 t2#P#px#SP#pxsp1.ibd<br><br>mysql [localhost] {msandbox} (recovery) > CREATE TABLE t2_t LIKE t2;<br>Query OK, 0 rows affected (0.02 sec)<br><br>mysql [localhost] {msandbox} (recovery) > ALTER TABLE t2_t REMOVE PARTITIONING;<br>Query OK, 0 rows affected (0.02 sec)<br>Records: 0 Duplicates: 0 Warnings: 0<br><br>mysql [localhost] {msandbox} (recovery) > ALTER TABLE t2_t DISCARD TABLESPACE;<br>Query OK, 0 rows affected (0.04 sec) |
|
1 |
mysql [localhost] {msandbox} (recovery) > ALTER TABLE t2_t IMPORT TABLESPACE;<br>Query OK, 0 rows affected, 1 warning (2.49 sec)<br><br>mysql [localhost] {msandbox} (recovery) > ALTER TABLE t2 EXCHANGE PARTITION pxsp1 WITH TABLE t2_t;<br>Query OK, 0 rows affected (3.11 sec)<br> <br>mysql [localhost] {msandbox} (recovery) > SELECT COUNT(*) FROM t2;<br>+----------+<br>| COUNT(*) |<br>+----------+<br>| 4546036 |<br>+----------+<br>1 row in set (0.94 sec) |
|
1 |
mysql [localhost] {msandbox} (test) > SELECT COUNT(*) FROM t2;<br>+----------+<br>| COUNT(*) |<br>+----------+<br>| 0 |<br>+----------+<br>1 row in set (0.00 sec)<br><br>mysql [localhost] {msandbox} (test) > ALTER TABLE t2 DISCARD TABLESPACE;<br>Query OK, 0 rows affected (0.02 sec)<br><br>mysql [localhost] {msandbox} (test) > ! cp -v /sbx/msb/msb_5_6_210/data/test/t2#P#* /sbx/msb/msb_5_7_5/data/test/<br>`/sbx/msb/msb_5_6_210/data/test/t2#P#p0#SP#p0sp0.ibd' -> `/sbx/msb/msb_5_7_5/data/test/t2#P#p0#SP#p0sp0.ibd'<br>`/sbx/msb/msb_5_6_210/data/test/t2#P#p0#SP#p0sp1.ibd' -> `/sbx/msb/msb_5_7_5/data/test/t2#P#p0#SP#p0sp1.ibd'<br>`/sbx/msb/msb_5_6_210/data/test/t2#P#p1#SP#p1sp0.ibd' -> `/sbx/msb/msb_5_7_5/data/test/t2#P#p1#SP#p1sp0.ibd'<br>`/sbx/msb/msb_5_6_210/data/test/t2#P#p1#SP#p1sp1.ibd' -> `/sbx/msb/msb_5_7_5/data/test/t2#P#p1#SP#p1sp1.ibd'<br>`/sbx/msb/msb_5_6_210/data/test/t2#P#px#SP#pxsp0.ibd' -> `/sbx/msb/msb_5_7_5/data/test/t2#P#px#SP#pxsp0.ibd'<br>`/sbx/msb/msb_5_6_210/data/test/t2#P#px#SP#pxsp1.ibd' -> `/sbx/msb/msb_5_7_5/data/test/t2#P#px#SP#pxsp1.ibd'<br>mysql [localhost] {msandbox} (test) > ALTER TABLE t2 IMPORT TABLESPACE;<br>Query OK, 0 rows affected, 6 warnings (11.36 sec)<br><br>mysql [localhost] {msandbox} (test) > SHOW WARNINGS G<br>*************************** 1. row ***************************<br> Level: Warning<br> Code: 1810<br>Message: InnoDB: IO Read error: (2, No such file or directory) Error opening './test/t2#P#p0#SP#p0sp0.cfg', will attempt to import without schema verification<br>[...]<br> |
Resources
RELATED POSTS