In MySQL 8.0, error logging uses the MySQL component architecture described at Section 5.5, “MySQL Components”. 在MySQL 8.0中,错误日志记录使用第5.5节,“MySQL组件”中描述的MySQL组件体系结构。The error log subsystem consists of components that perform log event filtering and writing, as well as a system variable that configures which components to enable to achieve the desired logging result.错误日志子系统由执行日志事件筛选和写入的组件以及配置哪些组件以实现所需日志记录结果的系统变量组成。
This section discusses how to select components for error logging. 本节讨论如何选择用于错误日志记录的组件。For instructions specific to log filters, see Section 5.4.2.4, “Types of Error Log Filtering”. 有关日志筛选器的具体说明,请参阅第5.4.2.4节,“错误日志过滤的类型”。For instructions specific to the JSON and system log sinks, see Section 5.4.2.7, “Error Logging in JSON Format”, and Section 5.4.2.8, “Error Logging to the System Log”. 有关JSON和系统日志接收器的具体说明,请参阅第5.4.2.7节,“JSON格式的错误日志记录”和第5.4.2.8节,“系统日志的错误记录”。For additional details about all available log components, see Section 5.5.3, “Error Log Components”.有关所有可用日志组件的更多详细信息,请参阅第5.5.3节,“错误日志组件”。
Component-based error logging offers these features:基于组件的错误日志记录提供了以下功能:
Log events can be filtered by filter components to affect the information available for writing.日志事件可以通过筛选器组件进行过滤,以影响可用于写入的信息。
Log events are output by sink (writer) components. Multiple sink components can be enabled, to write error log output to multiple destinations.日志事件由接收器(写入器)组件输出。可以启用多个接收器组件,将错误日志输出写入多个目标。
Built-in filter and sink components combine to implement the default error log format.内置的筛选器和接收器组件结合起来实现默认的错误日志格式。
A loadable sink enables logging in JSON format.可加载接收器支持JSON格式的日志记录。
A loadable sink enables logging to the system log.可加载接收器允许将日志记录到系统日志中。
System variables control which log components to enable and how each component operates.系统变量控制要启用哪些日志组件以及每个组件的操作方式。
The log_error_services
system variable controls which log components to enable for error logging. log_error_services
系统变量控制要启用哪些日志组件进行错误日志记录。The variable may contain a list with 0, 1, or many elements. In the latter case, elements may be delimited by semicolon or (as of MySQL 8.0.12) comma, optionally followed by space. 变量可以包含一个包含0、1或多个元素的列表。在后一种情况下,元素可以用分号或(从MySQL 8.0.12开始)逗号分隔,可以选择后跟空格。A given setting cannot use both semicolon and comma separators. Component order is significant because the server executes components in the order listed.给定的设置不能同时使用分号和逗号分隔符。组件顺序很重要,因为服务器按列出的顺序执行组件。
By default, 默认情况下,log_error_services
has this value:log_error_services
具有以下值:
mysql> SELECT @@GLOBAL.log_error_services;
+----------------------------------------+
| @@GLOBAL.log_error_services |
+----------------------------------------+
| log_filter_internal; log_sink_internal |
+----------------------------------------+
That value indicates that log events first pass through the 该值表示日志事件首先通过log_filter_internal
filter component, then through the log_sink_internal
sink component, both of which are built in. log_filter_internal
筛选器组件,然后通过log_sink_internal
sink组件,这两个组件都是内置的。A filter modifies log events seen by components named later in the 筛选器会修改稍后在log_error_services
value. log_error_services
值中命名的组件所看到的日志事件。A sink is a destination for log events. 接收器是日志事件的目标。Typically, a sink processes log events into log messages that have a particular format and writes these messages to its associated output, such as a file or the system log.通常,接收器将日志事件处理为具有特定格式的日志消息,并将这些消息写入其相关输出,如文件或系统日志。
The final component in the log_error_services
value cannot be a filter. This is an error because any changes it has on events would have no effect on output:log_error_services
值中的最后一个组件不能是筛选器。这是一个错误,因为它对事件所做的任何更改都不会对输出产生影响:
mysql> SET GLOBAL log_error_services = 'log_filter_internal';
ERROR 1231 (42000): Variable 'log_error_services' can't be set to the value
of 'log_filter_internal'
To correct the problem, include a sink at the end of the value:要更正此问题,请在值的末尾包含一个接收器:
mysql> SET GLOBAL log_error_services = 'log_filter_internal; log_sink_internal';
Query OK, 0 rows affected (0.00 sec)
The combination of log_filter_internal
and log_sink_internal
implements the default error log filtering and output behavior. log_filter_internal
和log_sink_internal
的组合实现了默认的错误日志筛选和输出行为。The action of these components is affected by other server options and system variables:这些组件的操作会受到其他服务器选项和系统变量的影响:
The output destination is determined by the 输出目标由--log-error
option (and, on Windows, --pid-file
and --console
). --log-error
选项确定(在Windows上,还有--pid-file
和--console
)。These determine whether to write error messages to the console or a file and, if to a file, the error log file name. 这些决定是将错误消息写入控制台还是文件,如果写入文件,则决定错误日志文件名。See Section 5.4.2.2, “Default Error Log Destination Configuration”.请参阅第5.4.2.2节,“默认错误日志目标配置”。
The log_error_verbosity
and log_error_suppression_list
system variables affect which types of log events log_filter_internal
permits or suppresses. log_error_verbosity
和log_error_pression_list
系统变量会影响log_filter_internal
允许或禁止哪些类型的日志事件。See Section 5.4.2.5, “Priority-Based Error Log Filtering (log_filter_internal)”.请参阅第5.4.2.5节,“基于优先级的错误日志筛选(Log_filter_internal)”。
To change the set of log components used for error logging, load components as necessary, perform any component-specific configuration, and modify the 要更改用于错误日志记录的日志组件集,请根据需要加载组件,执行任何特定于组件的配置,并修改log_error_services
value. Adding or removing log components is subject to these constraints:log_error_services
值。添加或删除日志组件受以下约束:
To add a log component to the list of enabled components:要将日志组件添加到已启用组件的列表中,请执行以下操作:
Load the component using 使用INSTALL COMPONENT
(unless it is built in or already loaded).INSTALL COMPONENT
(安装组件)加载组件(除非它是内置的或已经加载的)。
If the component exposes any system variables that must be set for component initialization to succeed, assign those variables appropriate values.如果组件公开任何必须设置才能成功初始化组件的系统变量,请为这些变量分配适当的值。
Enable the component by listing it in the 通过在log_error_services
value.log_error_services
值中列出组件来启用该组件。
For a component to be permitted in the 对于log_error_services
value, it must be known. log_error_services
值中允许的组件,它必须是已知的。A component is known if it is built in, or if it is loadable and has been loaded using 组件是内置的,还是可加载的,并且已使用INSTALL COMPONENT
. INSTALL COMPONENT
加载,都是已知的。Attempts to name an unknown component at server startup cause 在服务器启动时尝试命名未知组件会导致log_error_services
to be set to its default value. log_error_services
设置为其默认值。Attempts to name an unknown component at runtime produce an error and the 在运行时尝试命名未知组件会产生错误,并且log_error_services
value remains unchanged.log_error_services
值保持不变。
To disable a log component, remove it from the 要禁用日志组件,请将其从log_error_services
value. log_error_services
值中删除。Then, if the component is loadable and you also want to unload it, use 然后,如果组件是可加载的,并且您也想卸载它,请使用UNINSTALL COMPONENT
.UNINSTALL COMPONENT
。
Attempts to use 尝试使用UNINSTALL COMPONENT
to unload a loadable component that is still named in the log_error_services
value produce an error.UNINSTALL COMPONENT
卸载仍在log_error_services
值中命名的可加载组件会产生错误。
For example, to use the system log sink (例如,要使用系统日志接收器(log_sink_syseventlog
) instead of the default sink (log_sink_internal
), first load the sink component, then modify the log_error_services
value:log_sink_syseventlog
)而不是默认接收器(log_sink_internal
),请首先加载接收器组件,然后修改log_error_services
值:
INSTALL COMPONENT 'file://component_log_sink_syseventlog'; SET GLOBAL log_error_services = 'log_filter_internal; log_sink_syseventlog';
The URN to use for loading a log component with 用于加载带有INSTALL COMPONENT
is the component name prefixed with file://component_
. INSTALL COMPONENT
的日志组件的URN是前缀为file://component_
。For example, for the 例如,对于log_sink_syseventlog
component, the corresponding URN is file://component_log_sink_syseventlog
.log_sink_syseventlog
组件,对应的URN为file://component_log_sink_syseventlog
。
It is possible to configure multiple log sinks, which enables sending output to multiple destinations. 可以配置多个日志接收器,从而可以将输出发送到多个目的地。To enable the system log sink in addition to (rather than instead of) the default sink, set the 要在默认接收器之外启用系统日志接收器(而不是),请设置log_error_services
value like this:log_error_services
值,如下所示:
SET GLOBAL log_error_services = 'log_filter_internal; log_sink_internal; log_sink_syseventlog';
To revert to using only the default sink and unload the system log sink, execute these statements:要恢复到仅使用默认接收器并卸载系统日志接收器,请执行以下语句:
SET GLOBAL log_error_services = 'log_filter_internal; log_sink_internal; UNINSTALL COMPONENT 'file://component_log_sink_syseventlog';
To configure a log component to be enabled at each server startup, use this procedure:要将日志组件配置为在每次服务器启动时启用,请使用以下过程:
If the component is loadable, load it at runtime using 如果组件是可加载的,请在运行时使用INSTALL COMPONENT
. INSTALL COMPONENT
加载它。Loading the component registers it in the 加载该组件会将其注册到mysql.component
system table so that the server loads it automatically for subsequent startups.mysql.component
系统表中,以便服务器自动加载该组件以供后续启动。
Set the 在启动时设置log_error_services
value at startup to include the component name. log_error_services
值以包括组件名称。Set the value either in the server 在服务器my.cnf
file, or use SET PERSIST
, which sets the value for the running MySQL instance and also saves the value to be used for subsequent server restarts; see Section 13.7.6.1, “SET Syntax for Variable Assignment”. my.cnf
文件中设置值,或者使用SET PERSIST
,它为正在运行的MySQL实例设置值,并保存该值以用于后续服务器重新启动;参见第13.7.6.1节,“变量赋值的SET语法”。A value set in my.cnf
takes effect at the next restart. my.cnf
中设置的值将在下次重新启动时生效。A value set using 使用SET PERSIST
takes effect immediately, and for subsequent restarts.SET PERSIST
设置的值将立即生效,并用于随后的重新启动。
Suppose that you want to configure, for every server startup, use of the JSON log sink (假设您想为每次服务器启动配置JSON日志接收器(log_sink_json
) in addition to the built-in log filter and sink (log_filter_internal
, log_sink_internal
). log_sink_json
),以及内置的日志筛选器和接收器(log_filter_internal
、log_sink_internal
)。First load the JSON sink if it is not loaded:如果JSON接收器未加载,请首先加载:
INSTALL COMPONENT 'file://component_log_sink_json';
Then set 然后将log_error_services
to take effect at server startup. You can set it in my.cnf
:log_error_services
设置为在服务器启动时生效。您可以在my.cnf
中设置它:
[mysqld] log_error_services='log_filter_internal; log_sink_internal; log_sink_json'
Or you can set it using 或者可以使用SET PERSIST
:SET PERSIST
进行设置:
SET PERSIST log_error_services = 'log_filter_internal; log_sink_internal; log_sink_json';
The order of components named in log_error_services
is significant, particularly with respect to the relative order of filters and sinks. log_error_services
中命名的组件的顺序非常重要,尤其是在筛选器和接收器的相对顺序方面。Consider this 考虑以下log_error_services
value:log_error_services
值:
log_filter_internal; log_sink_1; log_sink_2
In this case, log events pass to the built-in filter, then to the first sink, then to the second sink. Both sinks receive the filtered log events.在这种情况下,日志事件会传递到内置筛选器,然后传递到第一个接收器,然后再传递到第二个接收器。两个接收器都接收筛选的日志事件。
Compare that to this 将其与此log_error_services
value:log_error_services
值进行比较:
log_sink_1; log_filter_internal; log_sink_2
In this case, log events pass to the first sink, then to the built-in filter, then to the second sink. The first sink receives unfiltered events. The second sink receives filtered events. 在这种情况下,日志事件会传递到第一个接收器,然后传递到内置筛选器,然后再传递到第二个接收器。第一个接收器接收未经过滤的事件。第二个接收器接收经过过滤的事件。You might configure error logging this way if you want one log that contains messages for all log events, and another log that contains messages only for a subset of log events.如果您希望一个日志包含所有日志事件的消息,而另一个日志仅包含日志事件子集的消息,则可以通过这种方式配置错误日志记录。
If the enabled log components include a sink that provides Performance Schema support, events written to the error log are also written to the Performance Schema 如果启用的日志组件包括提供性能架构支持的接收器,则写入错误日志的事件也会写入性能架构error_log
table. error_log
表。This enables examining error log contents using SQL queries. Currently, the traditional-format 这允许使用SQL查询检查错误日志内容。目前,传统格式的log_sink_internal
and JSON-format log_sink_json
sinks support this capability. log_sink_internal
和JSON格式的log_sink_json
接收器都支持这一功能。See Section 27.12.21.1, “The error_log Table”.请参阅第27.12.21.1节,“错误日志表”。