SQLSTATE[HY000] [2002] Operation timed out

SQLSTATE[HY000] [2002] Operation timed out

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

SQLSTATE[HY000] [2002] Operation timed out

When a developer moves from a local environment to a remote database, the transition often reveals hidden network configurations that work seamlessly on localhost but fail immediately outside of it. The error SQLSTATE[HY000] [2002] Operation timed out is one of the most common indicators of this issue. It specifically tells you that the TCP connection could not be established within the expected timeframe, rather than indicating a syntax error or an invalid password.

In your specific scenario, the credentials work on a previous project using MySQLi but fail with PDO. While PDO and MySQLi share the same underlying driver for the C extension in PHP, they initialize differently. The code snippets you provided define the DSN (Data Source Name) correctly, and the options array sets ERRMODE_EXCEPTION to ensure failures are caught rather than silently ignored. However, the fact that both attempts result in a timeout suggests the issue lies outside the PHP logic and inside the network path between your local machine and the remote server.

The Client-Server Handshake Failure

The error code 2002 is standard across MySQL clients (CLI, PDO, MySQLi) to denote "Can't connect to MySQL server." When you tested this via the terminal using /Applications/MAMP/Library/bin/mysql, you received ERROR 2003 (HY000): Can't connect to MySQL server on 'xx.xxx.xxx.xx' (60).

This is the definitive clue. A timeout occurs when the client sends a TCP SYN packet to the server's default port (usually 3306), and either:

  1. The firewall drops the packet without responding.
  2. The server refuses the connection but does not send a RST packet immediately.
  3. The network route is unstable or blocked by an intermediate device.

Since your terminal command failed with the exact same host, port, and credentials used in PHP, ruling out a PHP-specific bug is straightforward: this is a connectivity issue, not a library configuration issue.

Firewall and Security Groups

The most frequent cause for error 2002 on remote hosts is an unconfigured firewall or security group. When you set up a new VPS or cloud instance (like AWS EC2, DigitalOcean Droplets, or Linode), the default inbound rules often only allow SSH (port 22). If your application attempts to connect on port 3306 and the server's operating system firewall (such as ufw, firewalld, or iptables) blocks the traffic, the client waits for a response that never arrives.

This wait eventually exceeds the default timeout threshold defined in the MySQL client configuration or the PHP connection settings, resulting in the "Operation timed out" message. Even if you have opened port 3306 in your cloud provider's security group dashboard, the server-side OS firewall might still be blocking it.

Testing Without Terminal Access

You mentioned a lack of terminal access on the remote machine to run SHOW GLOBAL VARIABLES LIKE 'PORT';. While checking the port is useful, the immediate priority is verifying network accessibility. You can perform several checks without SSH:

  1. Port Scanning: Use a tool like telnet, nc (netcat), or online scanners to verify if port 3306 is open from your local network. For example, running nc -zv x.xxx.xxx.xx 3306 should return "succeeded" if the connection is possible.
  2. Cloud Console: Most cloud providers offer a console where you can attach a shell to an instance or run diagnostic commands directly from the web interface.
  3. Database Provider Tools: If you are using a managed service, their dashboard usually has a built-in connection test tool that validates network reachability before attempting authentication.

Resolution and Best Practices

To resolve this, log into your server's control panel or SSH session (if accessible via another method) and ensure port 3306 is explicitly allowed for inbound traffic. On Ubuntu/Debian systems, this involves running sudo ufw allow 3306.

Once the firewall rules are adjusted, retry your PDO connection. The timeout error should cease, and the database will respond with a successful handshake. It is also worth noting that while MySQLi and PDO behave identically regarding network errors, ensuring your PHP version supports the current driver standards is good practice. If you are moving to a newer application stack, consider using Laravel's built-in database management features or following their documentation for secure connection handling if you decide to migrate to a framework-based approach.

The timeout was never a PHP logic error; it was a network gatekeeping issue. Once the path is open, your existing credentials and code structure will function as intended.

Our network