Mysql – How to import a .sql file in MySQL

importMySQLmysqldump

I am trying to import a .sql file using MySQL Workbench and I get this error:

ERROR 1046 (3D000) at line 28: No database selected

I have first created an empty database called with the same name as the .sql file but it doesn't work.
I have also tried to do it with the mysql command client using:

mysqldump -u root database > file.sql 

But it says a have an error in my SQL syntax. Furthermore, I don't know the path where I have to set the file.sql.

Best Answer

juergen d's answer is of course correct; however, considering your error message, you can also add to your SQL file at the beginning line like:

USE your_database_name;

This should also do the job and let you import under the Workbench.

When you are using mysqldump, the exported file will be saved in the current folder. It doesn't matter under what path it is. Just when importing from command line you need to be at the same folder or specify path to the file. But this isn't the case when you are using visual tool like Workbench, where you need to select the file from folder tree anyway.