Can't connect to MySQL server on 'ipaddress' (110)
When a PHP script on a shared host like Namecheap fails with Can't connect to MySQL server on 'ipaddress' (110), the error message is deceptive. It suggests a network connectivity issue, yet if you can successfully connect to that same database from your local machine using MySQL Workbench, the physical link between your computer and the server is working fine. The problem isn't usually your internet connection; it's almost certainly how the PHP process attempts to reach the database versus how your desktop client does.
The root cause of error code 110 is a refusal to connect. In network terms, this means the TCP three-way handshake never completed because the remote host actively rejected the incoming connection or timed out waiting for an acknowledgment. When you run MySQL Workbench from outside the server's internal network, your client connects directly to the database port (usually 3306) over the public internet. This often succeeds if the firewall allows traffic on that specific port. However, when you try to connect via mysql_connect() from a web request running on the same server (or a peer within the hosting environment), the dynamic is different.
The code snippet provided uses the legacy MySQL extension:
$db = mysql_connect("ipaddress", "user", "pass") or die('I cannot connect to the database because: ' . mysql_error());
There are two critical issues here beyond just the error 110. First, the mysql_* functions were deprecated in PHP 5.5 and removed entirely in PHP 7.0+. If you are running on a modern stack, this code will not execute at all unless you are on legacy software. Second, the specific error 110 indicates that the connection attempt is being dropped before authentication can even occur.
On shared hosting environments like Namecheap, the primary culprit is almost invariably the firewall configuration or the specific socket requirements of the shared environment. When you connect from Workbench, you are likely bypassing strict local firewall rules because you are an external user initiating a session. In contrast, applications running on the server often hit internal security boundaries that block direct TCP connections to localhost or internal IPs unless explicitly permitted.
Furthermore, many shared hosts do not allow direct TCP/IP connections to the database port from other servers on the network for security reasons. They may only permit connections via Unix sockets if the application and database run on the same machine, or they might require a specific tunnel (like SSH port forwarding) to bridge the gap if you are trying to reach a remote DB from an app server that is not the primary DB host.
To resolve this without upgrading your entire stack immediately (though highly recommended), check the my.cnf configuration on the database server. Ensure there are no bind-address restrictions preventing external IPs from connecting, although this is less likely on shared hosts which often run isolated VMs. More importantly, verify that the PHP process is not being sandboxed in a way that prevents network calls to port 3306.
If you are maintaining legacy code, you must switch to mysqli or PDO. The modern approach handles connection pooling and prepared statements better, but it also surfaces connection errors more clearly. If you find you cannot connect via TCP/IP from your application server to the database server on a shared plan, the only viable long-term solution is often to move the database to the same host as your application or ensure your hosting provider supports secure proxy connections.
For those working with Laravel, modernizing this logic is straightforward. Using PDO within a service container ensures you are not relying on deprecated functions:
$pdo = new PDO("mysql:host={$dbHost};dbname={$dbName}", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
This code will throw an exception immediately if the connection is refused, giving you a much clearer stack trace than the generic die() statement. It also forces you to handle the connection state explicitly, which helps in debugging network timeouts versus authentication failures.
Ultimately, error 110 on a shared host usually points to a firewall or sandbox restriction rather than a broken cable. If your application is on one server and the database is remote, ensure that the hosting provider's terms allow cross-server TCP connections and that you have configured any necessary whitelisting in their control panel.