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?
All the top answers are good. But just in case someone wants to run the query from a text file on a remote server AND save results to a file (instead of showing on console), you can do this:
Hope this helps someone.
Since
mysql -u yourusername -p yourpassword yourdatabase < text_file
did not work on a remote server (Amazon's EC2)...Make sure that the Database is created first.
Then:
instead of redirection I would do the following
This will execute the file path-to-sql-file
Very likely, you just need to change the slash/blackslash: from
to
So the command would be:
use the following from mysql command prompt-
Use no quotation. Even if the path contains space(' ') use no quotation at all.
You have quite a lot of options:
mysql -h hostname -u user database < path/to/test.sql