HY000/2002 php network_error ai_generated true

Warning: mysqli::__construct(): (HY000/2002): Connection timed out in /var/www/app/src/Database/Connection.php:15

ID: php/mysqli-connection-timeout

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
1Evidence
2024-03-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
PHP 8.1 active
PHP 8.2 active
PHP 8.3 active
MySQL 8.0 active
MariaDB 10.6 active

Root Cause

PHP's MySQLi extension fails to establish a TCP connection to the MySQL server within the configured timeout period, often due to network latency, firewall blocking, or incorrect host/port.

generic

中文

PHP的MySQLi扩展在配置的超时时间内无法与MySQL服务器建立TCP连接,通常由网络延迟、防火墙阻止或主机/端口错误导致。

Official Documentation

https://www.php.net/manual/en/mysqli.construct.php

Workarounds

  1. 85% success Increase the MySQLi connection timeout in the connection code: $mysqli = new mysqli('host', 'user', 'pass', 'db', 3306, null, 10); where the last parameter is timeout in seconds.
    Increase the MySQLi connection timeout in the connection code: $mysqli = new mysqli('host', 'user', 'pass', 'db', 3306, null, 10); where the last parameter is timeout in seconds.
  2. 90% success Check network connectivity: run 'telnet <mysql_host> 3306' from the PHP server. If it fails, adjust firewall rules (e.g., 'ufw allow 3306' on Ubuntu) or verify the MySQL server's bind-address is not 127.0.0.1.
    Check network connectivity: run 'telnet <mysql_host> 3306' from the PHP server. If it fails, adjust firewall rules (e.g., 'ufw allow 3306' on Ubuntu) or verify the MySQL server's bind-address is not 127.0.0.1.
  3. 70% success Use persistent connections with mysqli: p:host to reuse connections and reduce timeout issues, but ensure MySQL's wait_timeout is high enough.
    Use persistent connections with mysqli: p:host to reuse connections and reduce timeout issues, but ensure MySQL's wait_timeout is high enough.

中文步骤

  1. Increase the MySQLi connection timeout in the connection code: $mysqli = new mysqli('host', 'user', 'pass', 'db', 3306, null, 10); where the last parameter is timeout in seconds.
  2. Check network connectivity: run 'telnet <mysql_host> 3306' from the PHP server. If it fails, adjust firewall rules (e.g., 'ufw allow 3306' on Ubuntu) or verify the MySQL server's bind-address is not 127.0.0.1.
  3. Use persistent connections with mysqli: p:host to reuse connections and reduce timeout issues, but ensure MySQL's wait_timeout is high enough.

Dead Ends

Common approaches that don't work:

  1. 75% fail

    Increasing PHP's default_socket_timeout in php.ini does not affect MySQLi connection timeout; MySQLi uses its own timeout settings via MYSQLI_OPT_CONNECT_TIMEOUT.

  2. 60% fail

    Restarting the web server or PHP-FPM does not resolve network-level issues like firewall rules or DNS resolution problems.

  3. 50% fail

    Adding 'localhost' instead of '127.0.0.1' may switch from TCP to Unix socket, but if the socket path is wrong, it still fails.