错误消息可以来自服务器端或客户端,每条错误消息都包含错误代码、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.
在服务器端,一些信息是用于错误日志的。有关配置服务器写入日志的位置和方式的信息,请参阅 第 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 层面,MySQL 中有几个错误信息来源:At the SQL level, there are several sources of error information in MySQL:
SQL 语句警告和错误信息可以通过
SHOW WARNINGS和SHOW ERRORS语句获取。warning_count系统变量表示错误、警告和通知的数量(如果sql_notes系统变量被禁用,则不包括通知)。error_count系统变量表示错误的数量。其值不包括警告和通知。SQL statement warning and error information is available through theSHOW WARNINGSandSHOW ERRORSstatements. Thewarning_countsystem variable indicates the number of errors, warnings, and notes (with notes excluded if thesql_notessystem variable is disabled). Theerror_countsystem variable indicates the number of errors. Its value excludes warnings and notes.GET DIAGNOSTICS语句可用于检查诊断区域中的诊断信息。参见 第 15.6.7.3 节,“GET DIAGNOSTICS 语句”。TheGET DIAGNOSTICSstatement may be used to inspect the diagnostic information in the diagnostics area. See Section 15.6.7.3, “GET DIAGNOSTICS Statement”.SHOW REPLICA STATUS语句输出包含关于复制服务器上发生的复制错误的信息。SHOW REPLICA STATUSstatement output includes information about replication errors occurring on replica servers.SHOW ENGINE INNODB STATUS语句输出包括最近的外键错误信息,如果CREATE TABLE语句针对InnoDB表失败。SHOW ENGINE INNODB STATUSstatement output includes information about the most recent foreign key error if aCREATE TABLEstatement for anInnoDBtable fails.
客户端程序从两个来源接收错误: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_errno()返回 MySQL 错误代码。mysql_errno()returns the MySQL error code.mysql_sqlstate()返回 SQLSTATE 值。mysql_sqlstate()returns the SQLSTATE value.mysql_error()返回消息字符串。mysql_error()returns the message string.mysql_stmt_errno()、mysql_stmt_sqlstate()和mysql_stmt_error()是针对预处理语句的相应错误函数。mysql_stmt_errno(),mysql_stmt_sqlstate(), andmysql_stmt_error()are the corresponding error functions for prepared statements.mysql_warning_count()返回最近一次语句的错误、警告和注释的数量。mysql_warning_count()returns the number of errors, warnings, and notes for the most recent statement.
有关客户端库错误函数的描述,请参见 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)
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