Search the MySQL manual:

3.6 Using mysql in Batch Mode

In the previous sections, you used mysql interactively to enter queries and view the results. You can also run mysql in batch mode. To do this, put the commands you want to run in a file, then tell mysql to read its input from the file:

shell> mysql < batch-file

If you are running mysql under Windows and have some special characters in the file that causes problems, you can do:

dos> mysql -e "source batch-file"

If you need to specify connection parameters on the command-line, the command might look like this:

shell> mysql -h host -u user -p < batch-file
Enter password: ********

When you use mysql this way, you are creating a script file, then executing the script.

If you want the script to continue even if you have errors, you should use the --force command-line option.

Why use a script? Here are a few reasons:

The default output format is different (more concise) when you run mysql in batch mode than when you use it interactively. For example, the output of SELECT DISTINCT species FROM pet looks like this when run interactively:

+---------+
| species |
+---------+
| bird    |
| cat     |
| dog     |
| hamster |
| snake   |
+---------+

But like this when run in batch mode:

species
bird
cat
dog
hamster
snake

If you want to get the interactive output format in batch mode, use mysql -t. To echo to the output the commands that are executed, use mysql -vvv.

You can also use scripts in the mysql command-line prompt by using the source command:

mysql> source filename;

User Comments

Posted by [name withheld] on Wednesday December 18 2002, @5:27pm[Delete] [Edit]

It would be very nice if source would take a string instead of a line. That way, you could have a lot of the functionality of Procedures by just passing it things like CONCAT(@procedurePath,"/doTheThing.txt").

It would also allow for your sources to call each other without having to hard-code an absolute path into each of them, because they could also do "source CONCAT(@libraryPath,"/checkValidity.txt")".

This seems so useful I (a newcomer to MySQL) was amazed that this was how it DIDN'T work.
-The Amazing Llama

Posted by Yurii Zborovs'kyi on Thursday March 6 2003, @2:57am[Delete] [Edit]

How to measure total batch running time for several SQLs:

# at start of your script file
SET @start=UNIX_TIMESTAMP();

# great job
...
...
...

# at bottom of your script file
SET
@s=@seconds:=UNIX_TIMESTAMP()-@start,
@d=TRUNCATE(@s/86400,0), @s=MOD(@s,86400),
@h=TRUNCATE(@s/3600,0), @s=MOD(@s,3600),
@m=TRUNCATE(@s/60,0), @s=MOD(@s,60),
@day=IF(@d>0,CONCAT(@d,' day'),''),
@hour=IF(@d+@h>0,CONCAT(IF(@d>0,LPAD(@h,2,'0'),@h),' hour'),''),
@min=IF(@d+@h+@m>0,CONCAT(IF(@d+@h>0,LPAD(@m,2,'0'),@m),' min.'),''),
@sec=CONCAT(IF(@d+@h+@m>0,LPAD(@s,2,'0'),@s),' sec.');

SELECT
CONCAT(@seconds,' sec.') AS seconds,
CONCAT_WS(' ',@day,@hour,@min,@sec) AS elapsed;

# enjoy :)

p.s. Tested & works
p.p.s. No fractions of seconds :(

jz

Posted by Musba - on Tuesday May 20 2003, @3:41am[Delete] [Edit]

Example of a Korn Shell Script

#!/bin/ksh
mysql --user=<user> --password=<password> -h <host> <<!!
SELECT VERSION(), CURRENT_DATE;
quit
!!

Add your own comment.