Can't connect to MySQL server error 113
When you encounter Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on 'host' (113), it is tempting to assume the database service is down. The fact that netstat -tulpen shows a listener on port 3306 bound to 0.0.0.0 suggests the TCP stack is accepting connections, yet the application layer still rejects them. This discrepancy usually points to a mismatch between network reachability and user-level permissions rather than a kernel crash or service outage.
The error code 113 (EHOSTUNREACH) typically indicates that the remote host is unreachable from the perspective of the client. However, in the context of mysqli_connect(), this often masks a deeper authentication failure where the server refuses to establish a session because the client cannot authenticate against the user table.
Binding vs. Listening
Your configuration shows the MySQL daemon listening on 0.0.0.0:3306. This setting is correct for accepting connections from external IP addresses, provided your operating system firewall allows traffic on port 3306. If you are connecting from a different machine, ensure that your host's firewall (such as firewalld, ufw, or the security group in AWS/Azure) is explicitly allowing inbound TCP traffic on port 3306. Without this, packets will be dropped at the OS level, triggering an unreachable error before they even reach the mysqld process.
The Host Column Trap
The most frequent cause of persistent connection issues, assuming network flow is open, lies in the Host column of the mysql.user table. In your provided snippet, you have a user entry for root with the host set to <ip>. If this refers to a specific IP address or a wildcard (%), the behavior differs significantly from how modern MySQL versions handle authentication.
MySQL 5.7 and later use Caching_sha2_password by default. Older clients, such as older versions of PHP's mysqli extension, may attempt to authenticate using the legacy mysql_native_password. If the server expects the new hash format but receives a legacy challenge-response handshake, it will reject the connection. While this often results in an "Access denied" error, some network configurations or specific client implementations manifest this rejection as an unreachable error due to immediate socket closure upon authentication failure.
To resolve this, you must ensure the root user is configured to accept the password algorithm your client supports. You can force the server to use the legacy plugin for testing purposes:
ALTER USER 'root'@'<ip>' IDENTIFIED WITH mysql_native_password BY 'your_password';
FLUSH PRIVILEGES;
Alternatively, if you are running an older version of MySQL (5.6 or earlier), the default authentication plugin is mysql_native_password, and this mismatch should not occur unless your client library is severely outdated.
Localhost vs. TCP/IP Ambiguity
Your user table shows entries for both localhost and 127.0.0.1. While these often resolve to the same local interface, MySQL treats them as distinct hosts for permission checking. If you are connecting via a Unix socket (which is common on Linux when using mysqli with default settings), the connection bypasses port 3306 entirely and uses the socket file at /var/run/mysqld/mysqld.sock.
The error message explicitly mentions port 3306, implying your PHP code is attempting a TCP/IP connection. Ensure your environment variable MYSQL_UNIX_PORT is not set, or that your application configuration explicitly disables socket usage when connecting over the network. If you connect via socket but try to authenticate using credentials bound only for localhost, the query will fail.
Conclusion on Troubleshooting
If you have verified firewall rules and corrected the user host permissions, the next step is to inspect the MySQL error log. The mysqld log often provides a more granular error message than the client warning. Look for entries indicating "Authentication plugin 'caching_sha2_password' cannot be loaded" or "SSL connection required".
For production environments involving Laravel applications, ensuring your database credentials in the .env file match the exact user and host defined in mysql.user is critical. If you are using a managed service or a specific hosting provider, they may require explicit SSL verification or have modified the default authentication plugin. In such cases, updating your client library to the latest version of PHP is often the most effective fix, as modern PHP versions handle the caching_sha2_password protocol natively without requiring configuration changes on the server side.