X 插件支持通过 X 协议连接发送的消息进行压缩。如果服务器和客户端就共同支持的压缩算法达成一致,连接可以被压缩。启用压缩可以减少通过网络发送的字节数,但会增加服务器和客户端在压缩和解压操作上的额外 CPU 负担。因此,压缩的好处主要体现在网络带宽低、网络传输时间占据压缩和解压操作成本主导地位,以及结果集较大的情况下。X Plugin supports compression of messages sent over X Protocol connections. Connections can be compressed if the server and the client agree on a mutually supported compression algorithm. Enabling compression reduces the number of bytes sent over the network, but adds to the server and client an additional CPU cost for compression and decompression operations. The benefits of compression therefore occur primarily when there is low network bandwidth, network transfer time dominates the cost of compression and decompression operations, and result sets are large.
不同的 MySQL 客户端对连接压缩的支持实现方式不同;详细信息请查阅客户端文档。例如,对于经典的 MySQL 协议连接,请参见 第 6.2.8 节,“连接压缩控制”。Different MySQL clients implement support for connection compression differently; consult your client documentation for details. For example, for classic MySQL protocol connections, see Section 6.2.8, “Connection Compression Control”.
默认情况下,X 插件支持 zstd、LZ4 和 Deflate 压缩算法。使用 Deflate 算法进行压缩是通过 zlib 软件库完成的,因此 X 协议连接的 deflate_stream 压缩算法设置等同于经典 MySQL 协议连接的 zlib 设置。By default, X Plugin supports the zstd, LZ4, and Deflate
compression algorithms. Compression with the Deflate algorithm
is carried out using the zlib software library, so the
deflate_stream compression algorithm setting
for X Protocol connections is equivalent to the
zlib setting for classic MySQL protocol
connections.
在服务器端,您可以通过将 mysqlx_compression_algorithms 系统变量设置为仅包含允许的算法来禁止任何压缩算法。算法名称 zstd_stream、lz4_message 和 deflate_stream 可以任意组合指定,顺序和大小写不重要。如果系统变量值为空字符串,则不允许任何压缩算法,连接将不进行压缩。On the server side, you can disallow any of the compression
algorithms by setting the
mysqlx_compression_algorithms
system variable to include only those permitted. The algorithm
names zstd_stream,
lz4_message, and
deflate_stream can be specified in any
combination, and the order and lettercase are not important. If
the system variable value is the empty string, no compression
algorithms are permitted and connections are uncompressed.
下表比较了不同压缩算法的特性,并显示了它们的分配优先级。默认情况下,服务器会选择服务器和客户端共同允许的优先级最高的算法;客户端可以按照后面描述的方式更改优先级。客户端在指定算法时可以使用算法的简短别名。The following table compares the characteristics of the different compression algorithms and shows their assigned priorities. By default, the server chooses the highest-priority algorithm permitted in common by the server and the client; clients may change the priorities as described later. The short form alias for the algorithms can be used by clients when specifying them.
表 22.1 X 协议压缩算法特性Table 22.1 X Protocol Compression Algorithm Characteristics
| 算法Algorithm | 别名Alias | 压缩比Compression Ratio | 吞吐量Throughput | CPU 消耗CPU Cost | 默认优先级Default Priority |
|---|---|---|---|---|---|
zsth_stream |
zstd |
高High | 高High | 中Medium | 第一First |
lz4_message |
lz4 |
低Low | 高High | 最低Lowest | 第二Second |
deflate_stream |
deflate |
高High | 低Low | 最高Highest | 第三Third |
X 协议允许的压缩算法集(无论是用户指定的还是默认的)与 MySQL 服务器为经典 MySQL 协议连接允许的压缩算法集是独立的,经典 MySQL 协议连接允许的压缩算法集由 protocol_compression_algorithms 服务器系统变量指定。如果您没有指定 mysqlx_compression_algorithms 系统变量,X 插件不会回退使用经典 MySQL 协议连接的压缩设置。相反,其默认设置是允许 表 22.1,“X 协议压缩算法特性” 中显示的所有算法。这与 TLS 上下文的情况不同,TLS 上下文如果 X 插件系统变量未设置,则使用 MySQL 服务器设置,如 22.5.3 节,“使用 X 插件的加密连接” 所述。有关经典 MySQL 协议连接压缩的详细信息,请参见 6.2.8 节,“连接压缩控制”。The X Protocol set of permitted compression algorithms (whether
user-specified or default) is independent of the set of
compression algorithms permitted by MySQL Server for
classic MySQL protocol connections, which is specified by the
protocol_compression_algorithms
server system variable. If you do not specify the
mysqlx_compression_algorithms
system variable, X Plugin does not fall back to using
compression settings for classic MySQL protocol connections. Instead,
its default is to permit all algorithms shown in
Table 22.1, “X Protocol Compression Algorithm Characteristics”.
This is unlike the situation for the TLS context, where MySQL
Server settings are used if the X Plugin system variables are
not set, as described in
Section 22.5.3, “Using Encrypted Connections with X Plugin”. For
information about compression for classic MySQL protocol connections,
see Section 6.2.8, “Connection Compression Control”.
在客户端,X 协议连接请求可以指定多个用于压缩控制的参数:On the client side, an X Protocol connection request can specify several parameters for compression control:
压缩模式。The compression mode.
压缩级别。The compression level.
按优先顺序排列的允许的压缩算法列表。The list of permitted compression algorithms in priority order.
某些客户端或连接器可能不支持特定的压缩控制功能。例如,指定 X 协议连接的压缩级别仅由 MySQL Shell 支持,而其他 MySQL 客户端或连接器不支持。有关支持的功能及其使用方法的详细信息,请参阅各个产品的文档。Some clients or Connectors might not support a given compression-control feature. For example, specifying compression level for X Protocol connections is supported only by MySQL Shell, not by other MySQL clients or Connectors. See the documentation for specific products for details about supported features and how to use them.
连接模式具有以下允许值:The connection mode has these permitted values:
disabled:连接未压缩。disabled: The connection is uncompressed.preferred:服务器和客户端协商以找到双方都允许的压缩算法。如果没有可用的公共算法,则连接不进行压缩。如果没有明确指定,这是默认模式。preferred: The server and client negotiate to find a compression algorithm they both permit. If no common algorithm is available, the connection is uncompressed. This is the default mode if not specified explicitly.required:压缩算法协商与preferred模式相同,但如果没有可用的公共算法,连接请求将以错误终止。required: Compression algorithm negotiation occurs as forpreferredmode, but if no common algorithm is available, the connection request terminates with an error.
除了就每个连接商定压缩算法之外,服务器和客户端还可以从适用于商定算法的数值范围中达成压缩级别的共识。随着算法压缩级别的增加,数据压缩比也会增加,从而减少发送消息到客户端所需的网络带宽和传输时间。然而,数据压缩所需的努力也会增加,从而占用服务器的时间、CPU 和内存资源。压缩努力的增加与压缩比的增加并不呈线性关系。In addition to agreeing on a compression algorithm for each connection, the server and client can agree on a compression level from the numeric range that applies to the agreed algorithm. As the compression level for an algorithm increases, the data compression ratio increases, which reduces the network bandwidth and transfer time needed to send the message to the client. However, the effort required for data compression also increases, taking up time and CPU and memory resources on the server. Increases in the compression effort do not have a linear relationship to increases in the compression ratio.
客户端可以在与服务器进行 X 协议连接的能力协商期间请求特定的压缩级别。The client can request a specific compression level during capability negotiations with the server for an X Protocol connection.
MySQL 8.4 中 X 插件使用的默认压缩级别是通过性能测试选择的,被认为在压缩时间和网络传输时间之间达到了良好的平衡。这些默认值不一定与每种算法的库默认值相同。如果客户端没有为算法请求压缩级别,则适用这些默认值。默认压缩级别最初设置为 zstd 为 3,LZ4 为 2,Deflate 为 3。你可以使用 mysqlx_zstd_default_compression_level、mysqlx_lz4_default_compression_level 和 mysqlx_deflate_default_compression_level 系统变量来调整这些设置。The default compression levels used by X Plugin in MySQL
8.4 have been selected through performance testing
as being a good trade-off between compression time and network
transit time. These defaults are not necessarily the same as the
library default for each algorithm. They apply if the client
does not request a compression level for the algorithm. The
default compression levels are initially set to 3 for zstd, 2
for LZ4, and 3 for Deflate. You can adjust these settings using
the
mysqlx_zstd_default_compression_level,
mysqlx_lz4_default_compression_level,
and
mysqlx_deflate_default_compression_level
system variables.
为了防止服务器过度消耗资源,X 插件为每种算法设置了服务器允许的最大压缩级别。如果客户端请求的压缩级别超过此设置,服务器将使用其允许的最大压缩级别(客户端的压缩级别请求仅由 MySQL Shell 支持)。最大压缩级别最初设置为 zstd 为 11,LZ4 为 8,Deflate 为 5。您可以使用 mysqlx_zstd_max_client_compression_level、mysqlx_lz4_max_client_compression_level 和 mysqlx_deflate_max_client_compression_level 系统变量来调整这些设置。To prevent excessive resource consumption on the server,
X Plugin sets a maximum compression level that the server
permits for each algorithm. If a client requests a compression
level that exceeds this setting, the server uses its maximum
permitted compression level (compression level requests by a
client are supported only by MySQL Shell). The maximum
compression levels are initially set to 11 for zstd, 8 for LZ4,
and 5 for Deflate. You can adjust these settings using the
mysqlx_zstd_max_client_compression_level,
mysqlx_lz4_max_client_compression_level,
and
mysqlx_deflate_max_client_compression_level
system variables.
如果服务器和客户端允许多个共同的算法,在协商过程中选择算法的默认优先顺序如表 22.1,“X 协议压缩算法特性”所示。对于支持指定压缩算法的客户端,连接请求可以包含客户端允许的一系列算法,使用算法名称或其别名指定。服务器将列表中算法的顺序视为优先顺序。在此情况下使用的算法是客户端列表中第一个同时在服务器端允许的算法。然而,压缩算法的选项受压缩模式的限制:If the server and client permit more than one algorithm in common, the default priority order for choosing an algorithm during negotiation is shown in Table 22.1, “X Protocol Compression Algorithm Characteristics”. For clients that support specifying compression algorithms, the connection request can include a list of algorithms permitted by the client, specified using the algorithm name or its alias. The order of these algorithms in the list is taken as a priority order by the server. The algorithm used in this case is the first of those in the client list that is also permitted on the server side. However, the option for compression algorithms is subject to the compression mode:
如果压缩模式为
disabled,压缩算法选项将被忽略。If the compression mode isdisabled, the compression algorithms option is ignored.如果压缩模式为
preferred,但客户端允许的算法在服务器端不被允许,则连接将不进行压缩。If the compression mode ispreferredbut no algorithm permitted on the client side is permitted on the server side, the connection is uncompressed.如果压缩模式为
required,但客户端允许的算法在服务器端不被允许,则会发生错误。If the compression mode isrequiredbut no algorithm permitted on the client side is permitted on the server side, an error occurs.
要监控消息压缩的效果,请使用X 插件连接压缩监控中描述的 X 插件状态变量。您可以使用这些状态变量来计算当前设置下消息压缩的好处,并利用这些信息调整您的设置。To monitor the effects of message compression, use the X Plugin status variables described in Monitoring Connection Compression for X Plugin. You can use these status variables to calculate the benefit of message compression with your current settings, and use that information to tune your settings.
X 协议连接压缩具有以下行为和边界:X Protocol connection compression operates with the following behaviors and boundaries:
算法名称中的
_stream和_message后缀表示两种不同的操作模式:在流模式下,一个连接中的所有 X 协议消息将被压缩成一个连续的流,并必须以相同的方式解压——按照压缩的顺序且不能跳过任何消息。 在消息模式下,每条消息都是单独且独立地压缩的,解压顺序不必与压缩顺序一致。同时,消息模式也不要求解压所有压缩的消息。The_streamand_messagesuffixes in algorithm names refer to two different operational modes: In stream mode, all X Protocol messages in a single connection are compressed into a continuous stream and must be decompressed in the same manner—following the order they were compressed and without skipping any messages. In message mode, each message is compressed individually and independently, and need not be decompressed in the order in which they were compressed. Also, message mode does not require all compressed messages to be decompressed.在身份验证成功之前发送的任何消息都不会进行压缩。Compression is not applied to any messages that are sent before authentication succeeds.
控制流消息如
Mysqlx.Ok、Mysqlx.Error和Mysqlx.Sql.StmtExecuteOk不使用压缩。Compression is not applied to control flow messages such asMysqlx.Ok,Mysqlx.Error, andMysqlx.Sql.StmtExecuteOkmessages.如果服务器和客户端在能力协商期间就一个双方允许的压缩算法达成一致,所有其他 X 协议消息都可以被压缩。如果客户端在该阶段未请求压缩,则客户端和服务器都不会对消息进行压缩。All other X Protocol messages can be compressed if the server and client agree on a mutually permitted compression algorithm during capability negotiation. If the client does not request compression at that stage, neither the client nor the server applies compression to messages.
当通过 X 协议连接发送的消息被压缩时,由
mysqlx_max_allowed_packet系统变量指定的限制仍然适用。消息负载解压缩后,网络数据包必须小于此限制。如果超过该限制,X 插件会返回解压缩错误并关闭连接。When messages sent over X Protocol connections are compressed, the limit specified by themysqlx_max_allowed_packetsystem variable still applies. The network packet must be smaller than this limit after the message payload has been decompressed. If the limit is exceeded, X Plugin returns a decompression error and closes the connection.以下几点涉及客户端请求的压缩级别,仅 MySQL Shell 支持:The following points pertain to compression level requests by clients, which is supported only by MySQL Shell:
压缩级别必须由客户端以整数形式指定。如果提供其他类型的值,连接将因错误而关闭。Compression levels must be specified by the client as an integer. If any other type of value is supplied, the connection closes with an error.
如果客户端指定了算法但未指定压缩级别,服务器将使用该算法的默认压缩级别。If a client specifies an algorithm but not a compression level, the server uses its default compression level for the algorithm.
如果客户端请求的算法压缩级别超过服务器允许的最大级别,服务器将使用允许的最大级别。If a client requests an algorithm compression level that exceeds the server maximum permitted level, the server uses the maximum permitted level.
如果客户端请求的算法压缩级别低于服务器允许的最小级别,服务器将使用允许的最小级别。If a client requests an algorithm compression level that is less than the server minimum permitted level, the server uses the minimum permitted level.
您可以使用 X 插件状态变量来监控消息压缩的效果。当使用消息压缩时,会话 Mysqlx_compression_algorithm 状态变量显示当前 X 协议连接使用的压缩算法,而 Mysqlx_compression_level 显示所选择的压缩级别。You can monitor the effects of message compression using the
X Plugin status variables. When message compression is in use,
the session
Mysqlx_compression_algorithm
status variable shows which compression algorithm is in use for
the current X Protocol connection, and
Mysqlx_compression_level shows
the compression level that was selected.
X 插件状态变量可用于计算所选压缩算法的效率(数据压缩比)以及使用消息压缩的整体效果。在以下计算中使用状态变量的会话值,以查看在已知压缩算法的特定会话中消息压缩的好处。或者使用状态变量的全局值,以检查服务器在所有使用 X 协议连接的会话中消息压缩的整体好处,包括这些会话使用的所有压缩算法,以及未使用消息压缩的所有会话。然后,您可以通过调整允许的压缩算法、最大压缩级别和默认压缩级别来优化消息压缩,如 为 X 插件配置连接压缩 中所述。X Plugin status variables can be used to calculate the efficiency of the compression algorithms that are selected (the data compression ratio), and the overall effect of using message compression. Use the session value of the status variables in the following calculations to see what the benefit of message compression was for a specific session with a known compression algorithm. Or use the global value of the status variables to check the overall benefit of message compression for your server across all sessions using X Protocol connections, including all the compression algorithms that have been used for those sessions, and all sessions that did not use message compression. You can then tune message compression by adjusting the permitted compression algorithms, maximum compression level, and default compression level, as described in Configuring Connection Compression for X Plugin.
当使用消息压缩时,Mysqlx_bytes_sent 状态变量显示从服务器发送的字节总数,包括压缩消息的负载(在压缩后测量)、所有未被压缩的消息项(例如 X 协议头)以及任何未压缩的消息。Mysqlx_bytes_sent_compressed_payload 状态变量显示作为压缩消息负载发送的字节总数(在压缩后测量),而 Mysqlx_bytes_sent_uncompressed_frame 状态变量显示同一消息负载的字节总数,但在压缩前测量。因此,可以使用以下公式计算压缩比,该比值显示压缩算法的效率:When message compression is in use, the
Mysqlx_bytes_sent status
variable shows the total number of bytes sent out from the
server, including compressed message payloads measured after
compression, any items in compressed messages that were not
compressed such as X Protocol headers, and any uncompressed
messages. The
Mysqlx_bytes_sent_compressed_payload
status variable shows the total number of bytes sent as
compressed message payloads, measured after compression, and the
Mysqlx_bytes_sent_uncompressed_frame
status variable shows the total number of bytes for those same
message payloads but measured before compression. The
compression ratio, which shows the efficiency of the compression
algorithm, can therefore be calculated using the following
expression:
mysqlx_bytes_sent_uncompressed_frame / mysqlx_bytes_sent_compressed_payload
服务器发送的 X 协议消息的压缩效果可以使用以下表达式计算:The effectiveness of compression for X Protocol messages sent by the server can be calculated using the following expression:
(mysqlx_bytes_sent - mysqlx_bytes_sent_compressed_payload + mysqlx_bytes_sent_uncompressed_frame) / mysqlx_bytes_sent
对于服务器从客户端接收的消息,Mysqlx_bytes_received_compressed_payload 状态变量显示以压缩消息负载形式接收的字节总数(在解压缩前测量),而 Mysqlx_bytes_received_uncompressed_frame 状态变量显示相同消息负载的字节总数,但在解压缩后测量。Mysqlx_bytes_received 状态变量包括在解压缩前测量的压缩消息负载、压缩消息中的任何未压缩项,以及任何未压缩的消息。For messages received by the server from clients, the
Mysqlx_bytes_received_compressed_payload
status variable shows the total number of bytes received as
compressed message payloads, measured before decompression, and
the
Mysqlx_bytes_received_uncompressed_frame
status variable shows the total number of bytes for those same
message payloads but measured after decompression. The
Mysqlx_bytes_received status
variable includes compressed message payloads measured before
decompression, any uncompressed items in compressed messages,
and any uncompressed messages.