Can't Connect to MySQL Server on IPAddress (10061)
The error message Connect Error (2003) ... (10061) is one of the most common roadblocks when deploying PHP applications to remote servers. It appears deceptively simple, suggesting a network issue or a firewall block, but the underlying cause is almost always misconfiguration on the server side rather than a flaw in your connection logic.
In this scenario, you are attempting to connect using $mysqli=new mysqli($host,$username,$pass,$db). The code itself is syntactically correct for initiating a connection. However, the specific error code 10061 indicates that the client sent a TCP SYN packet to the MySQL port (usually 3306), and the server responded with a RST (reset). This happens because the MySQL server process on the remote host is actively listening but has explicitly rejected the incoming connection attempt.
When troubleshooting this, it is crucial to distinguish between two distinct layers of failure: network reachability and authentication/authorization. Your initial error suggests the network path exists, but the service refused entry. Once you modified your my.cnf file to allow remote connections and restarted the MySQL service, the nature of the error shifted to Access denied for user 'dbuser'@'mycurrenthostname'.
This transition reveals a fundamental misunderstanding of how MySQL manages user accounts compared to traditional operating system users. In many database systems, a username like dbuser is unique globally. In MySQL, however, the identity is defined by the combination of the username and the host from which it connects. The account dbuser@localhost is completely different from dbuser@'%'.
When you executed GRANT SELECT, INSERT, UPDATE ON mydbname TO 'dbuser'@'%', you created a specific privilege mapping for connections originating from any IP address. However, the error message explicitly states: Access denied for user 'dbuser'@'mycurrenthostname'. This indicates that your PHP application is connecting from a specific hostname or IP (e.g., 192.168.1.50), but MySQL is trying to validate that connection against the account dbuser@'192.168.1.50', which likely does not exist in your user table.
To resolve this, you must ensure the grant covers the specific identity being used by your PHP script. You have three primary options depending on your security requirements:
Grant for Specific Host: Identify the exact IP address or hostname of your development server and grant privileges specifically to
dbuser@'192.168.1.50'. This is the most secure approach as it prevents the user from connecting from arbitrary networks.GRANT SELECT, INSERT, UPDATE ON mydbname TO 'dbuser'@'192.168.1.50'; FLUSH PRIVILEGES;Use
localhostfor Local Connections: If your PHP application runs on the same machine where MySQL is installed (or if you are connecting via a Unix socket), you should connect using127.0.0.1or simply omit the host in the connection string to default tolocalhost. The accountdbuser@localhostusually has broader privileges than remote accounts.// Connect without specifying host to use localhost/unix socket $mysqli = new mysqli($username, $pass, $db);Verify Account Existence: Run
SELECT User, Host FROM mysql.user;from the command line or via a privileged tool likemysql -u root -p. Ensure that an entry exists for your application's source IP. If it doesn't, the previous GRANT command failed silently because you were creating a grant for a host that didn't match the incoming connection source.
A common pitfall involves the bind-address directive in your my.cnf file. If you set bind-address = 127.0.0.1, the MySQL server will only accept connections on the loopback interface, effectively blocking all remote IP attempts regardless of your firewall rules. This is likely what caused the initial 10061 error; the server was running but refused connections from external IPs because it wasn't listening on them.
Finally, ensure that your PHP code closes the connection properly after use. While the mysqli_connect_error() check handles failures, remember that $mysqli objects should be closed using $mysqli->close() to free up resources immediately, especially in long-running scripts or when managing multiple concurrent connections.
For production environments requiring robust database management and security best practices, consider leveraging modern PHP frameworks like Laravel. The official resource at https://laravelcompany.com provides comprehensive documentation on configuring secure database connections within their ecosystem, including how to handle environment-specific configuration variables to avoid hardcoding credentials directly in your source code.