MySQL 从 --all-databases 转储导入单个数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2342356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:23:43  来源:igfitidea点击:

Import single database from --all-databases dump

mysqldatabaseimportmysqldump

提问by savageguy

Is it possible to import a single database from an --all-databases mysqldump? I guess I can modify the file manually but wondering if there are any command line options to do this.

是否可以从 --all-databases mysqldump 导入单个数据库?我想我可以手动修改文件,但想知道是否有任何命令行选项可以做到这一点。

I am moving servers and have a lot of databases, most of which I don't currently need or want at the moment but would like to have the option to restore a single one if need be.

我正在移动服务器并且有很多数据库,其中大部分我目前不需要或不想要,但如果需要,我希望可以选择恢复单个数据库。

回答by Hetzbh

You can use the following command:

您可以使用以下命令:

mysql -u root -p --one-database destdbname < alldatabases.sql

Where destdbnameis your desired database which you want to restore.

destdbname您想要恢复的所需数据库在哪里。

Another option which is IMHO much safer, is to extract the DB from an --all-databasesdump. Example:

恕我直言,另一个更安全的选择是从--all-databases转储中提取数据库。例子:

sed -n '/^-- Current Database: `dbname`/,/^-- Current Database: `/p' alldatabases.sql > output.sql

Replace dbnamewith the desired database name. alldatabases.sqlis the name of your sql-dump file. That way you'll have the seperated DB on file, and then you can restore using a simple mysql command.

替换dbname为所需的数据库名称。alldatabases.sql是您的 sql-dump 文件的名称。这样,您将拥有单独的数据库文件,然后您可以使用简单的 mysql 命令进行恢复。

Good luck

祝你好运

(Credits goes to: Darren Mothersele - see his page)

(致谢:Darren Mothersele - 见他的页面

回答by Quassnoi

mysqldumpoutput is just a set of SQLstatements.

mysqldump输出只是一组SQL语句。

You can provide the desired database in the command line and skip the commands against the other databases using:

您可以在命令行中提供所需的数据库,并使用以下命令跳过针对其他数据库的命令:

mysql -D mydatabase -o mydatabase < dump.sql

This will only execute the commands when mydatabaseis in use

这只会mydatabase在使用时执行命令