Problem

You want to run multiple MySQL commands in sequence constantly. You can type them in one by one, which is time consuming and subjected to errors. Or you can encapsulate them in a script.

Solution

Here is one example of a Multi-Line MySQL script:

mysql –user=’username’ –password=’userpassword’ <<sqlscript
use sample_database
UPDATE comments
SET comment_type=’spam’
WHERE comment_author IN
(SELECT distinct comment_author FROM spam_table);

quit
sqlscript

You need to log into your database before you can run the script. The script is encapsulated between the two flags of  “sqlscript“. The second flag has to start from beginning of a new line and match the first flag.

You can execute the script with shell command in Linux. If you save the above script in a file named sample.script. Then you can run it in Linux in the line below:

sh sample.script