如何在删除ibdata1和ib_logfile的情况下恢复MySQL数据库

时间:2022-09-18 19:30:13

昨天,有个朋友对公司内部使用的一个MySQL实例开启binlog,但是在启动的过程中失败了(他也没提,为何会失败),在启动失败后,他删除了ibdata1和ib_logfile,后来,能正常启动了,但所有的表通过show tables能看到,但是select的过程中却报“Table doesn't exist”。

于是,建议他试试可传输表空间。

同时,自己也测试了下,确实可行。

测试版本 MySQL 5.6.32 社区版

恢复的基本步骤

1. 将原来的数据文件COPY到其它目录下。

2. 创建同名表,表结构必须保持一致。

3. 导出表空间

mysql> ALTER TABLE t DISCARD TABLESPACE;

4. 将原来的数据文件COPY回来

5. 导入表空间

mysql> ALTER TABLE t IMPORT TABLESPACE

下面的演示会略为复杂,主要是还原整个场景,并针对上述步骤中的2,4做了一个测试。

首先,创建测试数据

在这里创建两张表。之所以创建两张相同的表是为了方便后续的测试。

mysql> create table t1(id int,hiredate datetime);
Query OK, 0 rows affected (0.14 sec) mysql> create table t2(id int,hiredate datetime);
Query OK, 0 rows affected (0.01 sec) mysql> insert into t1 values(1,now());
Query OK, 1 row affected (0.06 sec) mysql> insert into t1 values(2,now());
Query OK, 1 row affected (0.00 sec) mysql> insert into t2 values(1,now());
Query OK, 1 row affected (0.00 sec) mysql> insert into t2 values(2,now());
Query OK, 1 row affected (0.00 sec)

关闭数据库

# /usr/test/mysql-5.6.32-linux-glibc2.5-x86_64/bin/mysqladmin shutdown -uroot -p123456 -h127.0.0.1 -P3310

删除ibdata1,ib_logfile0和ib_logfile1

[root@localhost data]# cd /data/
[root@localhost data]# ls
auto.cnf ib_logfile0 localhost.localdomain.err mysql_upgrade_info test
ibdata1 ib_logfile1 mysql performance_schema
[root@localhost data]# rm -rf ibdata1
[root@localhost data]# rm -rf ib_logfile*[root@localhost data]# ls
auto.cnf localhost.localdomain.err mysql mysql_upgrade_info performance_schema test

重新启动数据库

# /usr/test/mysql-5.6.32-linux-glibc2.5-x86_64/bin/mysqld --defaults-file=/usr/test/mysql-5.6.32-linux-glibc2.5-x86_64/my.cnf &

并没有报错

启动过程中的日志信息如下:

# -- ::  [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
-- :: [Note] /usr/test/mysql-5.6.-linux-glibc2.-x86_64/bin/mysqld (mysqld 5.6.) starting as process ...
-- :: [Note] Plugin 'FEDERATED' is disabled.
-- :: [Note] InnoDB: Using atomics to ref count buffer pool pages
-- :: [Note] InnoDB: The InnoDB memory heap is disabled
-- :: [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
-- :: [Note] InnoDB: Memory barrier is not used
-- :: [Note] InnoDB: Compressed tables use zlib 1.2.
-- :: [Note] InnoDB: Using Linux native AIO
-- :: [Note] InnoDB: Using CPU crc32 instructions
-- :: [Note] InnoDB: Initializing buffer pool, size = 128.0M
-- :: [Note] InnoDB: Completed initialization of buffer pool
-- :: [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
-- :: [Note] InnoDB: Setting file ./ibdata1 size to MB
-- :: [Note] InnoDB: Database physically writes the file full: wait...
-- :: [Note] InnoDB: Setting log file ./ib_logfile101 size to MB
-- :: [Note] InnoDB: Setting log file ./ib_logfile1 size to MB
-- :: [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
-- :: [Warning] InnoDB: New log files created, LSN=
-- :: [Note] InnoDB: Doublewrite buffer not found: creating new
-- :: [Note] InnoDB: Doublewrite buffer created
-- :: [Note] InnoDB: rollback segment(s) are active.
-- :: [Warning] InnoDB: Creating foreign key constraint system tables.
-- :: [Note] InnoDB: Foreign key constraint system tables created
-- :: [Note] InnoDB: Creating tablespace and datafile system tables.
-- :: [Note] InnoDB: Tablespace and datafile system tables created.
-- :: [Note] InnoDB: Waiting for purge to start
-- :: [Note] InnoDB: 5.6. started; log sequence number
-- :: [Note] Server hostname (bind-address): '*'; port:
-- :: [Note] IPv6 is available.
-- :: [Note] - '::' resolves to '::';
-- :: [Note] Server socket created on IP: '::'.
-- :: [Note] Event Scheduler: Loaded events
-- :: [Note] /usr/test/mysql-5.6.-linux-glibc2.-x86_64/bin/mysqld: ready for connections.
Version: '5.6.32' socket: '/data/mysql.sock' port: MySQL Community Server (GPL)

可见,在启动的过程中,MySQL会重建ibdata1和redo log。

登录mysql客户端,看之前创建的t1,t2是否能访问

# /usr/test/mysql-5.6.32-linux-glibc2.5-x86_64/bin/mysql -h127.0.0.1 -p123456 -uroot -P3310

mysql> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
| t2 |
+----------------+
2 rows in set (0.00 sec) mysql> select * from t1;
ERROR 1146 (42S02): Table 'test.t1' doesn't exist

通过show tables能查看有t1表存在,但表中的具体内容则无法查看

同时,错误日志中输出以下信息

-- ::  [Warning] InnoDB: Cannot open table test/t1 from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for how you can resolve the problem.

将数据目录下的test目录中的t1,t2表的数据文件和表定义文件COPY到其它地方

[root@localhost test]# cd /data/test/
[root@localhost test]# ll
total
-rw-rw---- mysql mysql Aug : t1.frm
-rw-rw---- mysql mysql Aug : t1.ibd
-rw-rw---- mysql mysql Aug : t2.frm
-rw-rw---- mysql mysql Aug : t2.ibd
[root@localhost test]# mv * /backup/
[root@localhost test]# ls
[root@localhost test]# ll /backup/
total
-rw-rw---- mysql mysql Aug : t1.frm
-rw-rw---- mysql mysql Aug : t1.ibd
-rw-rw---- mysql mysql Aug : t2.frm
-rw-rw---- mysql mysql Aug : t2.ibd

登录客户端,创建t1和t2表,注意表结构和之前的必须保持一致

细心的童鞋会发现,下面的创表语句和刚开始的创表语句并不一样,列名不一致,这个其实是为了后续的测试

mysql> show tables;
Empty set (0.00 sec) mysql> create table t1(id_1 int,hiredate_1 datetime);
ERROR 1146 (42S02): Table 'test.t1' doesn't exist

明明已经手动移除了,为什么创建表的时候还报这个错误呢?

接下来,可先执行个drop table操作

mysql> drop table t1;
ERROR 1051 (42S02): Unknown table 'test.t1'
mysql> create table t1(id_1 int,hiredate_1 datetime);
Query OK, 0 rows affected (0.07 sec)

对于t2表,我们定义一个不同的表结构,看是否可行?

mysql> drop table t2;
ERROR 1051 (42S02): Unknown table 'test.t2'
mysql> create table t2(id_1 int);
Query OK, 0 rows affected (0.01 sec)

导出表空间

mysql> ALTER TABLE t1 DISCARD TABLESPACE;
Query OK, 0 rows affected (0.00 sec) mysql> ALTER TABLE t2 DISCARD TABLESPACE;
Query OK, 0 rows affected (0.00 sec)

这个时候,数据目录下的test目录下,数据文件没有了,只剩下了表结构文件

[root@localhost test]# ls
t1.frm t2.frm

导入表空间

首先对t1表进行测试

在这里,测试如下两种情况

1. 新的t1.frm+旧的t1.ibd

2. 旧的t1.frm+旧的t1.ibd

第一种情况

只是将t1表的数据文件COPY回来

[root@localhost test]# cp /backup/t1.ibd .
[root@localhost test]# chown mysql.mysql t1.ibd

导入t1表的表空间

mysql> ALTER TABLE t1 IMPORT TABLESPACE;
Query OK, 0 rows affected, 1 warning (0.21 sec) mysql> show warnings;
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1810 | InnoDB: IO Read error: (2, No such file or directory) Error opening './test/t2.cfg', will attempt to import without schema verification |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

查看t1表是否能访问

mysql> select * from t1;
+------+---------------------+
| id_1 | hiredate_1 |
+------+---------------------+
| 1 | 2016-08-18 17:45:02 |
| 2 | 2016-08-18 17:45:02 |
+------+---------------------+
2 rows in set (0.00 sec) mysql> flush table t1;
Query OK, 0 rows affected (0.00 sec) mysql> select * from t1;
+------+---------------------+
| id_1 | hiredate_1 |
+------+---------------------+
| 1 | 2016-08-18 17:45:02 |
| 2 | 2016-08-18 17:45:02 |
+------+---------------------+
2 rows in set (0.00 sec)

喔,确实能访问,注意观察,表的列名与新的创表语句保持一致。

在这里之所以使用flush table操作,是为了刷新内存中的表定义。

下面看看t1的第二种情况,旧的t1.frm+旧的t1.ibd

mysql> ALTER TABLE t1 DISCARD TABLESPACE;
Query OK, 0 rows affected (0.00 sec)
[root@localhost test]# cp /backup/t1.frm .
cp: overwrite `./t1.frm'? y
[root@localhost test]# cp /backup/t1.ibd .
[root@localhost test]# chown mysql.mysql t1.frm
[root@localhost test]# chown mysql.mysql t1.ibd
mysql> ALTER TABLE t1 import TABLESPACE;
Query OK, 0 rows affected, 1 warning (0.04 sec) mysql> select * from t1;
+------+---------------------+
| id_1 | hiredate_1 |
+------+---------------------+
| 1 | 2016-08-18 17:45:02 |
| 2 | 2016-08-18 17:45:02 |
+------+---------------------+
2 rows in set (0.00 sec) mysql> flush table t1;
Query OK, 0 rows affected (0.00 sec) mysql> select * from t1;
+------+---------------------+
| id | hiredate |
+------+---------------------+
| 1 | 2016-08-18 17:45:02 |
| 2 | 2016-08-18 17:45:02 |
+------+---------------------+
2 rows in set (0.00 sec)

第一次查询的时候还是新的列名,对表进行flush后,就恢复到原来的列名了。

下面来看看t2表的导入情况

因为t2表的表结构发生了改变,在这里,也是测试如下两种情况

1. 新的t2.frm+旧的t2.ibd

2. 旧的t2.frm+旧的t2.ibd

首先,只是导入t2表的数据文件

[root@localhost test]# cp /backup/t2.ibd .
[root@localhost test]# ll
total
-rw-rw---- mysql mysql Aug : t1.frm
-rw-r----- mysql mysql Aug : t1.ibd
-rw-rw---- mysql mysql Aug : t2.frm
-rw-r----- root root Aug : t2.ibd
[root@localhost test]# chown mysql.mysql t2.ibd

导入t2表的表空间进行测试

mysql> ALTER TABLE t2 import TABLESPACE;
Query OK, 0 rows affected, 1 warning (0.03 sec) mysql> select * from t2;
+------+
| id_1 |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec) mysql> flush table t2;
Query OK, 0 rows affected (0.00 sec) mysql> select * from t2;
+------+
| id_1 |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)

从结果可以看出,只能读出第一列。

下面测试第二种情况,旧的t2.frm和t2.ibd

mysql> ALTER TABLE t2 DISCARD TABLESPACE;
Query OK, 0 rows affected (0.06 sec)
[root@localhost test]# rm -rf t2.frm
[root@localhost test]# cp /backup/t2.frm .
[root@localhost test]# cp /backup/t2.ibd .
[root@localhost test]# chown mysql.mysql t2.frm
[root@localhost test]# chown mysql.mysql t2.ibd
mysql> ALTER TABLE t2 import TABLESPACE;
Query OK, 0 rows affected, 1 warning (0.09 sec) mysql> select * from t2;
+------+
| id_1 |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec) mysql> flush table t2;
Query OK, 0 rows affected (0.00 sec) mysql> select * from t2;
ERROR 1146 (42S02): Table 'test.t2' doesn't exist

在重新刷新后,就出现错误了,个人感觉,这个和系统表空间中的数据字典信息有关。

实际上,后续还测试了一下,如果将hiredate的列定义为varchar,则无论是使用之前的frm文件还是之后的,在导入表空间,进行查询时,数据库直接挂掉。

mysql> create table t1(id int,hiredate varchar(10));
Query OK, 0 rows affected (0.05 sec) mysql> ALTER TABLE t1 DISCARD TABLESPACE;
Query OK, 0 rows affected (0.00 sec) mysql> ALTER TABLE t1 import TABLESPACE;
Query OK, 0 rows affected, 1 warning (0.03 sec) mysql> select * from t1;
ERROR 2013 (HY000): Lost connection to MySQL server during query

结论

经过上面的一系列测试,可以看到

1. 使用可传输表空间,可以解决在删除ibdata1和ib_logfile的情况下恢复MySQL数据库,当然,本文测试的前提是数据库正常关闭下删除的ibdata1和ib_logfile。

2. 使用可传输表空间,建议新建表的表结构和原来的表结构完全一致,同时,在导入表空间前,只需COPY回原来的数据文件,即ibd。

事实上,在数据库正常关闭下删除ibdata1,会导致mysql库中的以下几张表无法访问

mysql> select table_name from information_schema.tables  where table_schema='mysql' and engine='innodb';
+----------------------+
| table_name |
+----------------------+
| innodb_index_stats |
| innodb_table_stats |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
+----------------------+
5 rows in set (0.00 sec) mysql> select * from mysql.innodb_index_stats;
ERROR 1146 (42S02): Table 'mysql.innodb_index_stats' doesn't exist
mysql> select * from mysql.innodb_table_stats;
ERROR 1146 (42S02): Table 'mysql.innodb_table_stats' doesn't exist
mysql> select * from mysql.slave_master_info;
ERROR 1146 (42S02): Table 'mysql.slave_master_info' doesn't exist
mysql> select * from mysql.slave_relay_log_info;
ERROR 1146 (42S02): Table 'mysql.slave_relay_log_info' doesn't exist
mysql> select * from mysql.slave_worker_info;
ERROR 1146 (42S02): Table 'mysql.slave_worker_info' doesn't exist

同时,错误日志中报如下信息

-- ::  [Warning] InnoDB: Cannot open table mysql/innodb_index_stats from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for how you can resolve the problem.
-- :: [Warning] InnoDB: Cannot open table mysql/innodb_table_stats from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for how you can resolve the problem.
-- :: [Warning] InnoDB: Cannot open table mysql/slave_master_info from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for how you can resolve the problem.
-- :: [Warning] InnoDB: Cannot open table mysql/slave_relay_log_info from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for how you can resolve the problem.
-- :: [Warning] InnoDB: Cannot open table mysql/slave_worker_info from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for how you can resolve the problem.

要解决这个问题,只能重建这些表。

参考

1. http://dev.mysql.com/doc/refman/5.6/en/tablespace-copying.html

2. http://dba.stackexchange.com/questions/48166/cannot-open-table-mysql-innodb-index-stats

如何在删除ibdata1和ib_logfile的情况下恢复MySQL数据库的更多相关文章

  1. SQL Server 在缺少文件组的情况下如何还原数据库

    SQL Server 在缺少文件组的情况下如何还原数据库 一.背景 我有一个A库,由于a,b两张表的数据量比较大,所以对表进行分区:在把A库迁移到一个新的集群上去,我只备份了A库的主分区过去进行还原为 ...

  2. 恢复MySQL数据库删除的数据

    在日常运维工作中,对于数据库的备份是至关重要的!数据库对于网站的重要性使得我们对 MySQL 数据库的管理不容有失!然而是人总难免会犯错误,说不定哪天大脑短路了,误操作把数据库给删除了,怎么办? 下面 ...

  3. Oracle备份恢复之无备份情况下恢复undo表空间

    UNDO表空间存储着DML操作数据块的前镜像数据,在数据回滚,一致性读,闪回操作,实例恢复的时候都可能用到UNDO表空间中的数据.如果在生产过程中丢失或破坏了UNDO表空间,可能导致某些事务无法回滚, ...

  4. Entity framework 意外删除了表,如何在不影响其它表的情况下恢复回来

    关于EntityFramework数据迁移原理 查询数据库的表"__MigrationHistory",遍历代码库的Migrations文件夹下的所有文件,如果文件不在__Migr ...

  5. 无shell情况下的mysql远程mof提权利用方法详解

    扫到一个站的注入<ignore_js_op> 在havij中得到mysql数据库中mysql库保存的数据库密码:<ignore_js_op> 有时候发现1.15版的还是最好用, ...

  6. 怎样使用PL&sol;SQL在不安装oracle 客户端的情况下使用oracle数据库

    在网上查了好多这方面的例子,但是似乎说的都不准确,在咨询朋友后终于实现了本机不安装oracle 的情况下,在windows系统上实现连接服务器上的数据库,现在贴出来与大家共享. 首先,我们需要一个PL ...

  7. 2019年春季学期第二周作业 基础作业 请在第一周作业的基础上,继续完成:找出给定的文件中数组的最大值及其对应的最小下标(下标从0开始)。并将最大值和对应的最小下标数值写入文件。 输入: 请建立以自己英文名字命名的txt文件,并输入数组元素数值,元素值之间用逗号分隔。 输出 在不删除原有文件内容的情况下,将最大值和对应的最小下标数值写入文件

    ~~~ include<stdio.h> include<stdlib.h> int main() { FILE*fp; int i=0,max=0,j=0,maxb=0; i ...

  8. SSD磁盘,CPU居高不下&comma;高并发的情况下&comma;是不是mysql解析器耗费的cpu资源高&quest;

    你看看我做的实验,这个user表是300多W纪录,普通磁盘下,消耗时间最多的是Copy to tmp table 0.81秒,当然在ssd下,这个可以减少很多很多的,第二高就是sending data ...

  9. 不root手机的情况下查看Android数据库

    最近写Android的时候发现想要读取数据库很不方便,使用adb工具的方法需要root手机,而华为手机root十分麻烦,需要解除密码,一些品牌手机即使root了也没有sqlite3命令,总之十分麻烦. ...

随机推荐

  1. 易懂 易上手的cookie 最简单明了 js中cookie的使用方法及教程

    今天项目中需要用到cookie 看到我的cookie不行了 大喊一声我曹 怎么可以这样 我就疯狂的在网上找 找啊 找 但是我感觉都太官方了   废话不说 看栗子 1.引入jQuery与jQuery.C ...

  2. linux系统CentOS7

    linux系统CentOS7 到http://mirrors.sohu.com/mysql/下载想要的mysql版本 这里用到的是 mysql-5.6.33-linux-glibc2.5-x86_64 ...

  3. iOS app上架需要提前准备的东西

    APP icon,要求1024*1024并且不能有圆角效果. 不同屏幕的截图 3.5的,4的,4.7的,5.5的 测试账号,即登录的账号密码(不能删除或更改的) 联系人电话,电子邮件 对项目的描述 关 ...

  4. &period;Net判断一个对象是否为数值类型探讨总结(高营养含量,含最终代码及跑分)

    前一篇发出来后引发了积极的探讨,起到了抛砖引玉效果,感谢大家参与. 吐槽一下:这个问题比其看起来要难得多得多啊. 大家的讨论最终还是没有一个完全正确的答案,不过我根据讨论结果总结了一个差不多算是最终版 ...

  5. Go推出的主要目的之一就是G内部大东西太多了,系统级开发巨型项目非常痛苦,Go定位取代C&plus;&plus;,Go以简单取胜(KISS)

    以前为了做compiler,研读+实现了几乎所有种类的语言.现在看语法手册几乎很快就可以理解整个语言的内容.后来我对比了一下go和rust,发现go的类型系统简直就是拼凑的.这会导致跟C语言一样,需要 ...

  6. Linux入门基础 &num;6:Linux用户基础

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  7. Kotlin 一个好用的新功能:Parcelize

    在开发中,如果有需要用到序列化和反序列化的操作,就会用到 Serializable 或者 Parcelable,它们各有优缺点,会适用于不同的场景. Serializable 的优点是实现简单,你只需 ...

  8. Python十题(第2课)

    一.天天向上的力量 C 一年365天,以第1天的能力值为基数,记为1.0.当好好学习时,能力值相比前一天提高N‰:当没有学习时,由于遗忘等原因能力值相比前一天下降N‰.每天努力或放任,一年下来的能力值 ...

  9. 本地和svn都删除文件导致版本不同的问题

    想要删除一个项目中的文件,同是要删除svn上的文件. 自己操作 1.直接右键删除了本地项目中的一个目录的模块 2.右键删除了库中svn中的这个目录 3.同步本地和svn上的代码 4.问题出现了,本地和 ...

  10. shell文件描述符和重定向

    1.文件描述符是与一个打开的文件或数据流相关联的整数.文件描述符0,1,2是系统预留的. 0 --------stdin(标准输入) 1 --------stdout(标准输出) 2--------- ...