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 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.
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
.