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.
-
I was able to import mysqldump using the below mysql> use mydatabase; mysql> source sql_file.sql;Giridharan Venugopal– Giridharan Venugopal2017年07月28日 02:03:55 +00:00Commented Jul 28, 2017 at 2:03
6 Answers 6
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.
Export:
mysqldump -u username –-password=your_password database_name > file.sql
Import:
mysql -u username –-password=your_password database_name < file.sql
-
11There has to be no space between
-p
andpassword
tombom– tombom2012年09月14日 11:47:39 +00:00Commented Sep 14, 2012 at 11:47 -
1What if I don't have a password?Barbara Dreamer– Barbara Dreamer2012年09月14日 11:57:18 +00:00Commented Sep 14, 2012 at 11:57
-
1Then skip password parameter.Kazimieras Aliulis– Kazimieras Aliulis2013年05月31日 10:35:06 +00:00Commented May 31, 2013 at 10:35
-
Piping (
mysqldump db -uuser -ppass|mysql new_db -uuser -ppass
) removes the need to use an intermediate file.Pacerier– Pacerier2015年10月12日 10:05:34 +00:00Commented Oct 12, 2015 at 10:05 -
3The database has to exist in MySQL for this to work, I think.Noumenon– Noumenon2016年02月05日 02:02:30 +00:00Commented Feb 5, 2016 at 2:02
You can also import an .sql file as an already connected user to the database manner :
mysql> use your_database_name;
mysql> source file.sql;
-
4source D:/path/file.sql; works superb! remember the front slash instead of back slash.Imdad– Imdad2015年01月09日 11:08:53 +00:00Commented Jan 9, 2015 at 11:08
Another way to import dump files when source <filename>
doesn't work is to do the following:
Dump
~> mysqldump --user=<user> --password=<password> <db_name> > <export_file_name>.sql
Import
> mysql -u <user> -p <pass> <db_name>
mysql> USE <db_name>; (if you didn't already select)
mysql> \. ~/<export_file_name>.sql
You are missing the password in command. Use the following.
mysql --u [username] --password=[password] [database name] < [dump file]
-
Database name is not required if the SQL file contains DB creation +
USE [database name]
.tukusejssirs– tukusejssirs2021年07月28日 16:11:59 +00:00Commented Jul 28, 2021 at 16:11
For Windows, in the MySQL server installation directory (e.g. C:\Program Files\MySQL\MySQL Server 5.5
), in the my.ini file
, add the following line under [mysqld]
in the server section:
max_allowed_packet = 16M
And save the changes. (If you are not allowed to save, then copy and paste to desktop and edit again, then paste it in the same location.)
After that, restart your MySQL server.