咱們知道啓用innodb_file_per_table選項後,單個表(InnoDB引擎)的數據和索引放入單獨的文件中(.ibd),建表語句保存在.frm文件中
本文假設192.168.85.132,3306實例沒法啓動(磁盤還能訪問),須要將原實例下的replcrash.py_user表恢復到192.168.85.132,3308實例html
mysqlfrm能夠讀取.frm文件並從該文件中找到表定義數據生成CREATE語句。在大多數狀況下,生成的CREATE語句用於在另外一個服務器上建立表或進行診斷等。python
默認模式,使用--basedir或者--server選項生成一個臨時實例,該模式須要指定--port選項給臨時實例使用,該端口不能與現有的實例衝突。在讀取.frm文件後,臨時實例將被關閉,全部的臨時文件將被刪除。
診斷模式,使用--diagnostic選項。byte-by-byte讀取.frm文件,儘量多的恢復信息。
診斷模式可能遇到的問題
• 不能解讀character set/collation values,這會影響到多字節字符列大小
• 不能讀取默認值
• 生成的create語句可能會有語法錯誤
當使用默認模式沒法讀取文件,或者服務器上沒有安裝MySQL實例時就使用診斷模式~~mysql
1、安裝 # mysql-utilities依賴mysql-connector-python >= 2.0.0 [root@ZST1 tools]# wget https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.1.7-1.el6.x86_64.rpm [root@ZST1 tools]# wget https://dev.mysql.com/get/Downloads/MySQLGUITools/mysql-utilities-1.6.5-1.el6.noarch.rpm # 安裝 [root@ZST1 tools]# yum localinstall mysql-connector-python-2.1.7-1.el6.x86_64.rpm -y [root@ZST1 tools]# yum localinstall mysql-utilities-1.6.5-1.el6.noarch.rpm -y 2、使用 # default mode # --basedir [root@ZST1 ~]# mysqlfrm --basedir=/usr/local/mysql/ /data/mysql/mysql3306/data/replcrash/py_user.frm --port=3333 --user=root # --server [root@ZST1 ~]# mysqlfrm --server=mydba:mysql5719@192.168.85.132:3306 /data/mysql/mysql3308/data/replcrash/py_user.frm --port=3333 --user=root 官方不推薦使用root用戶Running spawned server # diagnostic mode [root@ZST1 ~]# mysqlfrm --diagnostic /data/mysql/mysql3308/data/replcrash/py_user.frm
故障實例OFFLINE狀態,建議使用--basedir獲得建表語句sql
在命令行末尾添加 -vvv 能夠查看執行詳細步驟數據庫
[root@ZST1 ~]# mysqlfrm --basedir=/usr/local/mysql/ /data/mysql/mysql3306/data/replcrash/py_user.frm --port=3333 --user=root -vvv # Checking read access to .frm files # Creating a temporary datadir = /tmp/tmpktcZWc # Spawning server with --user=root. # Starting the spawned server on port 3333 ... # Cloning the MySQL server located at /usr/local/mysql. # Configuring new instance... # Locating mysql tools... # Location of files: # mysqld: /usr/local/mysql/bin/mysqld # mysqladmin: /usr/local/mysql/bin/mysqladmin # Setting up empty database and mysql tables... 2017-12-28T09:36:22.383542Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2017-12-28T09:36:24.528222Z 0 [Warning] InnoDB: New log files created, LSN=45790 2017-12-28T09:36:24.936974Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2017-12-28T09:36:25.019189Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 925a5f84-ebb2-11e7-a13e-000c29c1025c. 2017-12-28T09:36:25.024957Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2017-12-28T09:36:25.025657Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. # Starting new instance of the server... # Startup command for new server: /usr/local/mysql/bin/mysqld --no-defaults --basedir=/usr/local/mysql --datadir=/tmp/tmpktcZWc --pid-file=/tmp/tmpktcZWc/clone.pid --port=3333 --server-id=101 --socket=/tmp/tmpktcZWc/mysql.sock --tmpdir=/tmp/tmpktcZWc --user=root # Testing connection to new instance... 2017-12-28T09:36:29.346999Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2017-12-28T09:36:29.369718Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled 2017-12-28T09:36:29.369837Z 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.7.19) starting as process 9382 ... 2017-12-28T09:36:29.387216Z 0 [Note] InnoDB: PUNCH HOLE support available 2017-12-28T09:36:29.387287Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2017-12-28T09:36:29.387309Z 0 [Note] InnoDB: Uses event mutexes 2017-12-28T09:36:29.387334Z 0 [Note] InnoDB: GCC builtin __sync_synchronize() is used for memory barrier 2017-12-28T09:36:29.387353Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3 2017-12-28T09:36:29.387372Z 0 [Note] InnoDB: Using Linux native AIO 2017-12-28T09:36:29.388729Z 0 [Note] InnoDB: Number of pools: 1 2017-12-28T09:36:29.389559Z 0 [Note] InnoDB: Using CPU crc32 instructions 2017-12-28T09:36:29.396790Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M 2017-12-28T09:36:29.413527Z 0 [Note] InnoDB: Completed initialization of buffer pool 2017-12-28T09:36:29.419969Z 0 [Note] InnoDB: page_cleaner coordinator priority: -20 2017-12-28T09:36:29.434005Z 0 [Note] InnoDB: Highest supported file format is Barracuda. 2017-12-28T09:36:29.503503Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables 2017-12-28T09:36:29.503705Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ... 2017-12-28T09:36:30.110625Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB. 2017-12-28T09:36:30.113767Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active. 2017-12-28T09:36:30.113804Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active. 2017-12-28T09:36:30.114361Z 0 [Note] InnoDB: Waiting for purge to start # trying again... 2017-12-28T09:36:30.165192Z 0 [Note] InnoDB: 5.7.19 started; log sequence number 2539315 2017-12-28T09:36:30.165487Z 0 [Note] Plugin 'FEDERATED' is disabled. 2017-12-28T09:36:30.175080Z 0 [Note] InnoDB: Loading buffer pool(s) from /tmp/tmpktcZWc/ib_buffer_pool 2017-12-28T09:36:30.176769Z 0 [Warning] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key 2017-12-28T09:36:30.176801Z 0 [Note] Server hostname (bind-address): '*'; port: 3333 2017-12-28T09:36:30.176922Z 0 [Note] IPv6 is available. 2017-12-28T09:36:30.176941Z 0 [Note] - '::' resolves to '::'; 2017-12-28T09:36:30.176987Z 0 [Note] Server socket created on IP: '::'. 2017-12-28T09:36:30.190355Z 0 [Note] InnoDB: Buffer pool(s) load completed at 171228 17:36:30 2017-12-28T09:36:30.211166Z 0 [Note] Event Scheduler: Loaded 0 events 2017-12-28T09:36:30.211365Z 0 [Note] /usr/local/mysql/bin/mysqld: ready for connections. Version: '5.7.19' socket: '/tmp/tmpktcZWc/mysql.sock' port: 3333 MySQL Community Server (GPL) 2017-12-28T09:36:30.211377Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check. 2017-12-28T09:36:30.211381Z 0 [Note] Beginning of list of non-natively partitioned tables 2017-12-28T09:36:30.241025Z 0 [Note] End of list of non-natively partitioned tables # trying again... # Success! # Setting the root password... mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. # Connection Information: # -uroot -proot --socket=/tmp/tmpktcZWc/mysql.sock #...done. 2017-12-28T09:36:31.297094Z 3 [Note] Aborted connection 3 to db: 'unconnected' user: 'root' host: 'localhost' (Got an error reading communication packets) # Connecting to spawned server done. # Reading .frm files # # Reading the py_user.frm file. # Changing engine for .frm file /tmp/tmpktcZWc/replcrash_temp/py_user.frm: # Skipping to header at : 2 # General Data from .frm file: {'IO_SIZE': 4096, 'MYSQL_VERSION_ID': 50719, 'avg_row_length': 0, 'charset_low': 0, 'create_options': 9, 'db_create_pack': 2, 'default_charset': 33, 'default_part_eng': 0, 'extra_size': 33, 'frm_file_ver': 5, 'frm_version': 10, 'key_block_size': 0, 'key_info_length': 33, 'key_length': 361, 'legacy_db_type': 'INNODB', 'length': 12288, 'max_rows': 0, 'min_rows': 0, 'rec_length': 138, 'row_type': 0, 'table_charset': 33, 'tmp_key_length': 361} # Engine string: InnoDB # Server version in file: 5.7.19 # # CREATE statement for /data/mysql/mysql3306/data/replcrash/py_user.frm: # CREATE TABLE `replcrash`.`py_user` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) DEFAULT NULL, `add_time` datetime DEFAULT NULL, `server_id` varchar(10) DEFAULT NULL, PRIMARY KEY (`uid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 # Shutting down spawned server # Removing the temporary datadir 2017-12-28T09:36:31.471827Z 0 [Note] Giving 0 client threads a chance to die gracefully 2017-12-28T09:36:31.471874Z 0 [Note] Shutting down slave threads 2017-12-28T09:36:31.471891Z 0 [Note] Forcefully disconnecting 0 remaining clients 2017-12-28T09:36:31.471908Z 0 [Note] Event Scheduler: Purging the queue. 0 events 2017-12-28T09:36:31.473626Z 0 [Note] Binlog end 2017-12-28T09:36:31.473789Z 0 [Note] Shutting down plugin 'ngram' 2017-12-28T09:36:31.473820Z 0 [Note] Shutting down plugin 'BLACKHOLE' 2017-12-28T09:36:31.473839Z 0 [Note] Shutting down plugin 'ARCHIVE' 2017-12-28T09:36:31.473853Z 0 [Note] Shutting down plugin 'partition' 2017-12-28T09:36:31.473866Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL' 2017-12-28T09:36:31.473880Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES' 2017-12-28T09:36:31.473893Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES' 2017-12-28T09:36:31.473905Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS' 2017-12-28T09:36:31.473918Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN' 2017-12-28T09:36:31.473931Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS' 2017-12-28T09:36:31.473943Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS' 2017-12-28T09:36:31.473956Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES' 2017-12-28T09:36:31.473969Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS' 2017-12-28T09:36:31.473981Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES' 2017-12-28T09:36:31.474010Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE' 2017-12-28T09:36:31.474024Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE' 2017-12-28T09:36:31.474036Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG' 2017-12-28T09:36:31.474156Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED' 2017-12-28T09:36:31.474175Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED' 2017-12-28T09:36:31.474188Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD' 2017-12-28T09:36:31.474214Z 0 [Note] Shutting down plugin 'INNODB_METRICS' 2017-12-28T09:36:31.474227Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO' 2017-12-28T09:36:31.474241Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS' 2017-12-28T09:36:31.474256Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU' 2017-12-28T09:36:31.474269Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE' 2017-12-28T09:36:31.474281Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET' 2017-12-28T09:36:31.474303Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX' 2017-12-28T09:36:31.474317Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET' 2017-12-28T09:36:31.474330Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM' 2017-12-28T09:36:31.474343Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET' 2017-12-28T09:36:31.474356Z 0 [Note] Shutting down plugin 'INNODB_CMP' 2017-12-28T09:36:31.474369Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS' 2017-12-28T09:36:31.474382Z 0 [Note] Shutting down plugin 'INNODB_LOCKS' 2017-12-28T09:36:31.474395Z 0 [Note] Shutting down plugin 'INNODB_TRX' 2017-12-28T09:36:31.474407Z 0 [Note] Shutting down plugin 'InnoDB' 2017-12-28T09:36:31.474590Z 0 [Note] InnoDB: FTS optimize thread exiting. 2017-12-28T09:36:31.476410Z 0 [Note] InnoDB: Starting shutdown... 2017-12-28T09:36:31.577509Z 0 [Note] InnoDB: Dumping buffer pool(s) to /tmp/tmpktcZWc/ib_buffer_pool 2017-12-28T09:36:31.577612Z 0 [ERROR] InnoDB: Cannot open '/tmp/tmpktcZWc/ib_buffer_pool.incomplete' for writing: No such file or directory 2017-12-28T09:36:33.117642Z 0 [Note] InnoDB: Shutdown completed; log sequence number 2539334 2017-12-28T09:36:33.117712Z 0 [Note] Shutting down plugin 'MEMORY' 2017-12-28T09:36:33.117727Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA' 2017-12-28T09:36:33.117761Z 0 [Note] Shutting down plugin 'MRG_MYISAM' 2017-12-28T09:36:33.117767Z 0 [Note] Shutting down plugin 'MyISAM' 2017-12-28T09:36:33.117783Z 0 [Note] Shutting down plugin 'CSV' 2017-12-28T09:36:33.117790Z 0 [Note] Shutting down plugin 'sha256_password' 2017-12-28T09:36:33.117794Z 0 [Note] Shutting down plugin 'mysql_native_password' 2017-12-28T09:36:33.117966Z 0 [Note] Shutting down plugin 'binlog' 2017-12-28T09:36:33.118884Z 0 [Note] Unable to delete pid file: No such file or directory 2017-12-28T09:36:33.118898Z 0 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete [root@ZST1 ~]#
• 檢查讀取.frm文件的權限
• 建立臨時datadir
• 克隆生成新實例
• 啓動、鏈接新實例
• 讀取.frm文件,生成create語句
• 關閉新實例,刪除臨時datadir
mysqlfrm的使用參考:5.13 mysqlfrm — File reader for .frm files、3.3.2 How do you recover the CREATE statement from a damaged or offline server?服務器
這裏用到表空間傳輸,恢復環境使用mysqlfrm獲得語句建立相同結構的數據表,而後discard表空間,拷貝故障實例下的ibd文件並修改權限,從新載入表空間,載入過程會有警告,但不影響數據的讀取socket
# 將mysqlfrm獲得建表語句拷貝到192.168.85.132,3308上執行 mydba@192.168.85.132,3308 [(none)]> use replcrash; mydba@192.168.85.132,3308 [replcrash]> drop table if exists py_user; # 執行mysqlfrm獲得的create語句 mydba@192.168.85.132,3308 [replcrash]> CREATE TABLE `replcrash`.`py_user` ( -> `uid` int(11) NOT NULL AUTO_INCREMENT, -> `name` varchar(32) DEFAULT NULL, -> `add_time` datetime DEFAULT CURRENT_TIMESTAMP, -> `server_id` varchar(10) DEFAULT NULL, -> PRIMARY KEY (`uid`) -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.17 sec) # discard 表空間 mydba@192.168.85.132,3308 [replcrash]> alter table py_user discard tablespace; Query OK, 0 rows affected (0.86 sec) # 拷貝ibd文件,並修改權限 [root@ZST1 ~]# scp /data/mysql/mysql3306/data/replcrash/py_user.ibd 192.168.85.132:/data/mysql/mysql3308/data/replcrash/ [root@ZST1 ~]# chown -R mysql:mysql /data/mysql/mysql3308/data/replcrash/py_user.ibd # 載入表空間 mydba@192.168.85.132,3308 [replcrash]> alter table py_user import tablespace; Query OK, 0 rows affected, 1 warning (0.24 sec) mydba@192.168.85.132,3308 [replcrash]> show warnings; +---------+------+---------------------------------------------------------------------------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+---------------------------------------------------------------------------------------------------------------------------------------------------+ | Warning | 1810 | InnoDB: IO Read error: (2, No such file or directory) Error opening './replcrash/py_user.cfg', will attempt to import without schema verification | +---------+------+---------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) # 查詢數據是否一致 mydba@192.168.85.132,3308 [replcrash]> select * from py_user; +-----+------------------------------+---------------------+-----------+ | uid | name | add_time | server_id | +-----+------------------------------+---------------------+-----------+ | 1 | U4F9ZKZTJ9GK6RFV4A3EEJ | 2017-12-21 10:46:44 | 1323306 | | 2 | IB1FSWGFEH5CGIT4FWEZK81V | 2017-12-21 10:47:14 | 1323306 | | 3 | 9N9TF1C8SEGKXNHBUE6CPOXD0IUU | 2017-12-21 10:47:15 | 1323306 | | 4 | TEBD44BM3E0NZPSIVGI79Z | 2017-12-21 10:47:16 | 1323306 | | 5 | XS8Z2W2XIDIB5Y4TMF78L9INM7 | 2017-12-21 10:47:17 | 1323306 | | 6 | OP65BAJ6MNI1W02PNMJGHXX | 2017-12-21 10:47:18 | 1323306 | | 7 | 19R173XU7QXRPG21413BYMWMY1 | 2017-12-21 10:47:19 | 1323306 | | 8 | 768NGEAGK0VJ57MW98SG | 2017-12-21 10:58:02 | 1323306 | | 9 | 9EDYG3U7LFL80WIDKSV5JTI | 2017-12-21 11:03:41 | 1323306 | +-----+------------------------------+---------------------+-----------+ 9 rows in set (0.00 sec)
該功能在MySQL5.6後引入,針對InnoDB獨立表空間,能夠把一個表Copy到另外一個實例ide
使用該功能前提:
• 使用innodb_file_per_table
• 在作表導出時(export->copy data),只容許read-only事務
• 導入導出的數據庫data page size必須一致
• 在MySQL 5.7.4之前不能對分區表作分區遷移
• 對於使用外鍵的表,導入導出不支持,能夠經過set foreign_key_check=0;強制忽略。但對於分區表,目前不能進行操做
前面從新載入表空間時出現了警告,是由於咱們假設3306實例出現故障,數據庫沒法啓動,所以不能按照正常的表空間傳輸步驟操做ui
# InnoDB表空間傳輸遷移數據 1、遷出實例3306 mydba@192.168.85.132,3306 [(none)]> use replcrash; mydba@192.168.85.132,3306 [replcrash]> select count(*) from py_user; mydba@192.168.85.132,3306 [replcrash]> show create table py_user; 2、遷入實例3308 # 建立表 mydba@192.168.85.132,3308 [(none)]> use replcrash; mydba@192.168.85.132,3308 [replcrash]> CREATE TABLE `py_user` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) DEFAULT NULL, `add_time` datetime DEFAULT CURRENT_TIMESTAMP, `server_id` varchar(10) DEFAULT NULL, PRIMARY KEY (`uid`) ) ENGINE=InnoDB; # 查看相關文件 [root@ZST1 ~]# ll /data/mysql/mysql3308/data/replcrash/py_user* -rw-r-----. 1 mysql mysql 8666 Dec 20 16:57 /data/mysql/mysql3308/data/replcrash/py_user.frm -rw-r-----. 1 mysql mysql 98304 Dec 20 16:57 /data/mysql/mysql3308/data/replcrash/py_user.ibd [root@ZST1 ~]# # discard 表空間,.ibd文件會被刪除 mydba@192.168.85.132,3308 [replcrash]> alter table py_user discard tablespace; # 再次查看相關文件 [root@ZST1 ~]# ll /data/mysql/mysql3308/data/replcrash/py_user* -rw-r-----. 1 mysql mysql 8666 Dec 20 16:57 /data/mysql/mysql3308/data/replcrash/py_user.frm [root@ZST1 ~]# 3、遷出實例3306 mydba@192.168.85.132,3306 [replcrash]> flush tables py_user for export; 這個時候會加表鎖,只能讀不能寫,同時會產生一個py_user.cfg文件 [root@ZST1 ~]# ll /data/mysql/mysql3306/data/replcrash/py_user* -rw-r-----. 1 mysql mysql 517 Dec 20 17:03 /data/mysql/mysql3306/data/replcrash/py_user.cfg -rw-r-----. 1 mysql mysql 8666 Nov 30 09:23 /data/mysql/mysql3306/data/replcrash/py_user.frm -rw-r-----. 1 mysql mysql 376832 Dec 20 17:03 /data/mysql/mysql3306/data/replcrash/py_user.ibd [root@ZST1 ~]# # 將.idb、.cfg拷貝到遷入實例對應目錄 [root@ZST1 ~]# scp /data/mysql/mysql3306/data/replcrash/py_user.{cfg,ibd} 192.168.85.132:/data/mysql/mysql3308/data/replcrash/ # 解除鎖定 mydba@192.168.85.132,3306 [replcrash]> unlock tables; 4、遷入實例3308 # 修改文件權限 [root@ZST1 ~]# chown mysql.mysql /data/mysql/mysql3308/data/replcrash/py_user.{ibd,cfg} # 導入表空間文件 mydba@192.168.85.132,3308 [replcrash]> alter table py_user import tablespace; 到這裏整個innodb表就被遷移完成,比起邏輯(mysqldump)或物理備份(xtrabackup)方便不少
知曉基於.frm、.ibd恢復數據,再去閱讀從完整備份恢復單個innodb表就簡單了this
# InnoDB表空間傳輸遷移數據(分區表) 1、遷出實例3306 mydba@192.168.85.132,3306 [(none)]> use replcrash; mydba@192.168.85.132,3306 [replcrash]> select count(*) from py_user_partition; mydba@192.168.85.132,3306 [replcrash]> show create table py_user_partition; 2、遷入實例3308 # 建立表 mydba@192.168.85.132,3308 [(none)]> use replcrash; mydba@192.168.85.132,3308 [replcrash]> CREATE TABLE `py_user_partition` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) DEFAULT NULL, `add_time` datetime DEFAULT CURRENT_TIMESTAMP, `server_id` varchar(10) DEFAULT NULL, PRIMARY KEY (`uid`) ) ENGINE=InnoDB PARTITION BY KEY(uid) PARTITIONS 4; # 查看相關文件 [root@ZST1 ~]# ll -h /data/mysql/mysql3308/data/replcrash/py_user_partition* -rw-r-----. 1 mysql mysql 8.5K Jan 6 13:16 /data/mysql/mysql3308/data/replcrash/py_user_partition.frm -rw-r-----. 1 mysql mysql 96K Jan 6 13:16 /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p0.ibd -rw-r-----. 1 mysql mysql 96K Jan 6 13:16 /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p1.ibd -rw-r-----. 1 mysql mysql 96K Jan 6 13:16 /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p2.ibd -rw-r-----. 1 mysql mysql 96K Jan 6 13:16 /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p3.ibd [root@ZST1 ~]# # discard p0分區表空間,p0.ibd文件會被刪除 mydba@192.168.85.132,3308 [replcrash]> alter table py_user_partition discard partition p0 tablespace; # 再次查看相關文件 [root@ZST1 ~]# ll -h /data/mysql/mysql3308/data/replcrash/py_user_partition* -rw-r-----. 1 mysql mysql 8.5K Jan 6 13:16 /data/mysql/mysql3308/data/replcrash/py_user_partition.frm -rw-r-----. 1 mysql mysql 96K Jan 6 13:16 /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p1.ibd -rw-r-----. 1 mysql mysql 96K Jan 6 13:16 /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p2.ibd -rw-r-----. 1 mysql mysql 96K Jan 6 13:16 /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p3.ibd [root@ZST1 ~]# # p0分區不能查詢 mydba@192.168.85.132,3308 [replcrash]> select * from py_user_partition partition(p0); ERROR 1814 (HY000): Tablespace has been discarded for table 'py_user_partition' # 其餘分區能夠查詢 mydba@192.168.85.132,3308 [replcrash]> select * from py_user_partition partition(p1); Empty set (0.00 sec) 3、遷出實例3306 mydba@192.168.85.132,3306 [replcrash]> flush tables py_user_partition for export; 這個時候會加表鎖,只能讀不能寫,同時每個分區會產生一個.cfg文件 [root@ZST1 ~]# ll -h /data/mysql/mysql3306/data/replcrash/py_user_partition* -rw-r-----. 1 mysql mysql 8.5K Jan 6 13:09 /data/mysql/mysql3306/data/replcrash/py_user_partition.frm -rw-r-----. 1 mysql mysql 532 Jan 6 13:24 /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p0.cfg -rw-r-----. 1 mysql mysql 192K Jan 6 13:13 /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p0.ibd -rw-r-----. 1 mysql mysql 532 Jan 6 13:24 /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p1.cfg -rw-r-----. 1 mysql mysql 400K Jan 6 13:13 /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p1.ibd -rw-r-----. 1 mysql mysql 532 Jan 6 13:24 /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p2.cfg -rw-r-----. 1 mysql mysql 192K Jan 6 13:13 /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p2.ibd -rw-r-----. 1 mysql mysql 532 Jan 6 13:24 /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p3.cfg -rw-r-----. 1 mysql mysql 256K Jan 6 13:13 /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p3.ibd [root@ZST1 ~]# # 將p0分區的.idb、.cfg拷貝到遷入實例對應目錄 [root@ZST1 ~]# scp /data/mysql/mysql3306/data/replcrash/py_user_partition#P#p0.{cfg,ibd} 192.168.85.132:/data/mysql/mysql3308/data/replcrash/ # 解除鎖定 mydba@192.168.85.132,3306 [replcrash]> unlock tables; 4、遷入實例3308 # 修改文件權限 [root@ZST1 ~]# chown mysql.mysql /data/mysql/mysql3308/data/replcrash/py_user_partition#P#p0.{ibd,cfg} # 導入表空間文件 mydba@192.168.85.132,3308 [replcrash]> alter table py_user_partition import partition p0 tablespace; # 查看p0分區的數據 mydba@192.168.85.132,3308 [replcrash]> select count(*) from py_user_partition partition(p0); +----------+ | count(*) | +----------+ | 1480 | +----------+ 1 row in set (0.00 sec)
上面是針對某個分區進行表空間傳輸,其餘分區參考操做便可;在使用range分區時,若是隻想恢復特定某個分區,帶上partition_name很是方便
若是要針對整張分區表進行表空間傳輸,只需將關鍵字partition_name替換爲all
mysql> alter table py_user_partition discard partition all tablespace; mysql> alter table py_user_partition import partition all tablespace;