Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Mysql scripts in a batch?

Tags:

sql

mysql

Can someone link me to a tutorial or explain if there is a way to create some sort of batch file of mysql scripts / stored procs and run them all at the same time? I can not seem to find any documentation on this online but I feel that I might be searching using the wrong terms.


2 Answers

You can chain mysql scripts by calling them from within a script using the source command (details of command line options)

# my_textfile.sql
# ---------------
USE my_database;
\. subscript1.sql
\. subdir/subscript2.sql
\. /full/path/to/subscript3.sql

Command Line:
mysql < my_textfile.sql

Don't forget the command line options, if you are going to script the files you might need the password/ user account.

mysql -uyouraccount -pyourpassword YourDatabase < mytextfile.sql

This isn't the most secure way to do it because it puts your username/ password on the command line but it works. If you are doing much scripting I suggest you look into .my.cnf and the various options for saving your account/ password in there (and securing that file).

like image 133
Ogre Codes Avatar answered May 03 '26 18:05

Ogre Codes


You can simply create a text file with SQL statements separated with ; and then execute all statements with the MySQL command line client:

# my_textfile.sql
# ---------------
USE my_database;
SELECT * FROM table1;
UPDATE table2 SET foo='bar';

Command Line:
mysql < my_textfile.sql
like image 21
Tomas Avatar answered May 03 '26 17:05

Tomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!