How to fix Error while reading greeting packet?

How to fix Error while reading greeting packet?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why "Error while reading greeting packet" Means Your Connection Timed Out

When you see a PHP script outputting Fatal error: Maximum execution time of 30 seconds exceeded alongside warnings like Error while reading greeting packet, it is almost never a problem with your database credentials or network connectivity. It is a timeout issue. The client (your PHP code) sent a request to the server, but the server did not respond within the expected window, causing the connection attempt to abort before any handshake could complete.

In the scenario provided by the source material, the user attempts to connect to a remote server at mydomain.com:2082. The specific error message indicates that the TCP connection might have been established (allowing the client to send data), but the MySQL server did not return its initial "greeting packet" in time. This usually happens when the network round-trip time (RTT) is high, or when intermediate firewalls and load balancers drop idle connections aggressively.

The Role of mysqli_connect_timeout

The default behavior of PHP's mysqli extension is to wait indefinitely for a connection response once the TCP socket is open, or to use a very long timeout that depends on your server's configuration. However, if the remote MySQL server is under heavy load, restarting services, or if there is significant network latency between your XAMPP environment and mydomain.com, this default behavior results in a hang until the PHP execution limit is hit.

To resolve this, you must explicitly set the connection timeout. You can do this using the mysqli_connect_timeout option (available in PHP 5.4+) or by passing it directly to the constructor. The value is specified in seconds.

$host = 'mydomain.com:2082';
$user = 'phpmyadmin_user';
$pass = 'password';
$db   = 'database_name';

// Set a reasonable timeout of 15 seconds for this specific connection attempt
$options = [
    MYSQLI_CONNECT_TIMEOUT => 15, 
    // You can also set PDO::ATTR_TIMEOUT if switching to PDO
];

$conn = mysqli_connect($host, $user, $pass, $db, null, null, $options);

if (!$conn) {
    echo "Connection failed: " . mysqli_connect_error();
} else {
    echo "Connected successfully.";
}

By adding MYSQLI_CONNECT_TIMEOUT => 15, you force the mysqli extension to throw a fatal error if the server doesn't reply within 15 seconds. This prevents your script from hanging and hitting the generic Maximum execution time limit, which is difficult to debug because it gives no information about why the connection failed.

Investigating Network Intermediaries

Even with a timeout set correctly, you will still encounter this error if the connection cannot be completed in time due to external factors. The mydomain.com:2082 host suggests you are connecting to a remote service, possibly a shared hosting control panel or a specific application instance.

Common causes for delayed greeting packets include:

  1. Load Balancers: Many cloud providers and hosting companies place load balancers in front of their MySQL clusters. These devices often have strict idle connection timeouts (e.g., 30 seconds). If your PHP script takes longer to initiate the handshake than the load balancer's idle timeout, the balancer drops the socket before your request reaches the database.
  2. Firewall Rules: Intermediate firewalls may drop packets that sit in a TCP SYN_SENT state for too long.
  3. Server Overload: If the target MySQL server is processing heavy queries, it might not be able to process new connection requests immediately.

In these cases, increasing the timeout value alone is a temporary fix. You need to verify that the network path is stable. Using tools like ping or telnet from your XAMPP machine can help isolate the issue:

# Check if you can reach the host and port specifically
telnet mydomain.com 2082
# Or using PowerShell/Command Prompt
Test-NetConnection -ComputerName mydomain.com -Port 2082

If telnet hangs, the issue is likely at the network level (firewall or routing) rather than a PHP timeout setting.

Why echo $conn is Misleading

The source code includes echo $conn;. This line is redundant in modern PHP versions and can be confusing during debugging. In PHP 5.2+, mysqli_connect() returns a boolean true on success and false on failure. However, older versions or specific configurations might return the link ID. Echoing this variable directly does not help identify timeout errors because a timeout results in a failed connection (false), which will simply print "false" or nothing depending on error reporting settings.

You should always check the return value explicitly and handle errors immediately:

if ($conn === false) {
    $error = mysqli_connect_error();
    // Check specifically for timeouts or server gone away
    if (stripos($error, 'gone away') !== false || stripos($error, 'timeout') !== false) {
        die('Connection timeout: The remote server did not respond in time.');
    }
}

When to Consider mysqli_real_connect

If you are reusing a connection object or managing multiple connections manually, consider using mysqli_real_connect(). This method allows you to pass the host as a string and explicitly control the timeout parameters without relying on the constructor's parsing of the host/port string. It offers slightly more granular control over the connection process if you need to handle retries or complex error logic.

For most simple use cases, however, setting MYSQLI_CONNECT_TIMEOUT in the options array is the cleanest approach. It ensures that your application fails fast and explicitly rather than hanging until the web server kills the script due to execution time limits.

Our network