How to run SQL script in MySQL?

MysqlCommand Line

Mysql Problem Overview


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?

Mysql Solutions


Solution 1 - Mysql

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;

Solution 2 - Mysql

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

Solution 3 - Mysql

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

Solution 4 - Mysql

My favorite option to do that will be:

 mysql --user="username" --database="databasename" --password="yourpassword" < "filepath"

I use it this way because when you string it with "" you avoiding wrong path and mistakes with spaces and - and probably more problems with chars that I did not encounter with.


With @elcuco comment I suggest using this command with [space] before so it tell bash to ignore saving it in history, this will work out of the box in most bash.

in case it still saving your command in history please view the following solutions:

https://stackoverflow.com/questions/8473121/execute-command-without-keeping-it-in-history


extra security edit

Just in case you want to be extra safe you can use the following command and enter the password in the command line input:

mysql --user="username" --database="databasename" -p < "filepath"

Solution 5 - Mysql

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:

mysql -u yourusername -p yourpassword yourdatabase < query_file > results_file

Hope this helps someone.

Solution 6 - Mysql

I came here searching for this answer as well, and here is what I found works the best for me: Note I am using Ubuntu 16.x.x

  1. Access mysql using:

mysql -u <your_user> - p

  1. At the mysql prompt, enter:

source file_name.sql

Hope this helps.

Solution 7 - Mysql

Give the path of .sql file as:

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

See In The Image:

Solution 8 - Mysql

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

Solution 9 - Mysql

instead of redirection I would do the following

mysql -h <hostname> -u <username> --password=<password> -D <database> -e 'source <path-to-sql-file>'

This will execute the file path-to-sql-file

Solution 10 - Mysql

Never is a good practice to pass the password argument directly from the command line, it is saved in the ~/.bash_history file and can be accessible from other applications.

Use this instead:

mysql -u user --host host --port 9999 database_name < /scripts/script.sql -p
Enter password:

Solution 11 - Mysql

mysql -uusername -ppassword database-name < file.sql

Solution 12 - Mysql

So many ways to do it.

From Workbench: File > Run SQL Script -- then follow prompts

From Windows Command Line:
   Option 1: mysql -u usr -p
             mysql> source file_path.sql
   Option 2: mysql -u usr -p '-e source file_path.sql'
   Option 3: mysql -u usr -p < file_path.sql
   Option 4: put multiple 'source' statements inside of file_path.sql (I do this to drop and recreate schemas/databases which requires multiple files to be run)
             mysql -u usr -p < file_path.sql

If you get errors from the command line, make sure you have previously run

cd {!!>>mysqld.exe home directory here<<!!}
mysqld.exe --initialize 

This must be run from within the mysqld.exe directory, hence the CD.

Hope this is helpful and not just redundant.

Solution 13 - Mysql

Very likely, you just need to change the slash/blackslash: from

 \home\sivakumar\Desktop\test.sql

to

 /home/sivakumar/Desktop/test.sql

So the command would be:

source /home/sivakumar/Desktop/test.sql

Solution 14 - Mysql

From linux 14.04 to MySql 5.7, using cat command piped with mysql login:

cat /Desktop/test.sql | sudo mysql -uroot -p 

You can use this method for many MySQL commands to execute directly from Shell. Eg:

echo "USE my_db; SHOW tables;" | sudo mysql -uroot -p 

Make sure you separate your commands with semicolon (';').

I didn't see this approach in the answers above and thought it is a good contribution.

Solution 15 - Mysql

use the following from mysql command prompt-

source \\home\\user\\Desktop\\test.sql;

Use no quotation. Even if the path contains space(' ') use no quotation at all.

Solution 16 - Mysql

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:

mysql --host=localhost --user=your_username --password=your_password your_database_name < pathTofilename.sql

Solution 17 - Mysql

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.

Solution 18 - Mysql

If you are here LOOKING FOR A DRUPAL ENVIRONMENT

You can run with drush command on your project directory

drush sqlc

Solution 19 - Mysql

I had this error, and tried all the advice i could get to no avail.

Finally, the problem was that my folder had a space in the folder name which appearing as a forward-slash in the folder path, once i found and removed it, it worked fine.

Solution 20 - Mysql

I use Bash's Here Strings for an instant SQL execution:

mysql -uroot -p <<<"select date(now())"

https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Strings

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1160432View Question on Stackoverflow
Solution 1 - MysqlZoe EdwardsView Answer on Stackoverflow
Solution 2 - MysqlEugen RieckView Answer on Stackoverflow
Solution 3 - MysqlPaul PreibischView Answer on Stackoverflow
Solution 4 - MysqltalsibonyView Answer on Stackoverflow
Solution 5 - MysqlBhushanView Answer on Stackoverflow
Solution 6 - MysqlZac SmithView Answer on Stackoverflow
Solution 7 - MysqlShubham VermaView Answer on Stackoverflow
Solution 8 - MysqlShankar KalyankarView Answer on Stackoverflow
Solution 9 - MysqlDataGuruView Answer on Stackoverflow
Solution 10 - MysqlJorge E. HernándezView Answer on Stackoverflow
Solution 11 - MysqlМартин ПетровView Answer on Stackoverflow
Solution 12 - MysqlDonkeyKongView Answer on Stackoverflow
Solution 13 - MysqlBen LinView Answer on Stackoverflow
Solution 14 - MysqlRicarHincapieView Answer on Stackoverflow
Solution 15 - MysqlRajesh PaulView Answer on Stackoverflow
Solution 16 - Mysql3xCh1_23View Answer on Stackoverflow
Solution 17 - MysqlChad MxView Answer on Stackoverflow
Solution 18 - MysqlXab IonView Answer on Stackoverflow
Solution 19 - Mysqluser3753416View Answer on Stackoverflow
Solution 20 - MysqlTacahiroyView Answer on Stackoverflow