4.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.与服务器的连接可以对客户端和服务器之间的流量进行压缩,以减少通过连接发送的字节数。默认情况下,连接是未压缩的,但如果服务器和客户端就相互允许的压缩算法达成一致,则可以压缩连接。

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. 压缩连接起源于客户端,但会影响客户端和服务器端的CPU负载,因为双方都执行压缩和解压缩操作。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.因为启用压缩会降低性能,所以它的好处主要发生在网络带宽较低、网络传输时间主导压缩和解压缩操作的成本以及结果集较大的情况下。

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.本节描述了可用的压缩控制配置参数和可用于监视压缩使用的信息源。它适用于经典的MySQL协议连接。

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. 压缩控制不适用于FEDERATED表的连接。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 Protocol connections to a MySQL Server instance support compression from MySQL 8.0.19, but compression for X Protocol connections operates independently from the compression for classic MySQL protocol connections described here, and is controlled separately. MySQL服务器实例的X协议连接支持MySQL 8.0.19的压缩,但X协议连接的压缩独立于本文所述的经典MySQL协议连接的加密,并且是单独控制的。See Section 20.5.5, “Connection Compression with X Plugin” for information on X Protocol connection compression.有关X协议连接压缩的信息,请参阅第20.5.5节,“使用X插件的连接压缩”

Configuring Connection Compression配置连接压缩

As of MySQL 8.0.18, these configuration parameters are available for controlling connection compression:从MySQL 8.0.18开始,这些配置参数可用于控制连接压缩:

  • The protocol_compression_algorithms system variable configures which compression algorithms the server permits for incoming connections.protocol_compression_algorithms系统变量配置服务器允许传入连接使用哪些压缩算法。

  • The --compression-algorithms and --zstd-compression-level command-line options configure permitted compression algorithms and zstd compression level for these client programs: mysql, mysqladmin, mysqlbinlog, mysqlcheck, mysqldump, mysqlimport, mysqlpump, mysqlshow, mysqlslap, and mysqltest, and mysql_upgrade. --compression-algorithms--zstd压缩级别命令行选项为以下客户端程序配置允许的压缩算法和zstd压缩水平:mysqlmysqladminmysqlbinlogmysqlcheckmysqldumpmysqlimportmysqlpumpmysqlshowmysqlslapmysqltest,以及mysql_upgradeMySQL Shell also offers these command-line options from its 8.0.20 release.MySQL Shell在其8.0.20版本中也提供了这些命令行选项。

  • The MYSQL_OPT_COMPRESSION_ALGORITHMS and MYSQL_OPT_ZSTD_COMPRESSION_LEVEL options for the mysql_options() function configure permitted compression algorithms and zstd compression level for client programs that use the MySQL C API.mysql_options()函数的MYSQL_OPT_COMPRESSION_ALGORITHMSMYSQL_OPT_ZSTD_COMPRESSION_LEVEL选项为使用MYSQL C API的客户端程序配置允许的压缩算法和ZSTD压缩级别。

  • The MASTER_COMPRESSION_ALGORITHMS and MASTER_ZSTD_COMPRESSION_LEVEL options for the CHANGE MASTER TO statement configure permitted compression algorithms and zstd compression level for replica servers participating in source/replica replication. CHANGE MASTER TO语句的MASTER_COMPRESSION_ALGORITHMSMASTER_ZSTD_COMPRESSION_LEVEL选项为参与源/副本复制的副本服务器配置允许的压缩算法和ZSTD压缩级别。From MySQL 8.0.23, use the statement CHANGE REPLICATION SOURCE TO and the options SOURCE_COMPRESSION_ALGORITHMS and SOURCE_ZSTD_COMPRESSION_LEVEL instead.在MySQL 8.0.23中,使用语句CHANGE REPLICATION SOURCE TO和选项SOURCE_COMPRESSION_ALGORITHMSSOURCE_ZSTD_COMPRESSER_LEVEL代替。

  • The group_replication_recovery_compression_algorithms and group_replication_recovery_zstd_compression_level system variables configure permitted compression algorithms and zstd compression level for Group Replication recovery connections when a new member joins a group and connects to a donor.group_replication_recovery_compression_algoritiesgroup_replication-recovery_zstd_compression_level系统变量配置了当新成员加入组并连接到供体时,组复制恢复连接的允许压缩算法和zstd压缩级别。

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: Permit connections that use the zlib compression algorithm.:允许使用zlib压缩算法的连接。

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

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

Note注意

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

Examples:示例:

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

    [mysqld]
    protocol_compression_algorithms=zlib,zstd,uncompressed

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

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

    SET PERSIST sets the value for the running MySQL instance. 设置正在运行的MySQL实例的值。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. 要更改正在运行的MySQL实例的值而不将其转移到后续重启,请使用GLOBAL关键字而不是PERSISTSee Section 13.7.6.1, “SET Syntax for Variable Assignment”.请参阅第13.7.6.1节,“变量赋值的SET语法”

  • To permit only incoming connections that use zstd compression, configure the server at startup like this:要只允许使用zstd压缩的传入连接,请在启动时按如下方式配置服务器:

    [mysqld]
    protocol_compression_algorithms=zstd

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

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

    mysql --compression-algorithms=zlib,uncompressed
  • 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 (from MySQL 8.0.23) or CHANGE MASTER TO statement (before MySQL 8.0.23):要配置副本以使用zlibzstd连接连接到源,zstd连接的压缩级别为7,请使用CHANGE REPLICATION SOURCE TO语句(来自MySQL 8.0.23)或CHANGE MASTER TO语句(MySQL 8.0.22之前):

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

    This assumes that the slave_compressed_protocol system variable is disabled, for reasons described in Configuring Legacy Connection Compression.这假设slave_procompressed_protocol系统变量已禁用,原因如配置传统连接压缩中所述。

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.算法协商过程尝试使用zlib,然后是zstd,然后是未压缩的。如果双方找不到共同的算法,则连接尝试失败。

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. 因为双方必须就压缩算法达成一致,而且因为uncompressed是一个不一定允许的算法值,所以不一定会发生回退到未压缩连接的情况。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. 例如,如果服务器配置为允许zstd,而客户端配置为允许zlib,uncompressed,则客户端根本无法连接。In this case, no algorithm is common to both sides, so connection attempts fail.在这种情况下,双方都没有通用的算法,因此连接尝试失败。

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. 用于指定zstd压缩级别的配置参数取1到22之间的整数值,较大的值表示压缩级别的增加。The default zstd compression level is 3. The compression level setting has no effect on connections that do not use zstd compression.默认的zstd压缩级别为3。压缩级别设置对不使用zstd压缩的连接没有影响。

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.可配置的zstd压缩级别允许在较少的网络流量和较高的CPU负载之间进行选择,或者在较多的网络流量与较低的CPU负载间进行选择。更高的压缩级别可以减少网络拥塞,但额外的CPU负载可能会降低服务器性能。

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

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

  • Client programs support a --compress command-line option to specify use of compression for the connection to the server.客户端程序支持--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.对于使用MySQL C API的程序,为mysql_options()函数启用MySQL_OPT_COMPRESS选项将指定对与服务器的连接使用压缩。

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

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.在每种情况下,当指定使用压缩时,如果双方都允许,连接将使用zlib压缩算法,否则将回退到未压缩的连接。

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. 从MySQL 8.0.18开始,由于在配置连接压缩中介绍了额外的压缩参数来更好地控制连接压缩,因此刚才描述的压缩参数成为了遗留参数。An exception is MySQL Shell, where the --compress command-line option remains current, and can be used to request compression without selecting compression algorithms. MySQL Shell是一个例外,其中--compress命令行选项保持最新状态,可用于请求压缩而无需选择压缩算法。For information on MySQL Shell's connection compression control, see Using Compressed Connections.有关MySQL Shell的连接压缩控制的信息,请参阅使用压缩连接

The legacy compression parameters interact with the newer parameters and their semantics change as follows:传统压缩参数与较新的参数相互作用,其语义变化如下:

  • The meaning of the legacy --compress option depends on whether --compression-algorithms is specified:遗留--compress选项的含义取决于是否指定了--compression-algorithms

    • When --compression-algorithms is not specified, --compress is equivalent to specifying a client-side algorithm set of zlib,uncompressed.当未指定--compression-algorithms时,--compress相当于指定客户端算法集zlib,uncompressed

    • When --compression-algorithms is specified, --compress is equivalent to specifying an algorithm set of zlib and the full client-side algorithm set is the union of zlib plus the algorithms specified by --compression-algorithms. 当指定了--compression-algorithms时,--compress相当于指定了zlib的算法集,而完整的客户端算法集是zlib加上--compression-algorithms指定的算法的并集。For example, with both --compress and --compression-algorithms=zlib,zstd, the permitted-algorithm set is zlib plus zlib,zstd; that is, zlib,zstd. 例如,对于--compress--compression-algorithms=zlib,zstd,允许的算法集是zlibzlib,ztd;即zlib,zstdWith both --compress and --compression-algorithms=zstd,uncompressed, the permitted-algorithm set is zlib plus zstd,uncompressed; that is, zlib,zstd,uncompressed.对于--compress--compression-algorithms=zstd,uncompressed,允许的算法集是zlibzstd,uncompressed;亦即,zlib,zstd,uncompressed

  • The same type of interaction occurs between the legacy MYSQL_OPT_COMPRESS option and the MYSQL_OPT_COMPRESSION_ALGORITHMS option for the mysql_options() C API function.对于mysql_options()C API函数,旧版MYSQL_OPT_COMPRESSMYSQL_OPT_COMPRESS选项和MYSQL_OPT_COMPRESSION_ALGORITHMS选项之间也会发生相同类型的交互。

  • If the slave_compressed_protocol system variable is enabled, it takes precedence over MASTER_COMPRESSION_ALGORITHMS and connections to the source use zlib compression if both source and replica permit that algorithm. 如果启用了slave_progressed_protocol系统变量,则它优先于MASTER_COMPRESSION_ALGORITHMS,并且如果源和副本都允许该算法,则与源的连接使用zlib压缩。If slave_compressed_protocol is disabled, the value of MASTER_COMPRESSION_ALGORITHMS applies.如果禁用slave_progressed_protocol,则应用MASTER_COMPRESSION_ALGORITHMS的值。

Note注意

The legacy compression-control parameters are deprecated as of MySQL 8.0.18; expect it to be removed in a future version of MySQL.MySQL 8.0.18已弃用传统的压缩控制参数;预计它将在MySQL的未来版本中被删除。

Monitoring Connection Compression监控连接压缩

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

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客户端\status命令显示一行,如果当前连接启用了压缩,则显示Protocol:Compressed。如果该线路不存在,则连接未压缩。

As of 8.0.14, the MySQL Shell \status command displays a Compression: line that says Disabled or Enabled to indicate whether the connection is compressed.从8.0.14开始,MySQL Shell\status命令显示一行Compression:,显示Disabled或Enabled,以指示连接是否被压缩。

As of MySQL 8.0.18, these additional sources of information are available for monitoring connection compression:从MySQL 8.0.18开始,这些额外的信息源可用于监控连接压缩:

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

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

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

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

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