我有一个mysqldump备份我的mysql数据库,包括我们所有的表,这是大约440兆。我只想从mysqldump中恢复其中一个表的内容。这可能吗?从理论上讲,我可以只删除重建我想要的表的部分,但我甚至不知道如何有效地编辑这种大小的文本文档。
当前回答
我使用了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.
大多数现代文本编辑器应该能够处理这样大小的文本文件,如果您的系统能够胜任的话。
不管怎样,有一次我不得不很快地做这件事,而我没有时间去找任何工具。我设置了一个新的MySQL实例,导入了整个备份,然后输出了我想要的表。
然后我将该表导入主数据库。
这很乏味,但相当容易。祝你好运。
您可以使用如下所示的终端线导入单个表。 这里导入单个用户表到特定的数据库。
mysql -u root -p -D my_database_name < var/www/html/myproject/tbl_user.sql
这也可能有所帮助。
# mysqldump -u root -p database0 > /tmp/database0.sql
# mysql -u root -p -e 'create database database0_bkp'
# mysql -u root -p database0_bkp < /tmp/database0.sql
# mysql -u root -p database0 -e 'insert into database0.table_you_want select * from database0_bkp.table_you_want'
你应该尝试@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