8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 5 教程 / 5.5 以批处理模式使用 mysql
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

5.5 在批处理模式下使用 mysql5.5 Using mysql in Batch Mode

在前面的章节中,你使用 mysql 交互式地输入语句并查看结果。你也可以在批处理模式下运行 mysql。要这样做,将你想执行的语句放入一个文件中,然后告诉 mysql 从该文件读取输入:In the previous sections, you used mysql interactively to enter statements and view the results. You can also run mysql in batch mode. To do this, put the statements you want to run in a file, then tell mysql to read its input from the file:

$> mysql < batch-file

如果你在 Windows 下运行 mysql 并且文件中有一些特殊字符导致问题,你可以这样做:If you are running mysql under Windows and have some special characters in the file that cause problems, you can do this:

C:\> mysql -e "source batch-file"

如果您需要在命令行上指定连接参数,命令可能如下所示:If you need to specify connection parameters on the command line, the command might look like this:

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

当你以这种方式使用 mysql 时,你是在创建一个脚本文件,然后执行该脚本。When you use mysql this way, you are creating a script file, then executing the script.

如果你希望脚本即使其中的一些语句产生错误也能继续执行,应使用 --force 命令行选项。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.

为什么要使用脚本?这里有几个原因: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.

  • 在开发查询时,批处理模式也非常有用,特别是对于多行语句或多语句序列。如果你犯了错误,你不必重新输入所有内容。只需编辑你的脚本以纠正错误,然后告诉 mysql 再次执行它。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.

  • 如果你的查询产生大量输出,你可以通过分页器运行输出,而不是看着它从屏幕顶部滚动消失: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:

    $> mysql < batch-file | more
    
  • 你可以将输出捕获到文件中以便进一步处理:You can catch the output in a file for further processing:

    $> mysql < batch-file > mysql.out
    
  • 你可以将你的脚本分发给其他人,这样他们也可以运行这些语句。You can distribute your script to other people so that they can also run the statements.

  • 有些情况不允许交互式使用,例如,当你从 cron 作业运行查询时。在这种情况下,你必须使用批处理模式。Some situations do not allow for interactive use, for example, when you run a query from a cron job. In this case, you must use batch mode.

当你以批处理模式运行 mysql 时,默认输出格式与交互式使用时不同(更简洁)。例如,当 mysql 以交互方式运行时,SELECT DISTINCT species FROM pet 的输出如下: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 mysql is run interactively:

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

在批处理模式下,输出看起来像这样:In batch mode, the output looks like this instead:

species
bird
cat
dog
hamster
snake

如果你想在批处理模式下获得交互式输出格式,请使用 mysql -t。要在输出中显示执行的语句,请使用 mysql -v。If you want to get the interactive output format in batch mode, use mysql -t. To echo to the output the statements that are executed, use mysql -v.

你也可以通过使用 source 命令或 \. 命令,在 mysql 提示符下使用脚本:You can also use scripts from the mysql prompt by using the source command or \. command:

mysql> source filename;
mysql> \. filename

更多信息请参见 第 6.5.1.5 节,“从文本文件执行 SQL 语句”。See Section 6.5.1.5, “Executing SQL Statements from a Text File”, for more information.