MySQL mysql恢复到不同的数据库

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

mysql restore to different database

mysqlmysqldump

提问by giorgio

I back up my production database with the following command:

我使用以下命令备份我的生产数据库:

mysqldump -u root --opt --skip-extended-insert --databases my_production_db

The resulting dump file has the following lines near the top:

生成的转储文件在顶部附近有以下几行:

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `my_production_db` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `my_production_db `;

In order to restore the database to a different destination ie. my_debvelopment_dbI have to open the dump file and edit the bits where the database is named.

为了将数据库恢复到不同的目的地,即。my_debvelopment_db我必须打开转储文件并编辑命名数据库的位。

Then I run:

然后我运行:

mysql -u root  -p <password> < mydumpfile

I have not figured out another way to do it.

我还没有想出另一种方法来做到这一点。

As the database gets bigger this becomes impractical.

随着数据库变大,这变得不切实际。

Am I missing something? Cant I somehow specify where I want to restore the database? Would I need a different backup command?

我错过了什么吗?我不能以某种方式指定要恢复数据库的位置吗?我需要不同的备份命令吗?

采纳答案by minaz

If you drop the option --databasesbut still specify the database name, you will NOT get the create database statements. ie:

如果您删除该选项--databases但仍指定数据库名称,您将不会获得 create database 语句。IE:

mysqldump -u root --opt --skip-extended-insert  my_production_db

On your dev machine simply create any database you wish to restore to.

在您的开发机器上,只需创建您想要恢复到的任何数据库。

回答by lqez

@minaz answer was good, but I want to append a little bit more.

@minaz 的回答很好,但我想再补充一点。

The problem was caused by --databaseskeyword. If you omit the keyword, it will not contain any database creation contents.

问题是由--databases关键字引起的。如果省略关键字,它将不包含任何数据库创建内容。

So, Dump without --databaseskeyword.

所以,无--databases关键字转储。

mysqldump -u username -p database_name > dump.sql

And restore it with the target database name.

并使用目标数据库名称恢复它。

mysql -u username -p target_database_name < dump.sql

Also, there are several ways to do this. See the similar problem on here(dba.stackexchange).

此外,有几种方法可以做到这一点。请参阅此处的类似问题(dba.stackexchange)。

回答by Mukesh

On windows xampp I used following commands to achieve this

在 windows xampp 上,我使用以下命令来实现这一点

Export

出口

mysqldump -u root -p mydb > mydb.sql

Import

进口

mysql -u root -p mynewdb < mydb.sql

回答by netAction

If you already have your dump you can strip the commands for creating and using the database. Simply remove the fourth and the new fifth line.

如果您已经有了转储,则可以删除用于创建和使用数据库的命令。只需删除第四行和新的第五行。

sed '4d' dump.sql | sed '5d' > dump-striped.sql