最近、透過的テーブルスペース暗号化(TDE)を使用したテーブルの.ibdファイルを、
別インスタンスに移行する時の注意点を知ったので、メモしておきます。
【 前提 】
TDEを使用したテーブルであっても、mysqldumpを利用した移行は可能です。
ただし、dumpファイルは復号化された状態(平文)で出力されます。
「平文のファイルは扱いたくない!」という場合は、テーブルスペース(.ibdファイル)を
暗号化された状態のまま、EXPORT / IMPORT する必要があります。
【 やりたいこと 】
-
暗号化テーブルに対して、「FLUSH TABLES ... FOR EXPORT」を実行し.ibdファイルなどをコピー可能な状態にする
→ MySQL5.6 リファレンスマニュアル -
各種ファイルを移行先サーバに転送
-
移行先MySQLで、「ALTER TABLE tbl_name IMPORT TABLESPACE;」を実行し、.ibdファイルなどをインポート
→ MySQL5.6 リファレンスマニュアル
【 注意点 】
TDEを使用したテーブルに対して「FLUSH TABLES ... FOR EXPORT」を実行すると、
".cfpファイル" が生成されます。
このファイルには「TDEの暗号キー2種」の情報が含まれているようで、これを使用することで
移行先MySQLでも既存のテーブルスペースを復号化できるようです。
When exporting a tablespace that is encrypted using the InnoDB tablespace encryption feature, InnoDB generates a .cfp file in addition to a .cfg metadata file. The .cfp file must be copied to the destination server together with the .cfg file and tablespace file before performing the ALTER TABLE ... IMPORT TABLESPACE operation on the destination server. The .cfp file contains a transfer key and an encrypted tablespace key. On import, InnoDB uses the transfer key to decrypt the tablespace key.
【 検証 】
mysql> CREATE TABLE enc_t1 (id int, body text) ENCRYPTION = 'Y';
mysql> INSERT INTO enc_t1 VALUES (1, "This is test!"),(5,"MySQL5.7"),(10,"1+1=2");
mysql> FLUSH TABLES enc_t1 FOR EXPORT;
# 別コンソールを立ち上げる
[root@test-server ~]# ls /var/lib/mysql/test | grep enc
enc_t1.cfg
enc_t1.cfp
enc_t1.frm
enc_t1.ibd
[root@test-server ~]# scp /var/lib/mysql/test/enc_t1.* <移行先サーバ>
# 移行先サーバに移動
mysql> CREATE TABLE enc_t1 (id int, body text) ENCRYPTION = 'Y';
mysql> INSERT INTO enc_t1 VALUES (1000, "New server");
mysql> ALTER TABLE enc_t1 DISCARD TABLE SPACE;
[root@test-server2 ~]# cp enc_t1.ibd/.cfp/.cfg/.frm <スキーマのディレクトリ>
[root@test-server2 ~]# chown -R mysql:mysql <スキーマのディレクトリ>
mysql> SELECT * FROM enc_t1\G
*************************** 1. row ***************************
id: 1
body: This is test!
*************************** 2. row ***************************
id: 5
body: MySQL5.7
*************************** 3. row ***************************
id: 10
body: 1+1=2
【 補足 】
移行先サーバの鍵(keyringファイル)自体は更新されず、従来のままのようです。
# 移行元サーバ
$ strings /tmp/keyring
INNODBKey-63ff5824-5411-11e6-b70c-0800272020f4-1AESw
# 移行先サーバ(IMPORT前)
$ strings /tmp/keyring2
INNODBKey-a39cafed-bcfb-11e6-82f3-0800272020f4-1AES
# 移行先サーバ(IMPORT後)
$ strings /tmp/keyring2
INNODBKey-a39cafed-bcfb-11e6-82f3-0800272020f4-1AES