Portable Tablespace in InnoDB I test it!
Overview
I have recently blog on the company site about portable Tablespaces
(root@localhost) [(none)]>status; -------------- /home/mysql/templates/mysql-56p/bin/mysql Ver 14.14 Distrib 5.6.6-labs-april-2012, for linux2.6 (i686) using EditLine wrapper Connection id: 1 Current database: Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.6.6-labs-april-2012-log MySQL Community Server (GPL) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: latin1 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /home/mysql/instances/my56testm/mysql.sock Uptime: 21 sec Threads: 1 Questions: 5 Slow queries: 0 Opens: 16 Flush tables: 1 Open tables: 9 Queries per second avg: 0.238
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
(root@localhost) [(none)]>use test_tablespace1 Database changed (root@localhost) [test_tablespace1]>show tables; Empty set (0.00 sec) (root@localhost) [test_tablespace1]>create table tbtest(a int auto_increment PRIMARY KEY, |
1 2 3 4 5 6 |
rwx------ 2 mysql mysql 4096 2012-04-27 14:42 ./ drwxr-xr-x 4 mysql mysql 4096 2012-04-27 14:28 ../ -rw-rw---- 1 mysql mysql 65 2012-04-27 14:28 db.opt -rw-rw---- 1 mysql mysql 8606 2012-04-27 14:42 tbtest.frm -rw-rw---- 1 mysql mysql 360448 2012-04-27 14:45 tbtest.ibd root@000033:/home/mysql/instances/my56testm/data/test_tablespace1# |
1 2 3 4 5 6 7 |
drwx------ 2 mysql mysql 4096 2012-04-27 14:51 ./ drwxr-xr-x 4 mysql mysql 4096 2012-04-27 14:28 ../ -rw-rw---- 1 mysql mysql 65 2012-04-27 14:28 db.opt -rw-rw---- 1 mysql mysql 440 2012-04-27 14:51 tbtest.cfg <--------------IS THERE!!! -rw-rw---- 1 mysql mysql 8606 2012-04-27 14:42 tbtest.frm -rw-rw---- 1 mysql mysql 360448 2012-04-27 14:45 tbtest.ibd root@000033:/home/mysql/instances/my56testm/data/test_tablespace1# |
1 2 3 4 5 6 7 8 |
Name of the machine: 000033 Schema/Table name: test_tablespace1/tbtest Table definition: a, b, dat last ROW_ID: DB_ROW_ID Transaction ID: DB_TRX_ID Rollback pointer: DB_ROLL_PTR Primary kye and value : PRIMARY a Last transaction valuea: DB_TRX_ID DB_ROLL_PTR b dat |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
root@000033:/home/mysql/instances/my56testm/data/test_tablespace1# mkdir -p /home/mysql/backup root@000033:/home/mysql/instances/my56testm/data/test_tablespace1# cp tbtest.* /home/mysql/backup/ root@000033:/home/mysql/instances/my56testm/data/test_tablespace1# ll total 380 drwx------ 2 mysql mysql 4096 2012-04-27 14:59 ./ drwxr-xr-x 4 mysql mysql 4096 2012-04-27 14:28 ../ -rw-rw---- 1 mysql mysql 65 2012-04-27 14:28 db.opt -rw-rw---- 1 mysql mysql 440 2012-04-27 14:51 tbtest.cfg -rw-rw---- 1 mysql mysql 8606 2012-04-27 14:42 tbtest.frm -rw-rw---- 1 mysql mysql 360448 2012-04-27 14:45 tbtest.ibd root@000033:/home/mysql/instances/my56testm/data/test_tablespace1# ll /home/mysql/backup total 376 drwxr-xr-x 2 root root 4096 2012-04-27 14:59 ./ drwxrwxr-- 30 mysql mysql 4096 2012-04-27 14:59 ../ -rw-r----- 1 root root 440 2012-04-27 14:59 tbtest.cfg -rw-r----- 1 root root 8606 2012-04-27 14:59 tbtest.frm -rw-r----- 1 root root 360448 2012-04-27 14:59 tbtest.ibd root@000033:/home/mysql/instances/my56testm/data/test_tablespace |
1 |
UNLOCK TABLES;
|
1 2 3 4 5 6 |
drwx------ 2 mysql mysql 4096 2012-04-27 15:01 ./ drwxr-xr-x 4 mysql mysql 4096 2012-04-27 14:28 ../ -rw-rw---- 1 mysql mysql 65 2012-04-27 14:28 db.opt -rw-rw---- 1 mysql mysql 8606 2012-04-27 14:42 tbtest.frm -rw-rw---- 1 mysql mysql 360448 2012-04-27 14:45 tbtest.ibd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
(root@localhost) [test_tablespace1]>create table tbtest2 select * from tbtest; Query OK, 8192 rows affected (1.02 sec) Records: 8192 Duplicates: 0 Warnings: 0 (root@localhost) [test_tablespace1]> Drop tbtest: (root@localhost) [test_tablespace1]>drop table tbtest; Query OK, 0 rows affected (0.08 sec) (root@localhost) [test_tablespace1]>show tables; +----------------------------+ | Tables_in_test_tablespace1 | +----------------------------+ | tbtest2 | +----------------------------+ 1 row in set (0.00 sec) |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
(root@localhost) [test_tablespace1]>create table tbtest(a char(1)); Query OK, 0 rows affected (0.23 sec) (root@localhost) [test_tablespace1]>ALTER TABLE tbtest DISCARD TABLESPACE; Query OK, 0 rows affected (0.04 sec) (root@localhost) [test_tablespace1]> drwx------ 2 mysql mysql 4096 2012-04-27 15:05 ./ drwxr-xr-x 4 mysql mysql 4096 2012-04-27 14:28 ../ -rw-rw---- 1 mysql mysql 65 2012-04-27 14:28 db.opt -rw-rw---- 1 mysql mysql 8606 2012-04-27 15:02 tbtest2.frm -rw-rw---- 1 mysql mysql 425984 2012-04-27 15:02 tbtest2.ibd -rw-rw---- 1 mysql mysql 8554 2012-04-27 15:04 tbtest.frm -rw-rw---- 1 mysql mysql 98304 2012-04-27 15:04 tbtest.ibt <----------- DETACHED tablespace |
1 2 3 4 5 6 7 8 9 10 11 12 |
root@000033:/home/mysql/instances/my56testm/data/test_tablespace1# cp /home/mysql/backup/* . root@000033:/home/mysql/instances/my56testm/data/test_tablespace1# ll total 904 drwx------ 2 mysql mysql 4096 2012-04-27 15:06 ./ drwxr-xr-x 4 mysql mysql 4096 2012-04-27 14:28 ../ -rw-rw---- 1 mysql mysql 65 2012-04-27 14:28 db.opt -rw-rw---- 1 mysql mysql 8606 2012-04-27 15:02 tbtest2.frm -rw-rw---- 1 mysql mysql 425984 2012-04-27 15:02 tbtest2.ibd -rw-r----- 1 root root 440 2012-04-27 15:06 tbtest.cfg -rw-rw---- 1 mysql mysql 8606 2012-04-27 15:06 tbtest.frm -rw-r----- 1 root root 360448 2012-04-27 15:06 tbtest.ibd -rw-rw---- 1 mysql mysql 98304 2012-04-27 15:04 tbtest.ibt |
1 2 3 4 5 6 7 8 9 10 11 12 |
root@000033:/home/mysql/instances/my56testm/data/test_tablespace1# chown mysql:mysql tbtest.* root@000033:/home/mysql/instances/my56testm/data/test_tablespace1# ll total 904 drwx------ 2 mysql mysql 4096 2012-04-27 15:06 ./ drwxr-xr-x 4 mysql mysql 4096 2012-04-27 14:28 ../ -rw-rw---- 1 mysql mysql 65 2012-04-27 14:28 db.opt -rw-rw---- 1 mysql mysql 8606 2012-04-27 15:02 tbtest2.frm -rw-rw---- 1 mysql mysql 425984 2012-04-27 15:02 tbtest2.ibd -rw-r----- 1 mysql mysql 440 2012-04-27 15:06 tbtest.cfg -rw-rw---- 1 mysql mysql 8606 2012-04-27 15:06 tbtest.frm -rw-r----- 1 mysql mysql 360448 2012-04-27 15:06 tbtest.ibd -rw-rw---- 1 mysql mysql 98304 2012-04-27 15:04 tbtest.ibt |
- attach the ibt table space (I had it saved in backup)
- drop it
- recreate table as for initialal structure
- detach it again.
- copy back the old idb file and cfg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
(root@localhost) [test_tablespace1]>ALTER TABLE tbtest IMPORT TABLESPACE; Query OK, 0 rows affected (2.93 sec) (root@localhost) [test_tablespace1]>check table tbtest; +-------------------------+-------+----------+----------+ | Table | Op | Msg_type | Msg_text | +-------------------------+-------+----------+----------+ | test_tablespace1.tbtest | check | status | OK | +-------------------------+-------+----------+----------+ 1 row in set (0.02 sec) (root@localhost) [test_tablespace1]>select count(*) from tbtest; +----------+ | count(*) | +----------+ | 8192 | +----------+ 1 row in set (0.00 sec) |
- Create a fake table tbtest3
- change the info in the cfg file and the table space filename
- try to import it.
1 2 3 |
root@000033:/home/mysql/backup# cp tbtest.ibd tbtest3.ibd root@000033:/home/mysql/backup# cp tbtest.cfg tbtest3.cfg root@000033:/home/mysql/backup# vi tbtest3.cfg |
1 2 |
(root@localhost) [test_tablespace1]>ALTER TABLE tbtest3 IMPORT TABLESPACE; ERROR 1712 (HY000): InnoDB: While reading table name: 'I/O error'. |
Ok so Conclusions
The full procedure in a thumb
- take the table creation to replicate the structure
SHOW CREATE TABLE tbtest\G - Lock the table to copy it
FLUSH TABLE tbtest WITH READ LOCK; - Copy somewhere the files DON'T forget the .cnf
- UNLOCK TABLES;
- Drop the table
- Create a fake table using the create statement stored before
- Detach the table
- ALTER TABLE tbtest DISCARD TABLESPACE;
- MOVE !!! the *.ibt file in a safe place
- Copy over the previous files from the backup directory
- CHECK PERMISSION!!
- Import back the table space
- ALTER TABLE tbtest IMPORT TABLESPACE;
- check table;
{joscommentenable}