8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

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

B.2 错误信息接口B.2 Error Information Interfaces

错误消息可以来自服务器端或客户端,每条错误消息都包含错误代码、SQLSTATE 值和消息字符串,如 第 B.1 节,“错误消息来源和元素” 所述。有关服务器端、客户端及全局(服务器和客户端共享)错误的列表,请参见 MySQL 8.4 错误消息参考。Error messages can originate on the server side or the client side, and each error message includes an error code, SQLSTATE value, and message string, as described in Section B.1, “Error Message Sources and Elements”. For lists of server-side, client-side, and global (shared between server and clients) errors, see MySQL 8.4 Error Message Reference.

在程序内部进行错误检查时,请使用错误代码数字或符号,而不是错误消息字符串。消息字符串通常不会经常改变,但这是有可能的。此外,如果数据库管理员更改了语言设置,这会影响消息字符串的语言;请参见 第 12.12 节,“设置错误消息语言”。For error checking from within programs, use error code numbers or symbols, not error message strings. Message strings do not change often, but it is possible. Also, if the database administrator changes the language setting, that affects the language of message strings; see Section 12.12, “Setting the Error Message Language”.

MySQL 中的错误信息可以在服务器错误日志中、在 SQL 层、在客户端程序中以及在命令行中获取。Error information in MySQL is available in the server error log, at the SQL level, from within client programs, and at the command line.

错误日志Error Log

在服务器端,一些信息是用于错误日志的。有关配置服务器写入日志的位置和方式的信息,请参阅 第 7.4.2 节,“错误日志”。On the server side, some messages are intended for the error log. For information about configuring where and how the server writes the log, see Section 7.4.2, “The Error Log”.

其他服务器错误信息是用于发送给客户端程序的,并且可以如 客户端错误信息接口 中描述的那样获取。Other server error messages are intended to be sent to client programs and are available as described in Client Error Message Interface.

特定错误代码所在的范围决定了服务器是将错误消息写入错误日志还是发送给客户端。有关这些范围的信息,请参见 错误代码范围。The range within which a particular error code lies determines whether the server writes an error message to the error log or sends it to clients. For information about these ranges, see Error Code Ranges.

SQL 错误消息接口SQL Error Message Interface

在 SQL 层面,MySQL 中有几个错误信息来源:At the SQL level, there are several sources of error information in MySQL:

客户端错误消息接口Client Error Message Interface

客户端程序从两个来源接收错误:Client programs receive errors from two sources:

  • 来源于客户端自身的 MySQL 客户端库内的错误。Errors that originate on the client side from within the MySQL client library.

  • 来源于服务器端并由服务器发送到客户端的错误。这些错误在客户端库中接收,并提供给主客户端程序使用。Errors that originate on the server side and are sent to the client by the server. These are received within the client library, which makes them available to the host client program.

特定错误代码所在的范围决定了它是来源于客户端库内部还是从服务器接收到的有关客户端的信息。有关这些范围的信息,请参见 错误代码范围。The range within which a particular error code lies determines whether it originated from within the client library or was received by the client from the server. For information about these ranges, see Error Code Ranges.

无论错误是来源于客户端库内部还是从服务器接收到,MySQL 客户端程序都是通过调用客户端库中的 C API 函数来获取错误代码、SQLSTATE 值、消息字符串及其他相关信息:Regardless of whether an error originates from within the client library or is received from the server, a MySQL client program obtains the error code, SQLSTATE value, message string, and other related information by calling C API functions in the client library:

有关客户端库错误函数的描述,请参见 MySQL 8.4 C API 开发者指南。For descriptions of the client library error functions, see MySQL 8.4 C API Developer Guide.

MySQL 客户端程序可能以不同方式响应错误。客户端可能显示错误信息,以便用户采取纠正措施;也可能在内部尝试解决或重试失败操作,或采取其他措施。例如,(使用 mysql 客户端时),连接服务器失败可能会导致如下消息:A MySQL client program may respond to an error in varying ways. The client may display the error message so the user can take corrective measures, internally attempt to resolve or retry a failed operation, or take other action. For example, (using the mysql client), a failure to connect to the server might result in this message:

$> mysql -h no-such-host
ERROR 2005 (HY000): Unknown MySQL server host 'no-such-host' (-2)

命令行错误消息接口Command-Line Error Message Interface

perror 程序提供有关错误编号的命令行信息。请参阅 第 6.8.1 节,“perror — 显示 MySQL 错误信息”。The perror program provides information from the command line about error numbers. See Section 6.8.1, “perror — Display MySQL Error Message Information”.

$> perror 1231
MySQL error code MY-001231 (ER_WRONG_VALUE_FOR_VAR): Variable '%-.64s'
can't be set to the value of '%-.200s'

对于 MySQL NDB Cluster 错误,请使用 ndb_perror。请参阅 第 25.5.16 节,“ndb_perror — 获取 NDB 错误信息”。For MySQL NDB Cluster errors, use ndb_perror. See Section 25.5.16, “ndb_perror — Obtain NDB Error Message Information”.

$> ndb_perror 323
NDB error code 323: Invalid nodegroup id, nodegroup already existing:
Permanent error: Application error