How do I restore a dump file from mysqldump?
Asked Answered
V

19

669

I was given a MySQL database file that I need to restore as a database on my Windows Server 2008 machine.

I tried using MySQL Administrator, but I got the following error:

The selected file was generated by mysqldump and cannot be restored by this application.

How do I get this working?

Vulcanize answered 19/9, 2008 at 21:27 Comment(1)
Great question, thanks for asking this before me, upvoted! Ended up going with a non-accepted solution that worked great, but every method is good. Cheers.Spar
H
681

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.

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, i.e. standalone or as part of a package like WAMP). Once you're in that directory, you should be able to just type the command as I have it above.

Homophonic answered 19/9, 2008 at 21:29 Comment(8)
Do I use "MySQL Command Line Client"? I've never used MySQL before.Vulcanize
Open up the dump file in a text editor, it's fairly easy to pick through, if there are any "using" statements, then its of multiple databases, if there are none, you'll have to add one at the top before you can run that command.Homophonic
Of course the database that you put in the "using" statement will have to exist first.Homophonic
see vogs answer. The syntax is mysql -u<user> -p mydatabasename < db_backup.dump no need for a USE statement at the beginning of the fileGuilty
This flat-out flies in the MySQL command line interface. Note, however, that, to avoid a harmless, but disconcerting series of error messages, Windows installations must replace backslashes in path strings with forward slashes. I also used tee to attach a log file, and ran SHOW TABLES behind each restore to demonstrate that it completed.Hotel
"Open up the dump file in a text editor" suggestion seems strange. That might work with small databases or a buffered text editor like EditPad, but with everything else it will choke. It would be nice if you didn't have to take this step.Epigeous
my sql dump file is on my local computer, what should be the path of the dump file then. Is it ok to use the local computer path cause it isn't working for me. Should i have to first upload the dump file to remote server disk space?Goldi
As mentioned by @Guilty you don't need the USE <database-name>; at the top of the file. For a single database dump you can provide the database name in the mysql statement as a parameter. The answer chosen as correct should be edited with this information as not everyone notice useful comments.Menses
H
859

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;
Headline answered 9/6, 2009 at 18:58 Comment(9)
i personally like this method. it shows progress of the dump and also exactly how much time different sql is taking.Wingding
@Phrogz Actually you can supply password on the command line using mysql -u root -psecret without the space, but has the disadvantage that your password shows up in cleartext in process lists and log files. It's better, as you suggest, to use empty -p and type it in the prompt.Hizar
This method has the benefit of allowing session-based variables (e.g. FOREIGN_KEY_CHECKS) to be set without having to edit the dump fileGlauce
this is all well and great when doing this sort of thing by hand, but doesnt work from a bash script.Tonl
also - if you don't know the database name, then run: "head -n 30 db_backup.dump" and you should see the database name thereMulley
I needed to restore a backup so I could import it into 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. I just used windows explorer to put my dump file in C:\code\dump.sql. Then opened mySql from the start menu item. Created the DB, then ran the source command with the full path: mysql> create database temp mysql> use temp mysql> source c:\code\dump.sql (I added it as an answer below that has the better space formatting.)Contrivance
this is better as you don't have to modify the dump file if it is from a single database.Musca
One drawback: it cannot load gzipped dump (e.g. generated with mysqldump db | gzip -9 > dumpfile.sql.gz)Superabound
Worked like a charm! For some reason, I could not run the restore from the command line and needed to log into mysql and use your method. THANK YOU!Pater
H
681

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.

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, i.e. standalone or as part of a package like WAMP). Once you're in that directory, you should be able to just type the command as I have it above.

Homophonic answered 19/9, 2008 at 21:29 Comment(8)
Do I use "MySQL Command Line Client"? I've never used MySQL before.Vulcanize
Open up the dump file in a text editor, it's fairly easy to pick through, if there are any "using" statements, then its of multiple databases, if there are none, you'll have to add one at the top before you can run that command.Homophonic
Of course the database that you put in the "using" statement will have to exist first.Homophonic
see vogs answer. The syntax is mysql -u<user> -p mydatabasename < db_backup.dump no need for a USE statement at the beginning of the fileGuilty
This flat-out flies in the MySQL command line interface. Note, however, that, to avoid a harmless, but disconcerting series of error messages, Windows installations must replace backslashes in path strings with forward slashes. I also used tee to attach a log file, and ran SHOW TABLES behind each restore to demonstrate that it completed.Hotel
"Open up the dump file in a text editor" suggestion seems strange. That might work with small databases or a buffered text editor like EditPad, but with everything else it will choke. It would be nice if you didn't have to take this step.Epigeous
my sql dump file is on my local computer, what should be the path of the dump file then. Is it ok to use the local computer path cause it isn't working for me. Should i have to first upload the dump file to remote server disk space?Goldi
As mentioned by @Guilty you don't need the USE <database-name>; at the top of the file. For a single database dump you can provide the database name in the mysql statement as a parameter. The answer chosen as correct should be edited with this information as not everyone notice useful comments.Menses
O
314

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, i.e. standalone or as part of a package like WAMP). Once you're in that directory, you should be able to just type the command.

Othello answered 19/9, 2008 at 21:45 Comment(0)
P
64
mysql -u username -p -h localhost DATA-BASE-NAME < data.sql

look here - step 3: this way you dont need the USE statement

Putrescent answered 3/6, 2013 at 17:11 Comment(0)
N
41

When we make a dump file with mysqldump, what it contains is a big SQL script for recreating the databse contents. So we restore it by using starting up MySQL’s command-line client:

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:

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

Details will vary according to which options were used when creating the dump file.

Neo answered 19/9, 2008 at 21:34 Comment(1)
\. dumpfile.sql does the trick. Nice one.Clave
A
26

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

# 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
Apace answered 30/3, 2018 at 6:0 Comment(0)
W
21

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. This command works only on *nix.

Witter answered 6/2, 2012 at 8:20 Comment(0)
V
14

I got it to work following these steps…

  1. Open MySQL Administrator and connect to server

  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

  4. Name the new schema (example: "dbn")

    MySQL New Schema http://img262.imageshack.us/img262/4374/newwa4.th.gif enlarge image

  5. Open Windows Command Prompt (cmd)

    Windows Command Prompt http://img206.imageshack.us/img206/941/startef7.th.gif enlarge image

  6. Change directory to MySQL installation folder

  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

    MySQL dump restore command line http://img388.imageshack.us/img388/2489/cmdjx0.th.gif enlarge image

  8. Enjoy!

Vulcanize answered 1/10, 2008 at 13:52 Comment(0)
C
14

As a specific example of a previous answer:

I needed to restore a backup so I could import/migrate it into 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.

I used windows explorer to put my dump file in C:\code\dump.sql. Then opened MySql from the start menu item. 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
Contrivance answered 13/1, 2017 at 22:16 Comment(1)
This is the best and fastest solution. mysqlimport want tables not databaseGingergingerbread
E
12

You can try SQLyog 'Execute SQL script' tool to import sql/dump files.

enter image description here

Erlene answered 21/8, 2012 at 10:19 Comment(0)
T
10

Using a 200MB dump file created on Linux to restore on Windows w/ mysql 5.5 , I had more success with the

source file.sql

approach from the mysql prompt than with the

mysql  < file.sql

approach on the command line, that caused some Error 2006 "server has gone away" (on windows)

Weirdly, the service created during (mysql) install refers to a my.ini file that did not exist. I copied the "large" example file to my.ini which I already had modified with the advised increases.

My values are

[mysqld]
max_allowed_packet = 64M
interactive_timeout = 250
wait_timeout = 250
Transmontane answered 3/1, 2013 at 16:59 Comment(0)
P
10
./mysql -u <username> -p <password> -h <host-name like localhost> <database-name> < db_dump-file
Porosity answered 30/3, 2016 at 11:37 Comment(3)
This is the same as a couple of the other answers, and this is an eight year old question.Cunctation
Though the selected answer is very good, this answer was actually more useful for me. My database file was HUGE, and opening it in Notepad++ was a nightmare, it just couldn't handle the size and would not allow me to add the database name at the top of the sql file. In which case being able to specify the db name from the command line is actually more helpful.Oof
The mysql command does not accept spaces between the options and their arguments. It must be -u<username> instead, and the password should not be provided on the command line at all.Chinchin
G
5

One-liner command to restore the generated SQL from mysqldump

mysql -u <username> -p<password> -e "source <path to sql file>;"
Gruel answered 27/12, 2017 at 16:10 Comment(4)
e.g. mysql -u root -p12345678 -e "source /tmp/backup.sql;"Gruel
Don't use the password like this just use -p and command line will prompt you a secure password request after you press enter.Orthostichy
When you can afford interactive prompt, you are correct. When you can't (e.g. unattended script) you will probably use it inlineGruel
I had to use USE statement too like this line: mysql -p[PASSWORD] -u[USER] -e "USE [DATABASE NAME]; source [PATH/TO/SQL/FILE].sql;"Nipple
S
5

Assuming you already have the blank database created, you can also restore a database from the command line like this:

mysql databasename < backup.sql
Scientistic answered 13/6, 2019 at 22:40 Comment(0)
M
4

You cannot use the Restore menu in MySQL Admin if the backup / dump wasn't created from there. 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.

Marjoram answered 15/6, 2009 at 21:11 Comment(0)
T
3

Local mysql:

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

And if you use docker:

docker exec -i DOCKER_NAME mysql -u root --password=YOUR_PASS --database=YOUR_DB < ./dump.sql
Terms answered 16/12, 2022 at 4:2 Comment(0)
C
1

You can also use the restore menu in MySQL Administrator. You just have to open the back-up file, and then click the restore button.

Cannady answered 9/11, 2008 at 3:53 Comment(0)
S
1

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

If your dump size is larger set max_allowed_packet value to higher. Setting this value will help you to faster restoring of dump.

Shwalb answered 7/10, 2021 at 0:40 Comment(1)
You should add that before running that command, a user has to select a database to restore the data to with USE DB_NAMEVarian
W
0

How to Restore MySQL Database with MySQLWorkbench

You can run the drop and create commands in a query tab.

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 open sql file

  1. Click the Open an SQL script in a new query tab icon and choose your db dump file.
  2. Then Click Run SQL Script...
  3. It will then let you preview the first lines of the SQL dump script.
  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.
  6. Click Run
  7. Be patient for large DB restore scripts and watch as your drive space melts away! 🎉
Wellnigh answered 6/2, 2019 at 1:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.