8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

12.6 错误消息字符集12.6 Error Message Character Set

本节描述了 MySQL 服务器如何使用字符集来构建错误消息。有关错误消息语言(而非字符集)的信息,请参阅 第 12.12 节,“设置错误消息语言”。有关配置错误日志的一般信息,请参阅 第 7.4.2 节,“错误日志”。This section describes how the MySQL server uses character sets for constructing error messages. For information about the language of error messages (rather than the character set), see Section 12.12, “Setting the Error Message Language”. For general information about configuring error logging, see Section 7.4.2, “The Error Log”.

错误消息构造的字符集Character Set for Error Message Construction

服务器构建错误消息如下:The server constructs error messages as follows:

  • 消息模板使用 UTF-8 (utf8mb3)。The message template uses UTF-8 (utf8mb3).

  • 消息模板中的参数会被替换为适用于特定错误发生的值:Parameters in the message template are replaced with values that apply to a specific error occurrence:

    • 诸如表名或列名等标识符在内部使用 UTF-8,因此会原样复制。Identifiers such as table or column names use UTF-8 internally so they are copied as is.

    • 字符(非二进制)字符串值会从它们的字符集转换为 UTF-8。Character (nonbinary) string values are converted from their character set to UTF-8.

    • 二进制字符串值在字节范围 0x20 到 0x7E 内会按原样复制,对于该范围之外的字节则使用 \x 十六进制编码。例如,如果在尝试将 0x41CF9F 插入到 VARBINARY 唯一列时发生重复键错误,生成的错误消息将使用 UTF-8,并对部分字节进行十六进制编码:Binary string values are copied as is for bytes in the range 0x20 to 0x7E, and using \x hexadecimal encoding for bytes outside that range. For example, if a duplicate-key error occurs for an attempt to insert 0x41CF9F into a VARBINARY unique column, the resulting error message uses UTF-8 with some bytes hexadecimal encoded:

      Duplicate entry 'A\xCF\x9F' for key 1
      

错误消息的字符集处理Character Set for Error Message Disposition

一旦构建错误消息,服务器可以将其写入错误日志或发送给客户端:An error message, once constructed, can be written by the server to the error log or sent to clients:

  • 如果服务器将错误消息写入错误日志,它将以 UTF-8 编写,按构建时的格式,不转换为其他字符集。If the server writes the error message to the error log, it writes it in UTF-8, as constructed, without conversion to another character set.

  • 如果服务器向客户端程序发送错误消息,服务器会将其从 UTF-8 转换为由 character_set_results 系统变量指定的字符集。如果 character_set_results 的值为 NULL 或 binary,则不会进行转换。如果变量值为 utf8mb3 或 utf8mb4,也不会进行转换,因为这些字符集的字符表包含了消息构建中使用的所有 UTF-8 字符。If the server sends the error message to a client program, the server converts it from UTF-8 to the character set specified by the character_set_results system variable. If character_set_results has a value of NULL or binary, no conversion occurs. No conversion occurs if the variable value is utf8mb3 or utf8mb4, either, because those character sets have a repertoire that includes all UTF-8 characters used in message construction.

    如果字符无法在 character_set_results 中表示,则在转换过程中可能会进行某些编码。该编码使用 Unicode 代码点值:If characters cannot be represented in character_set_results, some encoding may occur during the conversion. The encoding uses Unicode code point values:

    • 基本多文种平面 (BMP) 范围内的字符 (0x0000 至 0xFFFF) 使用 \nnnn 表示法。Characters in the Basic Multilingual Plane (BMP) range (0x0000 to 0xFFFF) are written using \nnnn notation.

    • BMP 范围之外的字符 (0x10000 至 0x10FFFF) 使用 \+nnnnnn 表示法。Characters outside the BMP range (0x10000 to 0x10FFFF) are written using \+nnnnnn notation.

    客户端可以设置 character_set_results 来控制接收错误信息时使用的字符集。该变量可以直接设置,也可以通过诸如 SET NAMES 等间接方式设置。有关 character_set_results 的更多信息,请参阅 第 12.4 节,“连接字符集和排序规则”。Clients can set character_set_results to control the character set in which they receive error messages. The variable can be set directly, or indirectly by means such as SET NAMES. For more information about character_set_results, see Section 12.4, “Connection Character Sets and Collations”.