8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

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

6.2.8 连接压缩控制6.2.8 Connection Compression Control

服务器的连接可以在客户端和服务器之间的流量上使用压缩,以减少通过连接发送的字节数。默认情况下,连接是不压缩的,但如果服务器和客户端同意使用共同允许的压缩算法,则可以进行压缩。Connections to the server can use compression on the traffic between client and server to reduce the number of bytes sent over the connection. By default, connections are uncompressed, but can be compressed if the server and the client agree on a mutually permitted compression algorithm.

压缩连接起源于客户端,但会影响客户端和服务器两端的 CPU 负载,因为双方都要执行压缩和解压缩操作。由于启用压缩会降低性能,其好处主要体现在网络带宽低、网络传输时间占压缩和解压缩操作成本的主导地位以及结果集较大的情况下。Compressed connections originate on the client side but affect CPU load on both the client and server sides because both sides perform compression and decompression operations. Because enabling compression decreases performance, its benefits 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 协议连接。This section describes the available compression-control configuration parameters and the information sources available for monitoring use of compression. It applies to classic MySQL protocol connections.

压缩控制适用于客户端程序与服务器的连接,以及参与源/副本复制或组复制的服务器的连接。压缩控制不适用于FEDERATED表的连接。在以下讨论中,“客户端连接”是指从任何支持压缩的来源发起的与服务器的连接的简写,除非上下文表明特定的连接类型。Compression control applies to connections to the server by client programs and by servers participating in source/replica replication or Group Replication. Compression control does not apply to connections for FEDERATED tables. In the following discussion, “client connection” is shorthand for a connection to the server originating from any source for which compression is supported, unless context indicates a specific connection type.

Note

X 协议与 MySQL 服务器实例的连接支持压缩,但 X 协议连接的压缩独立于本文所述的经典 MySQL 协议连接的压缩,并且单独控制。有关 X 协议连接压缩的信息,请参见 第 22.5.5 节,“使用 X 插件进行连接压缩”。X Protocol connections to a MySQL Server instance support compression, but compression for X Protocol connections operates independently from the compression for classic MySQL protocol connections described here, and is controlled separately. See Section 22.5.5, “Connection Compression with X Plugin” for information on X Protocol connection compression.

配置连接压缩Configuring Connection Compression

这些配置参数可用于控制连接压缩:These configuration parameters are available for controlling connection compression:

用于指定压缩算法的配置参数是字符串类型的,并接受一个或多个逗号分隔的压缩算法名称的列表,顺序可以任意,从以下项目中选择(不区分大小写):Configuration parameters that enable specifying compression algorithms are string-valued and take a list of one or more comma-separated compression algorithm names, in any order, chosen from the following items (not case-sensitive):

  • zlib:允许使用 zlib 压缩算法的连接。zlib: Permit connections that use the zlib compression algorithm.

  • zstd:允许使用 zstd 压缩算法的连接。zstd: Permit connections that use the zstd compression algorithm.

  • uncompressed:允许未压缩的连接。uncompressed: Permit uncompressed connections.

Note

因为 uncompressed 是一个可能配置也可能不配置的算法名称,所以可以配置 MySQL 不允许未压缩的连接。Because uncompressed is an algorithm name that may or may not be configured, it is possible to configure MySQL not to permit uncompressed connections.

示例:Examples:

  • 要配置服务器允许用于传入连接的压缩算法,请设置 protocol_compression_algorithms 系统变量。默认情况下,服务器允许使用所有可用的算法。要在启动时显式配置该设置,请在服务器 my.cnf 文件中使用以下行:To configure which compression algorithms the server permits for incoming connections, set the protocol_compression_algorithms system variable. By default, the server permits all available algorithms. To configure that setting explicitly at startup, use these lines in the server my.cnf file:

    [mysqld]
    protocol_compression_algorithms=zlib,zstd,uncompressed
    

    要在运行时设置并持久化 protocol_compression_algorithms 系统变量,请使用以下语句:To set and persist the protocol_compression_algorithms system variable to that value at runtime, use this statement:

    SET PERSIST protocol_compression_algorithms='zlib,zstd,uncompressed';
    

    SET PERSIST 为正在运行的 MySQL 实例设置一个值。它还会保存该值,使其在随后的服务器重启中保留。要更改正在运行的 MySQL 实例的值而不让其在随后的重启中保留,请使用 GLOBAL 关键字而不是 PERSIST。参见 第 15.7.6.1 节,“SET 变量赋值语法”。SET PERSIST sets a value for the running MySQL instance. It also saves the value, causing it to carry over to subsequent server restarts. To change the value for the running MySQL instance without having it carry over to subsequent restarts, use the GLOBAL keyword rather than PERSIST. See Section 15.7.6.1, “SET Syntax for Variable Assignment”.

  • 要仅允许使用 zstd 压缩的传入连接,可在启动时这样配置服务器:To permit only incoming connections that use zstd compression, configure the server at startup like this:

    [mysqld]
    protocol_compression_algorithms=zstd
    

    或者,在运行时进行更改:Or, to make the change at runtime:

    SET PERSIST protocol_compression_algorithms='zstd';
    
  • 要允许 mysql 客户端启动 zlib 或 uncompressed 连接,请按如下方式调用:To permit the mysql client to initiate zlib or uncompressed connections, invoke it like this:

    mysql --compression-algorithms=zlib,uncompressed
    
  • 要配置副本使用 zlib 或 zstd 连接到源,并对 zstd 连接使用压缩级别 7,请使用 CHANGE REPLICATION SOURCE TO 语句:To configure replicas to connect to the source using zlib or zstd connections, with a compression level of 7 for zstd connections, use a CHANGE REPLICATION SOURCE TO statement:

    CHANGE REPLICATION SOURCE TO
      SOURCE_COMPRESSION_ALGORITHMS = 'zlib,zstd',
      SOURCE_ZSTD_COMPRESSION_LEVEL = 7;
    

    这假设replica_compressed_protocol系统变量已被禁用,原因可参见配置传统连接压缩。This assumes that the replica_compressed_protocol system variable is disabled, for reasons described in Configuring Legacy Connection Compression.

为了成功建立连接,连接双方必须就双方均允许的压缩算法达成一致。算法协商过程会尝试使用zlib,然后是zstd,最后是uncompressed。如果双方找不到共同的算法,连接尝试将失败。For successful connection setup, both sides of the connection must agree on a mutually permitted compression algorithm. The algorithm-negotiation process attempts to use zlib, then zstd, then uncompressed. If the two sides can find no common algorithm, the connection attempt fails.

因为双方必须就压缩算法达成一致,并且因为uncompressed是一个不一定被允许的算法值,所以回退到未压缩连接不一定会发生。例如,如果服务器配置允许zstd,而客户端配置允许zlib,uncompressed,则客户端根本无法连接。在这种情况下,双方没有共同的算法,因此连接尝试失败。Because both sides must agree on the compression algorithm, and because uncompressed is an algorithm value that is not necessarily permitted, fallback to an uncompressed connection does not necessarily occur. For example, if the server is configured to permit zstd and a client is configured to permit zlib,uncompressed, the client cannot connect at all. In this case, no algorithm is common to both sides, so connection attempts fail.

允许指定zstd压缩级别的配置参数接受 1 到 22 之间的整数值,值越大表示压缩级别越高。默认的zstd压缩级别是 3。压缩级别设置对不使用zstd压缩的连接没有影响。Configuration parameters that enable specifying the zstd compression level take an integer value from 1 to 22, with larger values indicating increasing levels of compression. The default zstd compression level is 3. The compression level setting has no effect on connections that do not use zstd compression.

可配置的zstd压缩级别允许在减少网络流量但增加 CPU 负载与增加网络流量但降低 CPU 负载之间进行选择。更高的压缩级别可以减少网络拥塞,但额外的 CPU 负载可能会降低服务器性能。A configurable zstd compression level enables choosing between less network traffic and higher CPU load versus more network traffic and lower CPU load. Higher compression levels reduce network congestion but the additional CPU load may reduce server performance.

配置传统连接压缩Configuring Legacy Connection Compression

在 MySQL 8.0.18 之前,这些配置参数可用于控制连接压缩:Prior to MySQL 8.0.18, these configuration parameters are available for controlling connection compression:

  • 客户端程序支持一个 --compress 命令行选项,以指定与服务器连接时使用压缩。Client programs support a --compress command-line option to specify use of compression for the connection to the server.

  • 对于使用 MySQL C API 的程序,为 mysql_options() 函数启用 MYSQL_OPT_COMPRESS 选项可以指定与服务器连接时使用压缩。For programs that use the MySQL C API, enabling the MYSQL_OPT_COMPRESS option for the mysql_options() function specifies use of compression for the connection to the server.

  • 对于源/副本复制,启用系统变量 replica_compressed_protocol 可以指定副本与源连接时使用压缩。For source/replica replication, enabling the system variable replica_compressed_protocol specifies use of compression for replica connections to the source.

在每种情况下,当指定使用压缩时,如果双方都允许,连接将使用zlib压缩算法,否则将回退到未压缩连接。In each case, when use of compression is specified, the connection uses the zlib compression algorithm if both sides permit it, with fallback to an uncompressed connection otherwise.

从 MySQL 8.0.18 开始,上述压缩参数变为遗留参数,因为引入了更多压缩参数以便对连接压缩进行更精细的控制,这些参数在 配置连接压缩 中有描述。例外情况是 MySQL Shell,其中 --compress 命令行选项仍然有效,并且可以在不选择压缩算法的情况下请求压缩。有关 MySQL Shell 的连接压缩控制的信息,请参见 使用压缩连接。As of MySQL 8.0.18, the compression parameters just described become legacy parameters, due to the additional compression parameters introduced for more control over connection compression that are described in Configuring Connection Compression. An exception is MySQL Shell, where the --compress command-line option remains current, and can be used to request compression without selecting compression algorithms. For information on MySQL Shell's connection compression control, see Using Compressed Connections.

旧版本的压缩参数与较新的参数交互,其语义变化如下:The legacy compression parameters interact with the newer parameters and their semantics change as follows:

监控连接压缩Monitoring Connection Compression

Compression 状态变量为 ON 或 OFF,用于指示当前连接是否使用压缩。The Compression status variable is ON or OFF to indicate whether the current connection uses compression.

mysql 客户端 \status 命令会显示一行 Protocol: Compressed,如果当前连接启用了压缩。如果该行不存在,说明连接未压缩。The mysql client \status command displays a line that says Protocol: Compressed if compression is enabled for the current connection. If that line is not present, the connection is uncompressed.

MySQL Shell \status 命令会显示一行 Compression:,显示 Disabled 或 Enabled,用于指示连接是否被压缩。The MySQL Shell \status command displays a Compression: line that says Disabled or Enabled to indicate whether the connection is compressed.

以下额外的信息来源可用于监控连接压缩:These additional sources of information are available for monitoring connection compression:

  • 要监控客户端连接中使用的压缩,请使用 Compression_algorithm 和 Compression_level 状态变量。对于当前连接,它们的值分别表示压缩算法和压缩级别。To monitor compression in use for client connections, use the Compression_algorithm and Compression_level status variables. For the current connection, their values indicate the compression algorithm and compression level, respectively.

  • 要确定服务器为传入连接配置允许的压缩算法,请检查 protocol_compression_algorithms 系统变量。To determine which compression algorithms the server is configured to permit for incoming connections, check the protocol_compression_algorithms system variable.

  • 对于源/副本复制连接,配置的压缩算法和压缩级别可以从多个来源获得:For source/replica replication connections, the configured compression algorithms and compression level are available from multiple sources:

    • 性能模式 replication_connection_configuration 表具有 COMPRESSION_ALGORITHMS 和 ZSTD_COMPRESSION_LEVEL 列。The Performance Schema replication_connection_configuration table has COMPRESSION_ALGORITHMS and ZSTD_COMPRESSION_LEVEL columns.

    • mysql.slave_master_info 系统表具有 Master_compression_algorithms 和 Master_zstd_compression_level 列。如果 master.info 文件存在,它也包含这些值的行。The mysql.slave_master_info system table has Master_compression_algorithms and Master_zstd_compression_level columns. If the master.info file exists, it contains lines for those values as well.