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. 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.
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 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:
The
protocol_compression_algorithmssystem variable configures which compression algorithms the server permits for incoming connections.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.The
MYSQL_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.The
SOURCE_COMPRESSION_ALGORITHMSandSOURCE_ZSTD_COMPRESSION_LEVELoptions for theCHANGE REPLICATION SOURCE TOstatement configure permitted compression algorithms andzstdcompression level for replica servers participating in source/replica replication.The
group_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: Permit connections that use thezlibcompression algorithm.zstd: Permit connections that use thezstdcompression algorithm.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.
Examples:
To configure which compression algorithms the server permits for incoming connections, set the
protocol_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,uncompressedTo set and persist the
protocol_compression_algorithmssystem variable to that value at runtime, use this statement:SET PERSIST protocol_compression_algorithms='zlib,zstd,uncompressed';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”.To permit only incoming connections that use
zstdcompression, configure the server at startup like this:[mysqld] protocol_compression_algorithms=zstdOr, to make the change at runtime:
SET PERSIST protocol_compression_algorithms='zstd';To permit the mysql client to initiate
zliboruncompressedconnections, invoke it like this:mysql --compression-algorithms=zlib,uncompressedTo configure replicas to connect to the source using
zliborzstdconnections, 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;This assumes that the
replica_compressed_protocolsystem variable is disabled, for reasons described in Configuring Legacy Connection Compression.
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.
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.
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.
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.
Prior to MySQL 8.0.18, these configuration parameters are available for controlling connection compression:
Client programs support a
--compresscommand-line option to specify use of compression for the connection to the server.For programs that use the MySQL C API, enabling the
MYSQL_OPT_COMPRESSoption for themysql_options()function specifies use of compression for the connection to the server.For source/replica replication, enabling the system variable
replica_compressed_protocolspecifies use of compression for replica connections to the source.
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.
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:
The meaning of the legacy
--compressoption depends on whether--compression-algorithmsis specified:When
--compression-algorithmsis not specified,--compressis equivalent to specifying a client-side algorithm set ofzlib,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.
The same type of interaction occurs between the legacy
MYSQL_OPT_COMPRESSoption and theMYSQL_OPT_COMPRESSION_ALGORITHMSoption for themysql_options()C API function.If the
replica_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.
The Compression status
variable is ON or OFF to
indicate whether the current connection uses compression.
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.
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:
To monitor compression in use for client connections, use the
Compression_algorithmandCompression_levelstatus variables. 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_algorithmssystem variable.For source/replica replication connections, the configured compression algorithms and compression level are available from multiple sources:
The Performance Schema
replication_connection_configurationtable hasCOMPRESSION_ALGORITHMSandZSTD_COMPRESSION_LEVELcolumns.The
mysql.slave_master_infosystem table hasMaster_compression_algorithmsandMaster_zstd_compression_levelcolumns. If themaster.infofile exists, it contains lines for those values as well.