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


当前回答

表应该在转储和数据库中呈现相同的结构。

`zgrep -a ^"INSERT INTO \`table_name" DbDump-backup.sql.tar.gz | mysql -u<user> -p<password> database_name`

or

`zgrep -a ^"INSERT INTO \`table_name" DbDump-backup.sql | mysql -u<user> -p<password> database_name`

其他回答

我使用了uloBasEI的sed命令的修改版本。它包括前面的DROP命令,读取直到mysql完成转储数据到你的表(解锁)。为我工作(重新)导入wp_users到一堆Wordpress网站。

sed -n -e '/DROP TABLE.*`mytable`/,/UNLOCK TABLES/p' mydump.sql > tabledump.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.

一个简单的解决方案是仅为希望单独恢复的表创建一个转储。你可以使用mysqldump命令来这样做,语法如下:

mysqldump -u [user] -p[password] [database] [table] > [output_file_name].sql

然后像往常一样导入它,它只会导入转储的表。

表应该在转储和数据库中呈现相同的结构。

`zgrep -a ^"INSERT INTO \`table_name" DbDump-backup.sql.tar.gz | mysql -u<user> -p<password> database_name`

or

`zgrep -a ^"INSERT INTO \`table_name" DbDump-backup.sql | mysql -u<user> -p<password> database_name`

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

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

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

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

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