W3docs

Lost connection to MySQL server at 'reading initial communication packet', system error: 0

This error message indicates that the client was unable to connect to the MySQL server.

This error message indicates that the client was unable to connect to the MySQL server. Here are some common causes for this error:

  1. max_allowed_packet is too small: The initial handshake packet exceeds the server's configured limit. Increase it in your MySQL configuration file (my.cnf or my.ini):

    [mysqld]
    max_allowed_packet=64M

    Restart the MySQL service after saving.

  2. Incorrect bind-address configuration: The server may only be listening on 127.0.0.1 while you are connecting remotely. Update your configuration:

    [mysqld]
    bind-address = 0.0.0.0

    Restart the service to apply changes.

  3. Firewall or network blocking: A firewall may be blocking port 3306. Verify connectivity from the client machine:

    nc -zv `<hostname>` 3306
  4. MySQL service not running or crashed: Ensure the database process is active. Check its status:

    sudo systemctl status mysql

To troubleshoot this issue further, you can try the following steps:

  1. Check the MySQL error log (typically /var/log/mysql/error.log) for packet size mismatches or authentication plugin errors.
  2. Verify network configuration to ensure the client and server can communicate on port 3306.
  3. Try connecting to the MySQL server from a different client machine to isolate whether the problem is with the client configuration or the server.
  4. If you are still unable to connect, review your PHP DSN (Data Source Name) for correct host, port, and charset settings, or seek assistance from a MySQL expert.