MySQL Connections

Configuration

host_cache_size

Check

SELECT @@host_cache_size;


SHOW GLOBAL VARIABLES LIKE "host_cache_size";

To view the contents of the host cache query the host_cache Performance Schema table.

Set

SET GLOBAL host_cache_size=200;

Requires the SYSTEM_VARIABLES_ADMIN privilege
Changing the cache size at runtime causes an implicit host cache flushing operation that clears the host cache, truncates the host_cache table, and unblocks any blocked hosts (7)
Setting host_cache_size to 0, either at server startup or at runtime, disables the host cache. With the cache disabled, the server performs a DNS lookup every time a client connects. (7)

Flushing the Host Cache

The cache can be flushed by changing the host_cache_size (see above), truncating the host_cache table, or executing the FLUSH HOSTS command.

TRUNCATE TABLE performance_schema.host_cache;

Requires the DROP privilige for the table.

FLUSH HOSTS;

Requires the RELOAD privilegeDeprecated as of MySQL 8.0.23 (8)
You may need to flush the cache if you get unexpected 'blocked' connections or if you change the IP addresses of any client hosts.

interactive_timeout

The number of seconds the server waits for activity on an interactive connection before closing it. (2)

Check

SELECT @@interactive_timeout;


SHOW GLOBAL VARIABLES LIKE "interactive_timeout";

Set

SET GLOBAL interactive_timeout=28800;

SET GLOBAL interactive_timeout=180;

SET GLOBAL interactive_timeout=3600;

# 8 Hours (Default)

# 3 Minutes

# 1 hour

To get a number of minutes expressed as seconds, multiply the minute value by 60.

max_connections

SELECT @@max_connections;


SHOW GLOBAL VARIABLES LIKE "max_connections";

max_connect_errors

SELECT @@max_connect_erros;


SHOW GLOBAL VARIABLES LIKE "max_connect_errors";

max_execution_time

Specifies how long (in milliseconds) a query can execute for before it is automatically killed.

Check

SELECT @@max_execution_time;


SHOW GLOBAL VARIABLES LIKE "max_execution_time";

Set

SET GLOBAL max_execution_time=0;

SET GLOBAL max_execution_time=1800000;

SET GLOBAL max_execution_time=2700000;

SET GLOBAL max_execution_time=3600000;

# Disable

# 30 minutes

# 45 minutes

# 1 hour

To get a number of minutes expressed as milliseconds, multiply the minute value by 60000.

wait_timeout

The number of seconds the server waits for activity on a noninteractive connection before closing it. (1)

Check

SELECT @@wait_timeout;


SHOW GLOBAL VARIABLES LIKE "wait_timeout";

Note that using:SHOW VARIABLES LIKE "wait_timeout"might give a seemingly confusing result since the "session wait_timeout value is initialized from the global wait_timeout or from the global interactive_timeout, depending on the type of client"

Set

SET GLOBAL wait_timeout=28800;

SET GLOBAL wait_timeout=180;

SET GLOBAL wait_timeout=3600;

# 8 Hours (Default)

# 3 Minutes

# 1 hour

To get a number of minutes expressed as seconds, multiply the minute value by 60.

Status Counters

SHOW GLOBAL STATUS;

Aborted_clients

"The number of connections that were aborted because the client died without closing the connection properly." (3)

Common Causes

Aborted_connects

"The number of failed attempts to connect to the MySQL server." (4)

These should correlate to entires like this in the mysql.log (if log_warnings parameter is set to 2)...

2023-01-11T03:24:06.161568Z 3067 [Note] Aborted connection 3067 to db: 'mydb' user: 'mysuer' host: 'myhost' (Got timeout reading communication packets)

Common Causes

Connections

"The number of connection attempts (successful or not) to the MySQL server." (6)

Connection_errors_accept

"The number of errors that occurred during calls to accept() on the listening port." (5)

Connection_errors_internal

"The number of connections refused due to internal errors in the server, such as failure to start a new thread or an out-of-memory condition." (5)

Connection_errors_max_connections

"The number of connections refused because the server max_connections limit was reached." (5)

Connection_errors_peer_address

"The number of errors that occurred while searching for connecting client IP addresses." (5)

Connection_errors_select

"The number of errors that occurred during calls to select() or poll() on the listening port. (Failure of this operation does not necessarily means a client connection was rejected.)" (5)

Connection_errors_tcpwrap

"The number of connections refused by the libwrap library." (5)

Bibliography & References

https://dev.mysql.com/doc/refman/5.6/en/communication-errors.htmlhttps://www.percona.com/blog/2016/05/16/mysql-got-an-error-reading-communication-packet-errors/
COUNTERSAborted_clients(3) https://dev.mysql.com/doc/refman/5.6/en/server-status-variables.html#statvar_Aborted_clients
Aborted_connects(4) https://dev.mysql.com/doc/refman/5.6/en/server-status-variables.html#statvar_Aborted_connectshttps://dba.stackexchange.com/questions/154531/how-do-i-determine-the-cause-of-high-mysql-aborted-connects-counthttps://stackoverflow.com/questions/23689803/mysql-aborted-connections-how-to-track-down-cause
Connection_errors_xxx(5) https://dev.mysql.com/doc/refman/5.6/en/server-status-variables.html#statvar_Connection_errors_xxx
Connections(6) https://dev.mysql.com/doc/refman/5.6/en/server-status-variables.html#statvar_Connections
CONFIGURATIONhost_cache_size(7) https://dev.mysql.com/doc/refman/8.0/en/host-cache.html(8) https://dev.mysql.com/doc/refman/8.0/en/flush.html#flush-hosts
interactive_timeout(2) https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_interactive_timeouthttps://orbisius.com/blog/change-mysqls-wait_timeout-interactive_timeout-variables-p1699
max_execution_timehttps://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_max_execution_time
wait_timeout(1) https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_wait_timeouthttps://orbisius.com/blog/change-mysqls-wait_timeout-interactive_timeout-variables-p1699https://serverfault.com/questions/355750/mysql-lowering-wait-timeout-value-to-lower-number-of-open-connectionshttps://dba.stackexchange.com/questions/189129/mysql-wait-timeout-variable