MYSQL表空間遷移
有如下原因你可能需要將InnoDB表復制到不同的數據庫服務器上。
不增加生產負載的情況下生成 一個報表
在一個新的服務器上建立一個和生產上數據相同的表
做一個備份在發生問題或錯誤操作時用于恢復
快速將數據從一個服務器遷移到另一個服務器
命令FLUSH TABLES ... FOREXPORT 使.ibd文件保持一致的狀態。只有文件處于一致的狀態我們才可以復制它。這個文件也會同時創建一個擴展名.cfg的二進制的文件。命令ALTER TABLE ...IMPORT TABLESPACE 會使用這個二進制文件對導入過程進行校驗。
對于 MySQL 5.6.8版本, ALTER TABLE ...IMPORT TABLESPACE 命令不再一定需要一個擴展名為.cfg二進制文件了。但如果真的沒有這個文件我們會收到下面這樣一個警告。
Message:InnoDB: IO Read error: (2, No such file or directory) Error opening './
test/t.cfg',will attempt to import without schema verification
1row in set (0.00 sec)
這個特性有時候還是很有用的。比如,在模式不匹配的導入過程中,或者在一些需要恢復的情景下,元數據又不能從.ibd文件獲得,則這個命令不需要一個擴展名為.cfg的二進制文件就可以導入的特性就很有用。
可遷移表空間的限制:
innodb_file_per_table 一定要打開成 ON. 在共享表空間上的表不能使用這個特性。
當表處理靜默狀態時,只有只讀語句可以使用這張表。
當導入表空間時,目的庫的頁尺寸要和源庫的頁尺寸相匹配。
DISCARD TABLESPACE 不支持分區表。如果你在分區表上使用命令 ALTER TABLE ... DISCARD TABLESPACE 你會看到如下錯誤: ERROR 1031 (HY000): 表引擎沒有這個選項。
DISCARD TABLESPACE 命令不支持有父子關系的表。如果 foreign_key_checks 被設置成1. 在使用命令之前我們可以將這一參數設置為0. foreign_key_checks=0.
ALTER TABLE ... IMPORT TABLESPACE 命令在導入表時不會檢查主外鍵關系。
如果是實時復制的時候, innodb_file_per_table 必需在主服務和從服務上設置為ON。
下面來看一個實例:
在源服務器上我們來對city表進行遷移:
1. mysql> use test;C:/C:/ProgramData/MySQL/MySQLServer 5.6/data/world2. C:/ProgramData/MySQL/MySQLServer 5.6/data/world>dir3. Volume in drive C has no label.4. Volume Serial Number is D0FA-F7A05. Directory of C:/ProgramData/MySQL/MySQL Server5.6/data/world6. 10/08/2013 03:15 PM.7. 10/08/2013 03:15 PM ..8. 10/08/2013 03:15 PM 8,710 city.frm9. 10/08/2013 03:15 PM 273,293 city.MYD10.10/08/2013 03:15 PM 43,008 city.MYI11.10/08/2013 03:15 PM 9,172 country.frm12.10/08/2013 03:15 PM 0 country.MYD13.10/08/2013 03:15 PM 5,120 country.MYI14.10/08/2013 03:15 PM 8,702 countrylanguage.frm15.10/08/2013 03:15 PM 38,376 countrylanguage.MYD16.10/08/2013 03:15 PM 18,432 countrylanguage.MYI17.10/08/2013 03:15 PM 61 db.opt18. 10File(s) 404,874 bytes19. 2 Dir(s) 224,709,537,792 bytes free20.mysql> use world21.Database changed22.mysql> show tables;23.+-----------------+24.| Tables_in_world |25.+-----------------+26.| city |27.| country |28.| countrylanguage |29.+-----------------+30.3 rows in set (0.00 sec)31.mysql> flush table cityfor export;32.ERROR 1031 (HY000): Table storage engine for 'city' doesn't havethis option33.mysql> alter table cityengine=innodb;34.mysql> flush table cityfor export; --對表加鎖。35.Query OK, 0 rows affected (0.18 sec)36.
復制表文件到目標位置
C:/ProgramData/MySQL/MySQL Server 5.6/data/world>mkdir city
C:/ProgramData/MySQL/MySQL Server 5.6/data/world>copy city.* city
city.cfg
city.frm
city.ibd
3 file(s) copied.
C:/ProgramData/MySQL/MySQL Server 5.6/data/world>cd city
C:/ProgramData/MySQL/MySQL Server 5.6/data/world/city>dir
Volume in drive C has no label.
Volume Serial Number is D0FA-F7A0
Directory of C:/ProgramData/MySQL/MySQL Server 5.6/data/world/city
10/10/2013 10:58 AM
10/10/2013 10:58 AM
10/10/2013 10:53 AM 582 city.cfg
10/10/2013 10:53 AM 8,710 city.frm
10/10/2013 10:53 AM 475,136 city.ibd
3 File(s) 484,428 bytes
2 Dir(s) 224,676,024,320 bytes free
在目標庫上刪除可能存在的同名表空間。
mysql> unlock tables;--釋放鎖。2. Query OK, 0 rowsaffected (0.07 sec)3. mysql> alter table city discard tablespace;刪除可能存在的同名表空間4. Query OK, 0 rowsaffected (0.23 sec)5. mysql> selectcount(*) from city;6. ERROR 1814 (HY000):Tablespace has been discarded for table 'city'7. mysql> alter tablecity import tablespace;8. ERROR 1146 (42S02):Table 'world.city' doesn't exist9. C:/ProgramData/MySQL/MySQLServer 5.6/data/world/city>copy city.* ..10.city.cfg11.city.frm12.Overwrite ../city.frm? (Yes/No/All): yes13.Access is denied.14.city.ibd15. 2 file(s) copied.16.C:/ProgramData/MySQL/MySQL Server 5.6/data/world/city>17.mysql> alter table city import tablespace;18.Query OK, 0 rows affected (0.94 sec)19.mysql> select count(*) from city;20.+----------+21.| count(*) |22.+----------+23.| 4079 |24.+----------+25.1 row in set (0.08 sec)
表空間被成功。
bitsCN.com聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com