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.在下面的讨论中,“客户端连接”是对来自支持压缩的任何源的服务器连接的简写,除非上下文指示特定的连接类型。
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插件的连接压缩”。
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
压缩水平:mysql、mysqladmin、mysqlbinlog、mysqlcheck、mysqldump、mysqlimport、mysqlpump、mysqlshow、mysqlslap和mysqltest,以及mysql_upgrade。MySQL 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_ALGORITHMS
和MYSQL_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_ALGORITHMS
和MASTER_ZSTD_COMPRESSION_LEVEL
选项为参与源/副本复制的副本服务器配置允许的压缩算法和ZSTD压缩级别。From MySQL 8.0.23, use the statement 在MySQL 8.0.23中,使用语句CHANGE REPLICATION SOURCE TO
and the options SOURCE_COMPRESSION_ALGORITHMS
and SOURCE_ZSTD_COMPRESSION_LEVEL
instead.CHANGE REPLICATION SOURCE TO
和选项SOURCE_COMPRESSION_ALGORITHMS
和SOURCE_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_algorities
和group_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.:允许未压缩的连接。
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 要更改正在运行的MySQL实例的值而不将其转移到后续重启,请使用GLOBAL
keyword rather than PERSIST
. GLOBAL
关键字而不是PERSIST
。See 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 要允许mysql客户端启动zlib
or uncompressed
connections, invoke it like this:zlib
或uncompressed
的连接,请按如下方式调用它:
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):zlib
或zstd
连接连接到源,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负载可能会降低服务器性能。
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 C API的程序,为MYSQL_OPT_COMPRESS
option for the mysql_options()
function specifies use of compression for the connection to the server.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 MySQL Shell是一个例外,其中--compress
command-line option remains current, and can be used to request compression without selecting compression algorithms. --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
,允许的算法集是zlib
加zlib,ztd
;即zlib,zstd
。With 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
,允许的算法集是zlib
加zstd,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
的值。
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的未来版本中被删除。
The Compression
status variable is ON
or OFF
to indicate whether the current connection uses compression.Compression
状态变量为ON
或OFF
,表示当前连接是否使用压缩。
The mysql client mysql客户端\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.\status
命令显示一行,如果当前连接启用了压缩,则显示Protocol:Compressed
。如果该线路不存在,则连接未压缩。
As of 8.0.14, the MySQL Shell 从8.0.14开始,MySQL Shell\status
command displays a Compression:
line that says Disabled
or Enabled
to indicate whether the connection is compressed.\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_algorithm
和Compression_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_AGORITHMS
和ZSTD_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_algorithms
和Master_zstd_compression_level
列。If the 如果master.info
file exists, it contains lines for those values as well.master.info
文件存在,它也包含这些值的行。