MySQL利用ext3grep恢复Myisam表
MySQL没有类型Oracle的闪回机制,当你执行了drop table xxx 之后,这个表就永久删除了,你只能从备份里进行恢复,如果你没有备份
MySQL没有类型Oracle的闪回机制,当你执行了drop table xxx 之后,这个表就永久删除了,你只能从备份里进行恢复,如果你没有备份,那你就只能哭了。本文提供一种思路,让类似情况能有挽回的机会。
我们知道,Mysql的MYISAM引擎表在当前Database目录下,有3个对应的文件,,frm结构文件,MYI索引文件,MYD数据文件。当你在Mysql中,drop了MYISAM引擎表后,其实就是在文件系统里将其对应的3个文件rm了。所以当你执行drop后,如果能将上面3个文件恢复,那么表也将恢复。
如果你使用的是EXT3文件系统,那么可以使用ext3grep来完成上面的操作,举例说明:
1.安装ext3grep
首先需要安装e2fsprogs-libs,可以到下载源码包
tar -xzvf e2fsprogs-1.41.5.tar.gz
cd e2fsprogs-1.41.5
mkdir build; cd build
../configure
make
make install
make install-libs(e2fsprogs-libs)
在安装ext3grep,可以到下载
tar xfvz ext3grep-0.10.1.tar.gz
cd ext3grep-0.10.1
./configure
make
make install
ext3grep -V
Running ext3grep version 0.10.1
ext3grep v0.10.1, Copyright (C) 2008 Carlo Wood.
ext3grep comes with ABSOLUTELY NO WARRANTY;
This program is free software; your freedom to use, change
and distribute this program is protected by the GPL.
2.删除测试表
mysql> use test
mysql> desc t;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)
mysql> select * from t;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
mysql>drop table t;
mysql> quit
3.停止mysql,并将数据文件所在分区umount。
# /etc/init.d/mysqld stop
Shutting down MySQL.. [ OK ]
cd
# umount /dev/sda6
4.利用ext3grep工具进行恢复
# ext3grep /dev/sda6 --ls --inode 2
Running ext3grep version 0.10.1
WARNING: I don't know what EXT3_FEATURE_COMPAT_EXT_ATTR is.
Number of groups: 1778
Loading group metadata... done
Minimum / maximum journal block: 29065730 / 29099045
Loading journal descriptors... sorting... done
The oldest inode block that is still in the journal, appears to be from 1243924942 = Tue Jun 2 14:42:22 2009
Number of descriptors in journal: 418; min / max sequence numbers: 13 / 36
Inode is Allocated
Loading sda6.ext3grep.stage2... done
The first block of the directory is 1539.
Inode 2 is directory "".
Directory block 1539:
.-- File type in dir_entry (r=regular file, d=directory, l=symlink)
| .-- D: Deleted ; R: Reallocated
Indx Next | Inode | Deletion time Mode File name
==========+==========+----------------data-from-inode------+-----------+=========
0 1 d 2 drwxr-xr-x .
1 2 d 2 drwxr-xr-x ..
2 3 d 11 drwx------ lost+found
3 4 r 12 rrw-r--r-- 1
4 5 r 13 rrw-r--r-- 5
5 end d 237569 drwxr-xr-x data
6 end r 15 D 1243919862 Tue Jun 2 13:17:42 2009 rrw-r--r-- 3.txt
可以看到Mysql数据文件的目录data对应的Inode是237569,然后在查看改Inode对应的block。
# ext3grep /dev/sda6 --inode 237569
Running ext3grep version 0.10.1
No --ls used; implying --print.
Inode is Allocated
Group: 29
Generation Id: 3010341297
uid / gid: 500 / 500
mode: drwxr-xr-x
size: 4096
num of links: 4
sectors: 8 (--> 0 indirect blocks).
Inode Times:
Accessed: 1243930169 = Tue Jun 2 16:09:29 2009
File Modified: 1243930170 = Tue Jun 2 16:09:30 2009
Inode Modified: 1243930170 = Tue Jun 2 16:09:30 2009
Deletion time: 0
Direct Blocks: 968704
Loading sda6.ext3grep.stage2... done
There is no directory block associated with inode 237569.
看到Direct Blocks: 968704,在查看这个block。
# ext3grep /dev/sda6 --ls --block 968704
Running ext3grep version 0.10.1
WARNING: I don't know what EXT3_FEATURE_COMPAT_EXT_ATTR is.
Number of groups: 1778
Minimum / maximum journal block: 29065730 / 29099045
Loading journal descriptors... sorting... done
The oldest inode block that is still in the journal, appears to be from 1243925348 = Tue Jun 2 14:49:08 2009
Number of descriptors in journal: 236; min / max sequence numbers: 15 / 45
Group: 29
Block 968704 is a directory. The block is Allocated
.-- File type in dir_entry (r=regular file, d=directory, l=symlink)
| .-- D: Deleted ; R: Reallocated
Indx Next | Inode | Deletion time Mode File name
==========+==========+----------------data-from-inode------+-----------+=========
0 1 d 237569 drwxr-xr-x .
1 3 d 2 drwxr-xr-x ..
2 3 r 237570 D 1243930170 Tue Jun 2 16:09:30 2009 rrw-r----- mysql.err
3 4 d 237571 drwx------ mysql
4 5 r 237641 rrw-r----- ibdata1
5 7 d 237642 drwx------ test
6 7 r 237646 D 1243926673 Tue Jun 2 15:11:13 2009 rrw-rw---- mysql.pid
7 8 r 237647 rrw-r----- ib_logfile0
8 end r 237648 rrw-r----- ib_logfile1
在data目录下面,其文件所对应的Inode,我们是删除的test下的表,所以在查看test目录所对应的Inode.
# ext3grep /dev/sda6 --inode 237642
Running ext3grep version 0.10.1
No --ls used; implying --print.
Inode is Allocated
Group: 29
Generation Id: 3010341370
uid / gid: 500 / 500
mode: drwx------
size: 4096
num of links: 2
sectors: 8 (--> 0 indirect blocks).
Inode Times:
Accessed: 1243928606 = Tue Jun 2 15:43:26 2009
File Modified: 1243928606 = Tue Jun 2 15:43:26 2009
Inode Modified: 1243928606 = Tue Jun 2 15:43:26 2009
Deletion time: 0
Direct Blocks: 971511
Loading sda6.ext3grep.stage2... done
There is no directory block associated with inode 237642.
# ext3grep /dev/sda6 --ls --block 971511
Running ext3grep version 0.10.1
WARNING: I don't know what EXT3_FEATURE_COMPAT_EXT_ATTR is.
Number of groups: 1778
Minimum / maximum journal block: 29065730 / 29099045
Loading journal descriptors... sorting... done
The oldest inode block that is still in the journal, appears to be from 1243925348 = Tue Jun 2 14:49:08 2009
Number of descriptors in journal: 236; min / max sequence numbers: 15 / 45
Group: 29
Block 971511 is a directory. The block is Allocated
.-- File type in dir_entry (r=regular file, d=directory, l=symlink)
| .-- D: Deleted ; R: Reallocated
Indx Next | Inode | Deletion time Mode File name
==========+==========+----------------data-from-inode------+-----------+=========
0 1 d 237642 drwx------ .
1 end d 237569 drwxr-xr-x ..
2 3 r 237643 D 1243928606 Tue Jun 2 15:43:26 2009 rrw-r----- u.frm
3 4 r 237649 D 1243928606 Tue Jun 2 15:43:26 2009 rrw-r----- u.MYD
4 end r 237645 D 1243928606 Tue Jun 2 15:43:26 2009 rrw-r----- u.MYI
5 end r 237644 D 1243926666 Tue Jun 2 15:11:06 2009 rrw-rw---- t.frm
6 7 r 237650 D 1243926666 Tue Jun 2 15:11:06 2009 rrw-rw---- t.MYI
7 end r 237651 D 1243926666 Tue Jun 2 15:11:06 2009 rrw-rw---- t.MYD
ok,到这里我们需要恢复的表t的3个文件已经出来了,然后在执行恢复。
# ext3grep /dev/sda6 --restore-inode 237650
Running ext3grep version 0.10.1
Restoring inode.237650
# ext3grep /dev/sda6 --restore-inode 237651
Running ext3grep version 0.10.1
Restoring inode.237651
# ext3grep /dev/sda6 --restore-inode 237644
Running ext3grep version 0.10.1
Restoring inode.237644
cd RESTORED_FILES/
mv inode.237644 t.frm
mv inode.237650 t.MYI
mv inode.237651 t.MYD
ll
total 20
-rw-r----- 1 root root 8556 Jun 2 16:26 t.frm
-rw-r----- 1 root root 21 Jun 2 16:26 t.MYD
-rw-r----- 1 root root 1024 Jun 2 16:26 t.MYI
将数据文件所在分区mount,并将恢复的表t的文件copy到原目录。设置好权限,启动mysql。
mount /dev/sda6 /u01
cp t.* /u01/data/test
chown -R mysql.mysql test/
/etc/init.d/mysqld start
Starting MySQL. [ OK ]
mysql> use test
Database changed
mysql> select * from t;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.01 sec)
OK,表已经被恢复。

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

MySQL 数据库中,用户和数据库的关系通过权限和表定义。用户拥有用户名和密码,用于访问数据库。权限通过 GRANT 命令授予,而表由 CREATE TABLE 命令创建。要建立用户和数据库之间的关系,需创建数据库、创建用户,然后授予权限。

数据集成简化:AmazonRDSMySQL与Redshift的零ETL集成高效的数据集成是数据驱动型组织的核心。传统的ETL(提取、转换、加载)流程复杂且耗时,尤其是在将数据库(例如AmazonRDSMySQL)与数据仓库(例如Redshift)集成时。然而,AWS提供的零ETL集成方案彻底改变了这一现状,为从RDSMySQL到Redshift的数据迁移提供了简化、近乎实时的解决方案。本文将深入探讨RDSMySQL零ETL与Redshift集成,阐述其工作原理以及为数据工程师和开发者带来的优势。

MySQL适合初学者使用,因为它安装简单、功能强大且易于管理数据。1.安装和配置简单,适用于多种操作系统。2.支持基本操作如创建数据库和表、插入、查询、更新和删除数据。3.提供高级功能如JOIN操作和子查询。4.可以通过索引、查询优化和分表分区来提升性能。5.支持备份、恢复和安全措施,确保数据的安全和一致性。

要填写 MySQL 用户名和密码,请:1. 确定用户名和密码;2. 连接到数据库;3. 使用用户名和密码执行查询和命令。

1.使用正确的索引索引通过减少扫描的数据量来加速数据检索select*fromemployeeswherelast_name='smith';如果多次查询表的某一列,则为该列创建索引如果您或您的应用根据条件需要来自多个列的数据,则创建复合索引2.避免选择*仅选择那些需要的列,如果您选择所有不需要的列,这只会消耗更多的服务器内存并导致服务器在高负载或频率时间下变慢例如,您的表包含诸如created_at和updated_at以及时间戳之类的列,然后避免选择*,因为它们在正常情况下不需要低效查询se

Navicat本身不存储数据库密码,只能找回加密后的密码。解决办法:1. 检查密码管理器;2. 检查Navicat的“记住密码”功能;3. 重置数据库密码;4. 联系数据库管理员。

数据库ACID属性详解ACID属性是确保数据库事务可靠性和一致性的一组规则。它们规定了数据库系统处理事务的方式,即使在系统崩溃、电源中断或多用户并发访问的情况下,也能保证数据的完整性和准确性。ACID属性概述原子性(Atomicity):事务被视为一个不可分割的单元。任何部分失败,整个事务回滚,数据库不保留任何更改。例如,银行转账,如果从一个账户扣款但未向另一个账户加款,则整个操作撤销。begintransaction;updateaccountssetbalance=balance-100wh

通过以下命令查看 MySQL 数据库:连接到服务器:mysql -u 用户名 -p 密码运行 SHOW DATABASES; 命令获取所有现有数据库选择数据库:USE 数据库名;查看表:SHOW TABLES;查看表结构:DESCRIBE 表名;查看数据:SELECT * FROM 表名;
