Solving MySQL “Aborted Connection: Error Reading Communication Packets” – Complete Troubleshooting Guide


5 views

When dealing with MySQL connection issues, you'll typically see these symptoms in your logs:

[Warning] Aborted connection 320935 to db: 'mydb' user: 'app_user' host: '192.168.1.100' (Got an error reading communication packets)

Client-side errors usually appear as:

Lost connection to MySQL server at 'reading initial communication packet', system error: 111
Host 'client_host' is blocked because of many connection errors

These MySQL server variables directly impact connection stability:

# Current recommended settings for production environments
max_allowed_packet = 512M
connect_timeout = 60
wait_timeout = 28800
interactive_timeout = 28800
net_read_timeout = 120
net_write_timeout = 120
max_connections = 1000
skip_name_resolve = ON

Based on the query log you provided showing repeated Init DB commands, this often indicates connection pool issues. Here's a proper connection handling example in Python:

import mysql.connector
from mysql.connector import errorcode

config = {
  'user': 'app_user',
  'password': 'secure_password',
  'host': 'db.example.com',
  'database': 'mydb',
  'connection_timeout': 30,
  'pool_name': 'mypool',
  'pool_size': 10,
  'autocommit': True
}

try:
    cnx = mysql.connector.connect(**config)
    cursor = cnx.cursor()
    cursor.execute("SELECT * FROM important_table")
    # Process results
except mysql.connector.Error as err:
    if err.errno == errorcode.CR_SERVER_LOST:
        print("Connection lost, retrying...")
        # Implement retry logic here
    else:
        print(f"Database error: {err}")
finally:
    if 'cursor' in locals():
        cursor.close()
    if 'cnx' in locals() and cnx.is_connected():
        cnx.close()

For remote connections, ensure your network infrastructure allows MySQL traffic:

# Check firewall rules (example for Linux)
iptables -L -n | grep 3306

# Test network latency and stability
mtr --report db.example.com

Use these MySQL commands to monitor connection attempts:

SHOW STATUS LIKE 'Aborted_connects';
SHOW STATUS LIKE 'Connection_errors%';
SHOW PROCESSLIST;

When standard fixes don't work, try these:

  1. Enable general query log temporarily
  2. Check for packet fragmentation with tcpdump
  3. Test with both TCP and socket connections
  4. Verify SSL configuration if using encrypted connections

Example tcpdump command for MySQL traffic analysis:

tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings

Implement these in your application architecture:

  • Proper connection pooling
  • Exponential backoff for reconnection attempts
  • Connection health checks
  • Graceful degradation when database is unavailable

When working with MySQL, you might encounter connection issues that manifest in several ways:

[Warning] Aborted connection 320935 to db: '...' user: '...' host: '...' (Got an error reading communication packets)

Client-side errors often appear as:

Lost connection to MySQL server at 'reading initial communication packet', system error: 111
Lost connection to MySQL server at 'reading authorization packet', system error: 0
Host '...' is blocked because of many connection errors

Many developers first try adjusting these parameters in my.cnf:

max_allowed_packet = 512M
connect_timeout = 60
net_read_timeout = 120
innodb_buffer_pool_size = 5G

While these changes can help, they don't always solve the root cause.

Examining the query log of failed connections often reveals this pattern:

22400 Connect user@host on dbname
22400 Query SELECT @@sql_mode
22400 Query SET SESSION sql_mode=''
22400 Query SET NAMES utf8
22400 Init DB dbname
22400 Init DB dbname

The duplicate Init DB command is particularly suspicious and warrants further investigation.

Several network factors can cause these issues:

  • Firewalls interrupting connections
  • Network latency spikes
  • Proxy servers with aggressive timeouts
  • DNS resolution problems

Here's a comprehensive approach to resolving these connection problems:

# 1. Verify your connection string in application code
# Example for Python with MySQL Connector:
import mysql.connector

config = {
  'user': 'username',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'dbname',
  'connection_timeout': 30,
  'connect_timeout': 30,
  'pool_size': 5,
  'pool_reset_session': False
}

try:
    cnx = mysql.connector.connect(**config)
except mysql.connector.Error as err:
    print(f"Connection error: {err}")

Additional server-side recommendations:

# 2. Check and adjust these MySQL variables:
SHOW VARIABLES LIKE '%timeout%';
SHOW VARIABLES LIKE '%max_allowed_packet%';
SHOW STATUS LIKE 'Aborted_connects';

# 3. For persistent blocked hosts:
FLUSH HOSTS;

Implementing proper connection pooling can prevent many of these issues:

// Java example using HikariCP
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost/dbname");
config.setUsername("user");
config.setPassword("password");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
config.setConnectionTimeout(30000); // 30 seconds
config.setIdleTimeout(600000); // 10 minutes
config.setMaxLifetime(1800000); // 30 minutes

HikariDataSource ds = new HikariDataSource(config);

When network problems are suspected, these tools can help:

# Check basic connectivity
ping mysql-server
telnet mysql-server 3306

# Test DNS resolution
nslookup mysql-server
dig mysql-server

# Check for packet loss
mtr mysql-server

For high-latency or unreliable networks, consider these adjustments:

# In my.cnf
[mysqld]
skip-name-resolve
wait_timeout = 28800
interactive_timeout = 28800
net_write_timeout = 60
net_retry_count = 10
max_connect_errors = 1000