Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run SQL script in MySQL?

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?

like image 220
user1160432 Avatar asked Jan 20 '12 10:01

user1160432


People also ask

Can I run the SQL script to MySQL?

To run SQL script in MySQL, use the MySQL workbench. First, you need to open MySQL workbench. Now, File -> Open SQL Script to open the SQL script. Note − Press OK button twice to connect with MySQL.

How do I run a .SQL file in MySQL?

use the MySQL command line client: mysql -h hostname -u user database < path/to/test. sql. Install the MySQL GUI tools and open your SQL file, then execute it.

How do I run a SQL script in MySQL terminal?

To run SQL files from the terminal, you can use the source or the backslash and dot command ( \. ) Next, enter the password for your root user. The path /Users/nsebhastian/Desktop/test/main. sql above needs to be changed to the SQL file path on your computer.


2 Answers

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;
like image 167
Zoe Edwards Avatar answered Oct 07 '22 22:10

Zoe Edwards


You have quite a lot of options:

  • use the MySQL command line client: mysql -h hostname -u user database < path/to/test.sql
  • Install the MySQL GUI tools and open your SQL file, then execute it
  • Use phpmysql if the database is available via your webserver
like image 253
Eugen Rieck Avatar answered Oct 07 '22 23:10

Eugen Rieck