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
How do I restore one database from a mysqldump containing multiple databases?
提问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 sed
and 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 sed
commands:
两个sed
命令:
- Only print the lines matching between the
CREATE DATABASE
lines (including bothCREATE DATABASE
lines), and - Delete the last
CREATE DATABASE
line from the output since we don't want mysqld to create a second database.
- 只打印行之间匹配的
CREATE DATABASE
行(包括两CREATE DATABASE
行),并且 - 从输出中删除最后
CREATE DATABASE
一行,因为我们不希望 mysqld 创建第二个数据库。
If your dump does not contain the CREATE DATABASE
lines, you can also match against the USE
lines.
如果您的转储不包含这些CREATE DATABASE
行,您也可以匹配这些USE
行。