In the previous sections, you used mysql interactively to enter statements and view the results. 在前面的部分中,您以交互方式使用mysql输入语句并查看结果。You can also run mysql in batch mode. 您还可以在批处理模式下运行mysql。To do this, put the statements you want to run in a file, then tell mysql to read its input from the file:为此,请将要运行的语句放在一个文件中,然后告诉mysql从该文件读取其输入:
shell> mysql < batch-file
If you are running mysql under Windows and have some special characters in the file that cause problems, you can do this:如果您在Windows下运行mysql,并且文件中有一些导致问题的特殊字符,则可以执行以下操作:
C:\> 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
Enter password:host
-uuser
-p <batch-file
********
When you use mysql this way, you are creating a script file, then executing the script.当您以这种方式使用mysql时,您正在创建一个脚本文件,然后执行该脚本。
If you want the script to continue even if some of the statements in it produce errors, you should use the 如果希望脚本继续运行,即使其中的某些语句产生错误,则应使用--force
command-line option.--force
命令行选项。
Why use a script? Here are a few reasons:为什么要使用脚本?以下是一些原因:
If you run a query repeatedly (say, every day or every week), making it a script enables you to avoid retyping it each time you execute it.如果重复运行查询(比如每天或每周),将其作为脚本可以避免每次执行时重新键入。
You can generate new queries from existing ones that are similar by copying and editing script files.通过复制和编辑脚本文件,可以从类似的现有查询生成新查询。
Batch mode can also be useful while you're developing a query, particularly for multiple-line statements or multiple-statement sequences. 在开发查询时,批处理模式也很有用,特别是对于多行语句或多语句序列。If you make a mistake, you don't have to retype everything. 如果你犯了一个错误,你不必重新键入所有内容。Just edit your script to correct the error, then tell mysql to execute it again.只需编辑脚本以更正错误,然后告诉mysql再次执行它。
If you have a query that produces a lot of output, you can run the output through a pager rather than watching it scroll off the top of your screen:如果您有一个产生大量输出的查询,您可以通过寻呼机运行输出,而不是看着它从屏幕顶部滚动:
shell> mysql < batch-file
| more
You can catch the output in a file for further processing:您可以在文件中捕获输出以进行进一步处理:
shell> mysql < batch-file
> mysql.out
You can distribute your script to other people so that they can also run the statements.您可以将脚本分发给其他人,以便他们也可以运行语句。
Some situations do not allow for interactive use, for example, when you run a query from a cron job. 有些情况不允许交互使用,例如,当您从cron作业运行查询时。In this case, you must use batch mode.在这种情况下,必须使用批处理模式。
The default output format is different (more concise) when you run mysql in batch mode than when you use it interactively. 在批处理模式下运行mysql时,默认输出格式与以交互方式使用mysql时不同(更简洁)。For example, the output of 例如,当以交互方式运行mysql时,SELECT DISTINCT species FROM pet
looks like this when mysql is run interactively:SELECT DISTINCT species FROM pet
的输出如下所示:
+---------+ | species | +---------+ | bird | | cat | | dog | | hamster | | snake | +---------+
In batch mode, the output looks like this instead:在批处理模式下,输出如下所示:
species bird cat dog hamster snake
If you want to get the interactive output format in batch mode, use mysql -t. 如果您想在批处理模式下获得交互式输出格式,请使用mysql-t。To echo to the output the statements that are executed, use mysql -v.要将执行的语句回显到输出,请使用mysql -v。
You can also use scripts from the mysql prompt by using the 您还可以使用source
command or \.
command:source
命令或\.
命令,从mysql提示符使用脚本:
mysql>source
mysql>filename
;\.
filename
See Section 4.5.1.5, “Executing SQL Statements from a Text File”, for more information.有关更多信息,请参阅第4.5.1.5节,“从文本文件执行SQL语句”。