本节描述了 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”.
服务器构建错误消息如下: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 range0x20to0x7E, and using\xhexadecimal encoding for bytes outside that range. For example, if a duplicate-key error occurs for an attempt to insert0x41CF9Finto aVARBINARYunique column, the resulting error message uses UTF-8 with some bytes hexadecimal encoded:Duplicate entry 'A\xCF\x9F' for key 1
一旦构建错误消息,服务器可以将其写入错误日志或发送给客户端: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 thecharacter_set_resultssystem variable. Ifcharacter_set_resultshas a value ofNULLorbinary, no conversion occurs. No conversion occurs if the variable value isutf8mb3orutf8mb4, 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 incharacter_set_results, some encoding may occur during the conversion. The encoding uses Unicode code point values:基本多文种平面 (BMP) 范围内的字符 (
0x0000至0xFFFF) 使用\表示法。Characters in the Basic Multilingual Plane (BMP) range (nnnn0x0000to0xFFFF) are written using\notation.nnnnBMP 范围之外的字符 (
0x10000至0x10FFFF) 使用\+nnnnnn表示法。Characters outside the BMP range (0x10000to0x10FFFF) are written using\+notation.nnnnnn
客户端可以设置
character_set_results来控制接收错误信息时使用的字符集。该变量可以直接设置,也可以通过诸如SET NAMES等间接方式设置。有关character_set_results的更多信息,请参阅 第 12.4 节,“连接字符集和排序规则”。Clients can setcharacter_set_resultsto control the character set in which they receive error messages. The variable can be set directly, or indirectly by means such asSET NAMES. For more information aboutcharacter_set_results, see Section 12.4, “Connection Character Sets and Collations”.