我有一个mysqldump备份我的mysql数据库,包括我们所有的表,这是大约440兆。我只想从mysqldump中恢复其中一个表的内容。这可能吗?从理论上讲,我可以只删除重建我想要的表的部分,但我甚至不知道如何有效地编辑这种大小的文本文档。


当前回答

前面提到的“sed”解决方案很好,但不是100%安全

你可以使用INSERT命令,其中的数据包含: ... 创建表(无论)……mytable… 或者甚至是精确的字符串"CREATE TABLE ' mytable ';" 如果您正在存储DML命令,例如!

(如果表很大,你不需要手动检查)

我会验证所使用的转储版本的确切语法,并有一个更严格的模式搜索:

避免”。*”并使用“^”来确保我们从行开头开始。 我更喜欢抓住最初的DROP

总而言之,这对我来说更好:

sed -n -e '/^DROP TABLE IF EXISTS \`mytable\`;/,/^UNLOCK TABLES;/p' mysql.dump > mytable.dump

其他回答

你应该尝试@bryn命令,但使用'分隔符,否则你也会提取有前缀或后缀的表,这是我通常做的:

sed -n -e '/DROP TABLE.*`mytable`/,/UNLOCK TABLES/p' dump.sql > mytable.sql

同样出于测试的目的,你可能想要在导入之前更改表名:

sed -n -e 's/`mytable`/`mytable_restored`/g' mytable.sql > mytable_restored.sql

要导入,可以使用mysql命令:

mysql -u root -p'password' mydatabase < mytable_restore.sql

Get a decent text editor like Notepad++ or Vim (if you're already proficient with it). Search for the table name and you should be able to highlight just the CREATE, ALTER, and INSERT commands for that table. It may be easier to navigate with your keyboard rather than a mouse. And I would make sure you're on a machine with plenty or RAM so that it will not have a problem loading the entire file at once. Once you've highlighted and copied the rows you need, it would be a good idea to back up just the copied part into it's own backup file and then import it into MySQL.

大多数现代文本编辑器应该能够处理这样大小的文本文件,如果您的系统能够胜任的话。

不管怎样,有一次我不得不很快地做这件事,而我没有时间去找任何工具。我设置了一个新的MySQL实例,导入了整个备份,然后输出了我想要的表。

然后我将该表导入主数据库。

这很乏味,但相当容易。祝你好运。

您可以尝试使用sed,以便只提取所需的表。

假设你的表名为mytable,文件名为mysql。Dump是包含你的巨大转储的文件:

$ sed -n -e '/CREATE TABLE.*`mytable`/,/Table structure for table/p' mysql.dump > mytable.dump

这将复制到文件mytable中。dump位于CREATE TABLE mytable和对应下一个表的下一个CREATE TABLE之间的内容。

然后可以调整文件mytable。其中包含表mytable的结构和数据(一个INSERT列表)。

我欣赏这里的一些独创性,但实际上根本没有理由使用sed来解决OP的问题。

注释“use——one-database”是正确答案,内置在MySQL/MariaDB中。不需要第三方黑客。

Mysql -u root -p databasename——one-database < localhost。SQL将只导入所需的数据库。

我还发现,在某些情况下,当使用它来导入一系列数据库时,它会在列表中为我创建下一个数据库(但不会在其中放入任何东西)。不知道为什么它这样做,但它使恢复更容易。

使用此命令,交互式地输入密码,它将导入所请求的数据库。