Ubuntu如何利用.ibd文件恢复MySQL数据?

## 背景:服务器中,MySQL程序坏了,也没有做定时备份的操作。为了是数据库恢复到最新的。

## 方法:可以使用MySQL的 .ibd 文件恢复。(需要原数据库的表结构)

## 文件位置:在Ubuntu系统中,默认位置是:/var/lib/mysql      .ibd文件就在这个目录下。

1、单个表恢复的情况

1.1、停止MySQL程序,备份.bd文件(若程序已经无法启动,则直接备份)

1.2、用原来表结构建表。

1.3、删除新建表的表空间。

ALTER TABLE 表名 DISCARD TABLESPACE;

1.4、将备份好的.ibd文件,复制到目标数据库数据ibd文件的目录下,并修改.ibd文件权限。

sudo chmod 777 表名.ibd

1.5、重新导入表空间即可恢复数据(无需重启目标数据库服务)

ALTER TABLE 表名 IMPORT TABLESPACE;

1.6、执行完后,刷新数据库 原来的数据就已经恢复。

2、多个表批量恢复的情况

2.1、批量拼接数据库中所有表的 删除表空间语句。

SELECT concat('alter table ', table_name, ' discard tablespace;') FROM information_schema.tables WHERE table_schema = '数据库名称'; 

±-----------------------------------------------------------+
| concat('alter table ‘, table_name, ’ discard tablespace;’) |
±-----------------------------------------------------------+
| alter table config discard tablespace; |
| alter table sysconfig discard tablespace; |
 

2.2、然后复制下来,执行所有语句。

2.3、批量拼接数据库中所有表的 导入表空间语句。

SELECT concat('alter table ', table_name, ' import tablespace;') FROM information_schema.tables WHERE table_schema = '数据库名';

±----------------------------------------------------------+
| concat('alter table ‘, table_name, ’ import tablespace;’) |
±----------------------------------------------------------+
| alter table config import tablespace; |
| alter table sysconfig import tablespace; |

2.4、然后复制下来,执行所有语句。

2.5、执行完后,刷新数据库 原来的数据就已经恢复。

Preconfiguring packages ... Selecting previously unselected package mysql-common. (Reading database ... 87026 files and directories currently installed.) Preparing to unpack .../0-mysql-common_5.8+1.0.8_all.deb ... Unpacking mysql-common (5.8+1.0.8) ... Selecting previously unselected package mysql-client-core-8.0. Preparing to unpack .../1-mysql-client-core-8.0_8.0.43-0ubuntu0.22.04.2_amd64.deb ... Unpacking mysql-client-core-8.0 (8.0.43-0ubuntu0.22.04.2) ... dpkg: error processing archive /tmp/apt-dpkg-install-6PLoY5/1-mysql-client-core-8.0_8.0.43-0ubuntu0.22.04.2_amd64.deb (--unpack): trying to overwrite '/usr/bin/myisam_ftdump', which is also in package mysql-commercial-server 9.4.0-2.1 dpkg-deb: error: paste subprocess was killed by signal (Broken pipe) Selecting previously unselected package mysql-client-8.0. Preparing to unpack .../2-mysql-client-8.0_8.0.43-0ubuntu0.22.04.2_amd64.deb ... Unpacking mysql-client-8.0 (8.0.43-0ubuntu0.22.04.2) ... Selecting previously unselected package libevent-pthreads-2.1-7:amd64. Preparing to unpack .../3-libevent-pthreads-2.1-7_2.1.12-stable-1build3_amd64.deb ... Unpacking libevent-pthreads-2.1-7:amd64 (2.1.12-stable-1build3) ... Selecting previously unselected package libmecab2:amd64. Preparing to unpack .../4-libmecab2_0.996-14build9_amd64.deb ... Unpacking libmecab2:amd64 (0.996-14build9) ... Selecting previously unselected package libprotobuf-lite23:amd64. Preparing to unpack .../5-libprotobuf-lite23_3.12.4-1ubuntu7.22.04.4_amd64.deb ... Unpacking libprotobuf-lite23:amd64 (3.12.4-1ubuntu7.22.04.4) ... Selecting previously unselected package mysql-server-core-8.0. Preparing to unpack .../6-mysql-server-core-8.0_8.0.43-0ubuntu0.22.04.2_amd64.deb ... Unpacking mysql-server-core-8.0 (8.0.43-0ubuntu0.22.04.2) ... dpkg: error processing archive /tmp/apt-dpkg-install-6PLoY5/6-mysql-server-core-8.0_8.0.43-0ubuntu0.22.04.2_amd64.deb (--unpack): trying to overwrite '/usr/bin/ibd2sdi', which is also in package mysql-commercial-server 9.4.0-2.1 dpkg-deb: error: paste subprocess was killed by signal (Broken pipe) Errors were encountered while processing: /tmp/apt-dpkg-install-6PLoY5/1-mysql-client-core-8.0_8.0.43-0ubuntu0.22.04.2_amd64.deb /tmp/apt-dpkg-install-6PLoY5/6-mysql-server-core-8.0_8.0.43-0ubuntu0.22.04.2_amd64.deb needrestart is being skipped since dpkg has failed E: Sub-process /usr/bin/dpkg returned an error code (1)
09-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值