Decoding MySQL Error 111: Connection Refused vs. Timeout
When you encounter Lost connection to MySQL server at 'reading initial communication packet', system error: 111, it is critical to stop and analyze exactly what the operating system is telling you. Error code 111 translates to "Connection refused." This is a hard failure, distinct from a timeout or a DNS resolution issue. It means the TCP handshake never completed successfully because the target endpoint explicitly rejected the connection request before any data could be exchanged.
In the context of your PHP script, this specific error occurring during the mysql_connect phase indicates that the application cannot establish a network path to the MySQL server at the configured hostname. The stack trace provided in the source code shows the use of the deprecated mysql_* extension, which is important because it lacks advanced error reporting compared to mysqli or PDO. However, even with modern drivers, this underlying network topology issue remains the same.
The most common cause for system error 111 on shared hosting environments like GoDaddy is a misconfigured bind-address in the MySQL server configuration file (my.cnf or mysqld.cnf). By default, MySQL often binds to 127.0.0.1, listening exclusively on localhost. If your PHP application attempts to connect via a socket (which is common when running as the same user on the same server) but the hostname configuration forces a TCP connection to a port that isn't listening or is blocked by a firewall, you get this rejection.
If you are connecting from the web server to the database server (a separate instance), ensure that the MySQL server's bind-address directive is set correctly. Setting bind-address = 0.0.0.0 allows the server to accept connections on all network interfaces. Conversely, if the firewall (iptables or firewalld) is active, it might be dropping incoming packets destined for port 3306. You would need to verify that port 3306 is open in your security groups or firewall rules.
Another frequent culprit is the skip-networking option. If this flag is set in the MySQL configuration, the server disables all TCP/IP connections and listens only on Unix sockets. If your PHP code specifies a hostname rather than a socket path (like /var/lib/mysql/mysql.sock), the connection will be refused immediately.
Furthermore, consider the max_connections setting. While less likely to cause an immediate "connection refused" during the initial handshake unless the server is already saturated, hitting this limit can sometimes manifest as connection errors depending on how the client retries. However, error 111 specifically points to a refusal at the network layer, not a resource exhaustion at the application layer (which usually looks different).
Your current PHP implementation relies on superglobals like $_GET without validation. The line $prof = addslashes(htmlentities($prof)); attempts to sanitize input, but relying on this method is fragile. The deprecated mysql_* functions also return boolean values that can be confusing without proper error handling contexts. Migrating to PDO with prepared statements not only resolves the deprecation warnings but also provides a more robust way to diagnose connection issues through exception wrapping.
Finally, check your ini_get('mysqli.reconnect') setting or equivalent in older drivers. Sometimes, a transient network blip causes the initial packet to fail, and the driver might retry. If the underlying issue is a firewall rule dropping packets intermittently, you might see this error flicker on and off. Persistent error 111 almost always points to a static configuration mismatch between the client's expectation of where the server lives and the server's actual listening state.
For modern Laravel applications or any new project, leveraging resources that detail secure database connection practices is advisable. Understanding these foundational network issues ensures you build robust architectures that don't rely on deprecated libraries or fragile hosting configurations.