MySQL 拷貝一個(gè)InnoDB分區(qū)表到另一個(gè)實(shí)例
拷貝一個(gè)innodb分區(qū)表到另一個(gè)實(shí)例
這個(gè)過(guò)程演示了如何將一個(gè)innodb分區(qū)表從一個(gè)正在運(yùn)行的Mysql服務(wù)器實(shí)例復(fù)制到另一個(gè)正在運(yùn)行的實(shí)例。同樣的過(guò)程,只要稍微做些調(diào)整,就可以在同一個(gè)實(shí)例上對(duì)InnoDB分區(qū)表執(zhí)行完全恢復(fù)。
1.在源實(shí)例上,如果不存在分區(qū)表,則創(chuàng)建分區(qū)表。在下面的例子中,創(chuàng)建了一個(gè)包含三個(gè)分區(qū)(p0, p1, p2)的表
mysql> use test;
Database changed
mysql> create table t1(i int) engine=innodb partition by key(i) partitions 3;
Query OK, 0 rows affected (0.38 sec)
mysql> insert into t1 values(1),(2),(3),(4),(5),(6),(7),(8),(9);
Query OK, 9 rows affected (0.03 sec)
Records: 9 Duplicates: 0 Warnings: 0
mysql> select * from t1;
±-----+
| i |
±-----+
| 4 |
| 5 |
| 1 |
| 6 |
| 7 |
| 2 |
| 3 |
| 8 |
| 9 |
±-----+
9 rows in set (0.00 sec)
在/Mysqldata/mysql/test目錄中,對(duì)于三個(gè)分區(qū)都有一個(gè)單獨(dú)的表空間(.ibd)文件:
[root@localhost ~]# cd /mysqldata/mysql/test
[root@localhost test]# ls -lrt
總用量 304
-rw-r-----. 1 mysql mysql 67 3月 15 16:53 db.opt
-rw-r-----. 1 mysql mysql 8554 3月 16 15:43 t1.frm
-rw-r-----. 1 mysql mysql 98304 3月 16 15:43 t1#P#p1.ibd
-rw-r-----. 1 mysql mysql 98304 3月 16 15:43 t1#P#p2.ibd
-rw-r-----. 1 mysql mysql 98304 3月 16 15:43 t1#P#p0.ibd
2.在目標(biāo)實(shí)例上,創(chuàng)建相同的分區(qū)表:
mysql> use test;
Database changed
mysql> create table t1(i int) engine=innodb partition by key(i) partitions 3;
Query OK, 0 rows affected (0.20 sec)
在/mysqldata/mysql/test目錄中,對(duì)于三個(gè)分區(qū)都有一個(gè)單獨(dú)的表空間(.ibd)文件:
[root@localhost ~]# cd /mysqldata/mysql/test
[root@localhost test]# ls -lrt
總用量 304
-rw-r-----. 1 mysql mysql 67 3月 15 16:55 db.opt
-rw-r-----. 1 mysql mysql 8554 3月 16 15:45 t1.frm
-rw-r-----. 1 mysql mysql 98304 3月 16 15:45 t1#P#p0.ibd
-rw-r-----. 1 mysql mysql 98304 3月 16 15:45 t1#P#p1.ibd
-rw-r-----. 1 mysql mysql 98304 3月 16 15:45 t1#P#p2.ibd
3.在目標(biāo)實(shí)例上,丟棄分區(qū)表的表空間。(在將表空間導(dǎo)入目標(biāo)實(shí)例之前,必須丟棄附加到接收表的表空間。)
mysql> alter table t1 discard tablespace;
Query OK, 0 rows affected (0.09 sec)
組成分區(qū)表表空間的三個(gè).ibd文件從/mysqldata/mysql/tes目錄中被丟棄,留下以下文件
[root@localhost ~]# cd /mysqldata/mysql/test
[root@localhost test]# ls -lrt
總用量 16
-rw-r-----. 1 mysql mysql 67 3月 15 16:55 db.opt
-rw-r-----. 1 mysql mysql 8554 3月 16 15:45 t1.frm
4.在源實(shí)例上,運(yùn)行FLUSH TABLES… FOR EXPORT用于暫停分區(qū)表并創(chuàng)建.cfg元數(shù)據(jù)文件
mysql> flush tables t1 for export;
Query OK, 0 rows affected (0.01 sec)
在源實(shí)例的/mysqldata/mysql/test目錄中創(chuàng)建元數(shù)據(jù)(.cfg)文件,每個(gè)表空間(.ibd)文件對(duì)應(yīng)一個(gè)元數(shù)據(jù)文件
[root@localhost ~]# cd /mysqldata/mysql/test
[root@localhost test]# ls -lrt
總用量 316
-rw-r-----. 1 mysql mysql 67 3月 15 16:53 db.opt
-rw-r-----. 1 mysql mysql 8554 3月 16 15:43 t1.frm
-rw-r-----. 1 mysql mysql 98304 3月 16 15:43 t1#P#p1.ibd
-rw-r-----. 1 mysql mysql 98304 3月 16 15:43 t1#P#p2.ibd
-rw-r-----. 1 mysql mysql 98304 3月 16 15:43 t1#P#p0.ibd
-rw-r-----. 1 mysql mysql 375 3月 16 16:00 t1#P#p1.cfg
-rw-r-----. 1 mysql mysql 375 3月 16 16:00 t1#P#p0.cfg
-rw-r-----. 1 mysql mysql 375 3月 16 16:00 t1#P#p2.cfg
FLUSH TABLES……FOR EXPORT語(yǔ)句確保對(duì)指定表的更改已刷新到磁盤,以便在實(shí)例運(yùn)行時(shí)可以進(jìn)行二進(jìn)制表拷貝。當(dāng)運(yùn)行FLUSH TABLES … FOR EXPORT時(shí),InnoDB會(huì)在數(shù)據(jù)庫(kù)目錄中為表的表空間文件生成一個(gè).cfg元數(shù)據(jù)文件。.cfg文件中包含導(dǎo)入表空間文件時(shí)驗(yàn)證模式的元數(shù)據(jù)。FLUSH TABLES … FOR EXPORT只能在表上運(yùn)行,而不能在單獨(dú)的表分區(qū)上運(yùn)行。
5.將.ibd和.cfg文件從源實(shí)例數(shù)據(jù)庫(kù)目錄復(fù)制到目標(biāo)實(shí)例數(shù)據(jù)庫(kù)目錄。例如
[root@localhost test]# scp t1*.{ibd,cfg} mysql@192.168.1.243:/mysqldata/mysql/test/
mysql@192.168.1.243’s password:
t1#P#p0.ibd 100% 96KB 96.0KB/s 00:00
t1#P#p1.ibd 100% 96KB 96.0KB/s 00:00
t1#P#p2.ibd 100% 96KB 96.0KB/s 00:00
t1#P#p0.cfg 100% 375 0.4KB/s 00:00
t1#P#p1.cfg 100% 375 0.4KB/s 00:00
t1#P#p2.cfg 100% 375 0.4KB/s 00:00
[root@localhost test]#
[root@localhost test]# ls -lrt
總用量 316
-rw-r-----. 1 mysql mysql 67 3月 15 16:55 db.opt
-rw-r-----. 1 mysql mysql 8554 3月 16 15:45 t1.frm
-rw-r-----. 1 mysql mysql 98304 3月 16 16:06 t1#P#p0.ibd
-rw-r-----. 1 mysql mysql 98304 3月 16 16:06 t1#P#p1.ibd
-rw-r-----. 1 mysql mysql 98304 3月 16 16:06 t1#P#p2.ibd
-rw-r-----. 1 mysql mysql 375 3月 16 16:06 t1#P#p0.cfg
-rw-r-----. 1 mysql mysql 375 3月 16 16:06 t1#P#p1.cfg
-rw-r-----. 1 mysql mysql 375 3月 16 16:06 t1#P#p2.cfg
6.在源實(shí)例上,使用unlock tables語(yǔ)句來(lái)釋放由flush tables … for export所獲取的鎖:
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
在源實(shí)例上釋放鎖時(shí),會(huì)向mysql日志文件寫入刪除.cfg文件的信息:
2022-03-16T08:08:27.653352Z 10 [Note] InnoDB: Deleting the meta-data file ‘./test/t1#P#p0.cfg’
2022-03-16T08:08:27.653656Z 10 [Note] InnoDB: Deleting the meta-data file ‘./test/t1#P#p1.cfg’
2022-03-16T08:08:27.654214Z 10 [Note] InnoDB: Deleting the meta-data file ‘./test/t1#P#p2.cfg’
2022-03-16T08:08:27.654256Z 10 [Note] InnoDB: Resuming purge
7.在目標(biāo)實(shí)例上,導(dǎo)入表空間:
mysql> select * from t1;
ERROR 1814 (HY000): Tablespace has been discarded for table ‘t1’
mysql> alter table t1 discard tablespace;
Query OK, 0 rows affected (0.09 sec)
mysql> alter table t1 import tablespace;
Query OK, 0 rows affected (0.46 sec)
mysql> select * from t1;
±-----+
| i |
±-----+
| 4 |
| 5 |
| 1 |
| 6 |
| 7 |
| 2 |
| 3 |
| 8 |
| 9 |
±-----+
9 rows in set (0.01 sec)
MySQL
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。