I have a .sql
file with an export from phpMyAdmin
. I want to import it into a different server using the command line.
I have a Windows Server 2008 R2 installation. I placed the .sql
file on the C drive, and I tried this command
database_name < file.sql
It is not working I get syntax errors.
- How can I import this file without a problem?
- Do I need to create a database first?
To import a single database, use the following command.
To import multiple database dumps, use the following command.
Among all the answers, for the problem above, this is the best one:
For backup purposes, make a BAT file and run this BAT file using Task Scheduler. It will take a backup of the database; just copy the following line and paste in Notepad and then save the .bat file, and run it on your system.
For importing multiple SQL files at one time, use this:
For simple importing:
For WAMP:
For XAMPP:
Add the
--force
option:While most answers here just mention the simple command
today it's quite common that databases and tables have utf8-collation where this command is not sufficient. Having utf8-collation in the exported tables it's required to use this command:
Surley this works for other charsets too, how to show the right notation can be seen here:
One comment mentioned also that if a database never exists an empty database had to be created first. This might be right in some cases, but depends on the export file. If the exported file includes already the command to create the database then the database never has to be created in a separated step, which even could cause an error on import. So on import it's advisable to have a look first in the file to know which commands are included there, on export it's advisable note the settings, especially if the file is very large and hard to read in an editor.
There are still more parameters for the command which are listed and explained here:
If you use another database-version consider searching for the corresponding version of the manual too. The mentioned links refer to MySQL version 5.7.