How to run SQL script in MySQL?

2020-01-25 03:22发布

I want to execute a text file containing SQL queries, in MySQL.

I tried to run source /Desktop/test.sql and received the error:

mysql> . \home\sivakumar\Desktop\test.sql ERROR: Failed to open file '\home\sivakumar\Desktop\test.sql', error: 2

Any idea on what I am doing wrong?

17条回答
兄弟一词,经得起流年.
2楼-- · 2020-01-25 03:25

Give the path of .sql file as:

source c:/dump/SQL/file_name.sql;

See In The Image:

查看更多
3楼-- · 2020-01-25 03:27

If you’re at the MySQL command line mysql> you have to declare the SQL file as source.

mysql> source \home\user\Desktop\test.sql;
查看更多
混吃等死
4楼-- · 2020-01-25 03:30

you can execute mysql statements that have been written in a text file using the following command:

mysql -u yourusername -p yourpassword yourdatabase < text_file

if yourdatabase has not been created yet, log into your mysql first using:

mysql -u yourusername -p yourpassword yourdatabase

then:

mysql>CREATE DATABASE a_new_database_name

then:

mysql -u yourusername -p yourpassword a_new_database_name < text_file

that should do it!

More info here: http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.html

查看更多
老娘就宠你
5楼-- · 2020-01-25 03:30
mysql -uusername -ppassword database-name < file.sql
查看更多
Luminary・发光体
6楼-- · 2020-01-25 03:32
mysql> source C:\Users\admin\Desktop\fn_Split.sql

Do not specify single quotes.

If the above command is not working, copy the file to c: drive and try again. as shown below,

mysql> source C:\fn_Split.sql
查看更多
兄弟一词,经得起流年.
7楼-- · 2020-01-25 03:33

For future reference, I've found this to work vs the aforementioned methods, under Windows in your msql console:

mysql>>source c://path_to_file//path_to_file//file_name.sql;

If your root drive isn't called "c" then just interchange with what your drive is called. First try backslashes, if they dont work, try the forward slash. If they also don't work, ensure you have your full file path, the .sql extension on the file name, and if your version insists on semi-colons, ensure it's there and try again.

查看更多
登录 后发表回答