简体   繁体   English

如何从 mysqldump 恢复转储文件?

[英]How do I restore a dump file from mysqldump?

I was given a MySQL database file that I need to restore as a database on my Windows Server 2008 machine.我得到了一个 MySQL 数据库文件,我需要将其恢复为我的 Windows Server 2008 计算机上的数据库。

I tried using MySQL Administrator, but I got the following error:我尝试使用 MySQL Administrator,但出现以下错误:

The selected file was generated by mysqldump and cannot be restored by this application.所选文件由 mysqldump 生成,无法由该应用程序恢复。

How do I get this working?我如何让这个工作?

If the database you want to restore doesn't already exist, you need to create it first.如果要还原的数据库不存在,则需要先创建它。

On the command-line, if you're in the same directory that contains the dumped file, use these commands (with appropriate substitutions):在命令行上,如果您位于包含转储文件的同一目录中,请使用以下命令(使用适当的替换):

C:\> mysql -u root -p

mysql> create database mydb;
mysql> use mydb;
mysql> source db_backup.dump;

It should be as simple as running this:它应该像运行这个一样简单:

mysql -u <user> -p < db_backup.dump

If the dump is of a single database you may have to add a line at the top of the file:如果转储是单个数据库,您可能需要在文件顶部添加一行:

USE <database-name-here>;

If it was a dump of many databases, the use statements are already in there.如果它是许多数据库的转储,则 use 语句已经在那里了。

To run these commands, open up a command prompt (in Windows) and cd to the directory where the mysql.exe executable is (you may have to look around a bit for it, it'll depend on how you installed mysql, ie standalone or as part of a package like WAMP).要运行这些命令,请打开命令提示符(在 Windows 中)并cdmysql.exe可执行文件所在的目录(您可能需要四处看看,这取决于您安装 mysql 的方式,即独立的或作为 WAMP 等软件包的一部分)。 Once you're in that directory, you should be able to just type the command as I have it above.进入该目录后,您应该可以像上面一样输入命令。

You simply need to run this:你只需要运行这个:

mysql -p -u[user] [database] < db_backup.dump

If the dump contains multiple databases you should omit the database name:如果转储包含多个数据库,则应省略数据库名称:

mysql -p -u[user] < db_backup.dump

To run these commands, open up a command prompt (in Windows) and cd to the directory where the mysql.exe executable is (you may have to look around a bit for it, it'll depend on how you installed mysql, ie standalone or as part of a package like WAMP).要运行这些命令,请打开命令提示符(在 Windows 中)并cdmysql.exe可执行文件所在的目录(您可能需要四处看看,这取决于您安装 mysql 的方式,即独立的或作为 WAMP 等软件包的一部分)。 Once you're in that directory, you should be able to just type the command.进入该目录后,您应该只需键入命令即可。

mysql -u username -p -h localhost DATA-BASE-NAME < data.sql

这里 - 第 3 步:这样你就不需要 USE 语句

When we make a dump file with mysqldump , what it contains is a big SQL script for recreating the databse contents.当我们使用mysqldump制作转储文件时,它包含的是一个用于重新创建数据库内容的大型 SQL 脚本。 So we restore it by using starting up MySQL's command-line client:所以我们通过启动 MySQL 的命令行客户端来恢复它:

mysql -uroot -p 

(where root is our admin user name for MySQL), and once connected to the database we need commands to create the database and read the file in to it: (其中root是我们的 MySQL 管理员用户名),一旦连接到数据库,我们需要命令来创建数据库并将文件读入其中:

create database new_db;
use new_db;
\. dumpfile.sql

Details will vary according to which options were used when creating the dump file.详细信息将根据创建转储文件时使用的选项而有所不同。

Run the command to enter into the DB运行命令进入数据库

 # mysql -u root -p 

Enter the password for the user Then Create a New DB输入用户的密码然后创建一个新的数据库

mysql> create database MynewDB;
mysql> exit

And make exit.Afetr that.Run this Command并让 exit.Afetr that.Run 这个命令

# mysql -u root -p  MynewDB < MynewDB.sql

Then enter into the db and type然后进入数据库并输入

mysql> show databases;
mysql> use MynewDB;
mysql> show tables;
mysql> exit

Thats it ........ Your dump will be restored from one DB to another DB就是这样......您的转储将从一个数据库恢复到另一个数据库

Or else there is an Alternate way for dump restore否则有另一种转储还原方式

# mysql -u root -p 

Then enter into the db and type然后进入数据库并输入

mysql> create database MynewDB;
mysql> show databases;
mysql> use MynewDB;
mysql> source MynewDB.sql;
mysql> show tables;
mysql> exit

If you want to view the progress of the dump try this:如果要查看转储的进度,请尝试以下操作:

pv -i 1 -p -t -e /path/to/sql/dump | mysql -u USERNAME -p DATABASE_NAME

You'll of course need 'pv' installed.您当然需要安装“pv”。 This command works only on *nix.此命令仅适用于 *nix。

I got it to work following these steps…我按照这些步骤让它工作......

  1. Open MySQL Administrator and connect to server打开 MySQL Administrator 并连接到服务器

  2. Select "Catalogs" on the left选择左侧的“目录”

  3. Right click in the lower-left box and choose "Create New Schema"右键单击左下角的框并选择“创建新架构”

    MySQL Administrator http://img204.imageshack.us/img204/7528/adminsx9.th.gif enlarge image MySQL 管理员 http://img204.imageshack.us/img204/7528/adminsx9.th.gif放大图片

  4. Name the new schema (example: "dbn")命名新模式(例如:“dbn”)

    MySQL New Schema http://img262.imageshack.us/img262/4374/newwa4.th.gif enlarge image MySQL 新架构 http://img262.imageshack.us/img262/4374/newwa4.th.gif放大图

  5. Open Windows Command Prompt (cmd)打开 Windows 命令提示符 (cmd)

    Windows Command Prompt http://img206.imageshack.us/img206/941/startef7.th.gif enlarge image Windows 命令提示符 http://img206.imageshack.us/img206/941/startef7.th.gif放大图像

  6. Change directory to MySQL installation folder将目录更改为 MySQL 安装文件夹

  7. Execute command:执行命令:

     mysql -u root -p dbn < C:\\dbn_20080912.dump

    …where "root" is the name of the user, "dbn" is the database name, and "C:\\dbn_20080912.dump" is the path/filename of the mysqldump .dump file ...其中“root”是用户名,“dbn”是数据库名称,“C:\\dbn_20080912.dump”是mysqldump .dump文件的路径/文件名

    MySQL dump restore command line http://img388.imageshack.us/img388/2489/cmdjx0.th.gif enlarge image MySQL dump 恢复命令行 http://img388.imageshack.us/img388/2489/cmdjx0.th.gif放大图片

  8. Enjoy!享受!

As a specific example of a previous answer:作为先前答案的具体示例:

I needed to restore a backup so I could import/migrate it into SQL Server.我需要恢复备份,以便我可以将它导入/迁移到 SQL Server。 I installed MySql only, but did not register it as a service or add it to my path as I don't have the need to keep it running.我只安装了 MySql,但没有将其注册为服务或将其添加到我的路径中,因为我不需要让它继续运行。

I used windows explorer to put my dump file in C:\\code\\dump.sql.我使用 Windows 资源管理器将我的转储文件放在 C:\\code\\dump.sql 中。 Then opened MySql from the start menu item.然后从开始菜单项打开MySql。 Created the DB, then ran the source command with the full path like so:创建数据库,然后使用完整路径运行源命令,如下所示:

mysql> create database temp
mysql> use temp
mysql> source c:\code\dump.sql

You can try SQLyog 'Execute SQL script' tool to import sql/dump files.您可以尝试使用SQLyog '执行 SQL 脚本'工具来导入 sql/dump 文件。

在此处输入图片说明

Using a 200MB dump file created on Linux to restore on Windows w/ mysql 5.5 , I had more success with the使用在 Linux 上创建的 200MB 转储文件在带有 mysql 5.5 的 Windows 上恢复,我在

source file.sql

approach from the mysql prompt than with the从 mysql 提示符的方法比使用

mysql  < file.sql

approach on the command line, that caused some Error 2006 "server has gone away" (on windows)在命令行上的方法,导致一些错误 2006“服务器已经消失”(在 Windows 上)

Weirdly, the service created during (mysql) install refers to a my.ini file that did not exist.奇怪的是,在 (mysql) 安装期间创建的服务引用了一个不存在的 my.ini 文件。 I copied the "large" example file to my.ini which I already had modified with the advised increases.我将“大”示例文件复制到 my.ini 中,我已经按照建议的增加修改了该文件。

My values are我的价值观是

[mysqld]
max_allowed_packet = 64M
interactive_timeout = 250
wait_timeout = 250
./mysql -u <username> -p <password> -h <host-name like localhost> <database-name> < db_dump-file

You cannot use the Restore menu in MySQL Admin if the backup / dump wasn't created from there.如果备份/转储不是从那里创建的,则不能使用 MySQL Admin 中的恢复菜单。 It's worth a shot though.不过值得一试。 If you choose to "ignore errors" with the checkbox for that, it will say it completed successfully, although it clearly exits with only a fraction of rows imported...this is with a dump, mind you.如果您选择使用复选框“忽略错误”,它会说它已成功完成,尽管它显然只导入了一小部分行就退出了……请注意,这是一个转储。

mysqldump恢复生成的SQL的单行命令

mysql -u <username> -p<password> -e "source <path to sql file>;"

假设您已经创建了空白数据库,您还可以从命令行恢复数据库,如下所示:

mysql databasename < backup.sql

You can also use the restore menu in MySQL Administrator.您还可以使用 MySQL Administrator 中的恢复菜单。 You just have to open the back-up file, and then click the restore button.您只需打开备份文件,然后单击恢复按钮。

If you are already inside mysql prompt and assume your dump file dump.sql , then we can also use command as below to restore the dump如果你已经在mysql提示符下并假定你的转储文件dump.sql ,那么我们也可以使用下面的命令来恢复转储

mysql> \. dump.sql

If your dump size is larger set max_allowed_packet value to higher.如果您的转储大小较大,请将max_allowed_packet值设置为更高。 Setting this value will help you to faster restoring of dump.设置此值将帮助您更快地恢复转储。

How to Restore MySQL Database with MySQLWorkbench如何使用 MySQLWorkbench 恢复 MySQL 数据库

You can run the drop and create commands in a query tab.您可以在查询选项卡中运行 drop 和 create 命令。

Drop the Schema if it Currently Exists删除当前存在的架构

DROP DATABASE `your_db_name`;

Create a New Schema创建新架构

CREATE SCHEMA `your_db_name`;

Open Your Dump File打开您的转储文件

MySQLWorkbench 打开 sql 文件

  1. Click the Open an SQL script in a new query tab icon and choose your db dump file.单击在新查询选项卡中打开 SQL 脚本图标并选择您的数据库转储文件。
  2. Then Click Run SQL Script...然后单击运行 SQL 脚本...
  3. It will then let you preview the first lines of the SQL dump script.然后它会让您预览 SQL 转储脚本的第一行。
  4. You will then choose the Default Schema Name然后您将选择默认架构名称
  5. Next choose the Default Character Set utf8 is normally a safe bet, but you may be able to discern it from looking at the preview lines for something like character_set .接下来选择默认字符集utf8 通常是一个安全的赌注,但您可以通过查看诸如character_set 之类的预览行来辨别它。
  6. Click Run点击运行
  7. Be patient for large DB restore scripts and watch as your drive space melts away!对大型数据库还原脚本保持耐心,并看着您的驱动器空间消失! 🎉 🎉

Local mysql:本地mysql:

mysql -u root --password=YOUR_PASS --database=YOUR_DB < ./dump.sql 

And if you use docker:如果你使用 docker:

docker exec -i DOCKER_NAME mysql -u root --password=YOUR_PASS --database=YOUR_DB < ./dump.sql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM