MySQL 如何从包含多个数据库的 mysqldump 还原一个数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3596444/
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 17:00:06  来源:igfitidea点击:

How do I restore one database from a mysqldump containing multiple databases?

mysqldatabaseimportmysqldumprestore

提问by The Pixel Developer

I have a mysql dump with 5 databases and would like to know if there is a way to import just one of those (using mysqldump or other).

我有一个包含 5 个数据库的 mysql 转储,想知道是否有办法只导入其中一个(使用 mysqldump 或其他)。

Suggestions appreciated.

建议表示赞赏。

回答by Tim

You can use the mysql command line --one-database option.

您可以使用 mysql 命令行 --one-database 选项。

mysql> mysql -u root -p --one-database YOURDBNAME < YOURFILE.SQL

Of course be careful when you do this.

当然,这样做时要小心。

You can also use a mysql dumpsplitter.

您还可以使用mysql dumpsplitter

回答by rjk

You can pipe the dumped SQL through sedand have it extract the database for you. Something like:

您可以通过管道传输转储的 SQLsed并让它为您提取数据库。就像是:

cat mysqldumped.sql | \
sed -n -e '/^CREATE DATABASE.*`the_database_you_want`/,/^CREATE DATABASE/ p' | \
sed -e '$d' | \
mysql

The two sedcommands:

两个sed命令:

  1. Only print the lines matching between the CREATE DATABASElines (including both CREATE DATABASElines), and
  2. Delete the lastCREATE DATABASEline from the output since we don't want mysqld to create a second database.
  1. 只打印行之间匹配的CREATE DATABASE行(包括两CREATE DATABASE行),并且
  2. 从输出中删除最后CREATE DATABASE一行,因为我们不希望 mysqld 创建第二个数据库。

If your dump does not contain the CREATE DATABASElines, you can also match against the USElines.

如果您的转储不包含这些CREATE DATABASE行,您也可以匹配这些USE行。