服务器的连接可以在客户端和服务器之间的流量上使用压缩,以减少通过连接发送的字节数。默认情况下,连接是不压缩的,但如果服务器和客户端同意使用共同允许的压缩算法,则可以进行压缩。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.
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.
这些配置参数可用于控制连接压缩:These configuration parameters are available for controlling connection compression:
protocol_compression_algorithms系统变量配置服务器允许用于传入连接的压缩算法。Theprotocol_compression_algorithmssystem variable configures which compression algorithms the server permits for incoming connections.--compression-algorithms和--zstd-compression-level命令行选项配置允许的压缩算法以及这些客户端程序的zstd压缩级别:mysql、mysqladmin、mysqlbinlog、mysqlcheck、mysqldump、mysqlimport、mysqlshow、mysqlslap 以及 mysqltest。MySQL Shell 也提供这些命令行选项。The--compression-algorithmsand--zstd-compression-levelcommand-line options configure permitted compression algorithms andzstdcompression level for these client programs: mysql, mysqladmin, mysqlbinlog, mysqlcheck, mysqldump, mysqlimport, mysqlshow, mysqlslap, and mysqltest. MySQL Shell also offers these command-line options.MYSQL_OPT_COMPRESSION_ALGORITHMS和MYSQL_OPT_ZSTD_COMPRESSION_LEVEL选项用于mysql_options()函数,配置使用 MySQL C API 的客户端程序允许使用的压缩算法和zstd压缩级别。TheMYSQL_OPT_COMPRESSION_ALGORITHMSandMYSQL_OPT_ZSTD_COMPRESSION_LEVELoptions for themysql_options()function configure permitted compression algorithms andzstdcompression level for client programs that use the MySQL C API.SOURCE_COMPRESSION_ALGORITHMS和SOURCE_ZSTD_COMPRESSION_LEVEL选项用于CHANGE REPLICATION SOURCE TO语句,配置参与源/副本复制的副本服务器允许使用的压缩算法和zstd压缩级别。TheSOURCE_COMPRESSION_ALGORITHMSandSOURCE_ZSTD_COMPRESSION_LEVELoptions for theCHANGE REPLICATION SOURCE TOstatement configure permitted compression algorithms andzstdcompression level for replica servers participating in source/replica replication.group_replication_recovery_compression_algorithms和group_replication_recovery_zstd_compression_level系统变量配置在新成员加入组并连接到捐赠者时,Group Replication 恢复连接允许的压缩算法和zstd压缩级别。Thegroup_replication_recovery_compression_algorithmsandgroup_replication_recovery_zstd_compression_levelsystem variables configure permitted compression algorithms andzstdcompression level for Group Replication recovery connections when a new member joins a group and connects to a donor.
用于指定压缩算法的配置参数是字符串类型的,并接受一个或多个逗号分隔的压缩算法名称的列表,顺序可以任意,从以下项目中选择(不区分大小写):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 thezlibcompression algorithm.zstd:允许使用zstd压缩算法的连接。zstd: Permit connections that use thezstdcompression algorithm.uncompressed:允许未压缩的连接。uncompressed: Permit uncompressed connections.
因为 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 theprotocol_compression_algorithmssystem variable. By default, the server permits all available algorithms. To configure that setting explicitly at startup, use these lines in the servermy.cnffile:[mysqld] protocol_compression_algorithms=zlib,zstd,uncompressed
要在运行时设置并持久化
protocol_compression_algorithms系统变量,请使用以下语句:To set and persist theprotocol_compression_algorithmssystem 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 PERSISTsets 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 theGLOBALkeyword rather thanPERSIST. See Section 15.7.6.1, “SET Syntax for Variable Assignment”.要仅允许使用
zstd压缩的传入连接,可在启动时这样配置服务器:To permit only incoming connections that usezstdcompression, 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 initiatezliboruncompressedconnections, 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 usingzliborzstdconnections, with a compression level of 7 forzstdconnections, use aCHANGE REPLICATION SOURCE TOstatement:CHANGE REPLICATION SOURCE TO SOURCE_COMPRESSION_ALGORITHMS = 'zlib,zstd', SOURCE_ZSTD_COMPRESSION_LEVEL = 7;
这假设
replica_compressed_protocol系统变量已被禁用,原因可参见配置传统连接压缩。This assumes that thereplica_compressed_protocolsystem 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.
在 MySQL 8.0.18 之前,这些配置参数可用于控制连接压缩:Prior to MySQL 8.0.18, these configuration parameters are available for controlling connection compression:
客户端程序支持一个
--compress命令行选项,以指定与服务器连接时使用压缩。Client programs support a--compresscommand-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 theMYSQL_OPT_COMPRESSoption for themysql_options()function specifies use of compression for the connection to the server.对于源/副本复制,启用系统变量
replica_compressed_protocol可以指定副本与源连接时使用压缩。For source/replica replication, enabling the system variablereplica_compressed_protocolspecifies 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:
遗留的
--compress选项的含义取决于是否指定了--compression-algorithms:The meaning of the legacy--compressoption depends on whether--compression-algorithmsis specified:当未指定
--compression-algorithms时,--compress相当于指定客户端算法集合为zlib,uncompressed。When--compression-algorithmsis not specified,--compressis equivalent to specifying a client-side algorithm set ofzlib,uncompressed.当指定
--compression-algorithms时,--compress相当于指定算法集合为zlib,完整的客户端算法集合是zlib与--compression-algorithms指定的算法的并集。例如,同时使用--compress和--compression-algorithms=zlib,zstd时,允许的算法集合是zlib加上zlib,zstd;也就是zlib,zstd。同时使用--compress和--compression-algorithms=zstd,uncompressed时,允许的算法集合是zlib加上zstd,uncompressed;也就是zlib,zstd,uncompressed。When--compression-algorithmsis specified,--compressis equivalent to specifying an algorithm set ofzliband the full client-side algorithm set is the union ofzlibplus the algorithms specified by--compression-algorithms. For example, with both--compressand--compression-algorithms=zlib,zstd, the permitted-algorithm set iszlibpluszlib,zstd; that is,zlib,zstd. With both--compressand--compression-algorithms=zstd,uncompressed, the permitted-algorithm set iszlibpluszstd,uncompressed; that is,zlib,zstd,uncompressed.
在遗留的
MYSQL_OPT_COMPRESS选项与MYSQL_OPT_COMPRESSION_ALGORITHMS选项之间,也会发生同类型的交互作用,这些选项用于mysql_options()C API 函数。The same type of interaction occurs between the legacyMYSQL_OPT_COMPRESSoption and theMYSQL_OPT_COMPRESSION_ALGORITHMSoption for themysql_options()C API function.如果启用了
replica_compressed_protocol系统变量,它将优先于SOURCE_COMPRESSION_ALGORITHMS,并且如果源和副本都允许该算法,则与源的连接将使用zlib压缩。如果replica_compressed_protocol被禁用,则应用SOURCE_COMPRESSION_ALGORITHMS的值。If thereplica_compressed_protocolsystem variable is enabled, it takes precedence overSOURCE_COMPRESSION_ALGORITHMSand connections to the source usezlibcompression if both source and replica permit that algorithm. Ifreplica_compressed_protocolis disabled, the value ofSOURCE_COMPRESSION_ALGORITHMSapplies.
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 theCompression_algorithmandCompression_levelstatus 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 theprotocol_compression_algorithmssystem 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 Schemareplication_connection_configurationtable hasCOMPRESSION_ALGORITHMSandZSTD_COMPRESSION_LEVELcolumns.mysql.slave_master_info系统表具有Master_compression_algorithms和Master_zstd_compression_level列。如果master.info文件存在,它也包含这些值的行。Themysql.slave_master_infosystem table hasMaster_compression_algorithmsandMaster_zstd_compression_levelcolumns. If themaster.infofile exists, it contains lines for those values as well.