mysql迁移:ibd表空间迁移库表
问题描述:将一个库中的表迁移到另一个数据库或实例下,利用ibd文件物理迁移,可适用情况为数据库起不来,强制迁移数据文件恢复
将数据库中的zabbix数据迁移到另一个库中
frm:存储表的列信息
ibd:表的数据行和索引
myd 表数据文件
myi 表索引文件
1.备份出来表结构,如果有历史库的情况可备份
mysqldump -uroot -p -S /data/3307/mysql.sock -B zabbix --no-data > /data/zabbix_20210128.sql
2.传到测试库上进行恢复
mysql> source /data/zabbix_20210128.sql ERROR 1813 (HY000): Tablespace '`zabbix`.`Student`' exists. ERROR 1813 (HY000): Tablespace '`zabbix`.`Teacher`' exists. ERROR 1813 (HY000): Tablespace '`zabbix`.`course`' exists.
...
错误原因:
应该在把表结构回复完成后,再把源库的,ibd文件传过来,要不然直接恢复有冲突
3.表结构恢复完成
mysql> show tables; +------------------+ | Tables_in_zabbix | +------------------+ | Student | | Teacher | | city | | course | | department | | stu | | zabbix_table | +------------------+ 7 rows in set (0.00 sec)
4.删除恢复库的表空间
mysql> alter table Student discard tablespace; table discard tablespace;Query OK, 0 rows affected (0.00 sec) mysql> alter table Teacher discard tablespace; Query OK, 0 rows affected (0.01 sec) mysql> alter table city discard tablespace; ERROR 1031 (HY000): Table storage engine for 'city' doesn't have this option mysql> alter table course discard tablespace; Query OK, 0 rows affected (0.00 sec) mysql> alter table department discard tablespace; ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails () mysql> alter table stu discard tablespace; Query OK, 0 rows affected (0.00 sec) mysql> alter table zabbix_table discard tablespace; Query OK, 0 rows affected (0.00 sec)
外键冲突
set foreign_key_checks=0 跳过外键检查。
5.重新删除表空间
mysql> alter table Student discard tablespace; table discard tablespace;Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> alter table Teacher discard tablespace; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> alter table city discard tablespace; ERROR 1031 (HY000): Table storage engine for 'city' doesn't have this option mysql> alter table course discard tablespace; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> alter table department discard tablespace; Query OK, 0 rows affected (0.01 sec) mysql> alter table stu discard tablespace; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> alter table zabbix_table discard tablespace; Query OK, 0 rows affected, 1 warning (0.01 sec)
有一个存储引擎为MyIsam的表不支持这样恢复
6.导入表空间
mysql> alter table Student import tablespace; mport tablespace;Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> alter table Teacher import tablespace; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> alter table city import tablespace; ERROR 1031 (HY000): Table storage engine for 'city' doesn't have this option mysql> alter table course import tablespace; Query OK, 0 rows affected, 1 warning (0.02 sec) mysql> alter table department import tablespace; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> alter table stu import tablespace; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> alter table zabbix_table import tablespace; Query OK, 0 rows affected, 1 warning (0.01 sec)
7.验证导入数据情况
默认存储引擎是Innodb的数据都没有问题,但是之前更改成MyIsam的导入不成功
8.恢复MyIsam存储引擎的表
MyIsam的存储方式:
Innodb的存储方式:
将源库的city表文件传输到恢复库指定位置
[mysql@mysql-test /data/3307/data/zabbix ]$ cp city.frm city.MYD city.MYI /data/3308/data/zabbix/
9.查询验证数据库数据
以MyIsam存储引擎的表可以直接物理迁移