Problem:
A developer encountered the error "Connection failed: SQLSTATE[HY000] [2002] Connection refused" while attempting to establish a PHP connection to a MySQL database on phpMyAdmin.
Troubleshooting:
The developer initially modified the servername variable from "localhost" to "127.0.0.1", resolving the error "Connection failed: SQLSTATE[HY000] [2002] No such file or directory." However, they still encountered the connection refused error.
Solution:
Upon further investigation, the developer discovered that the connection was attempting to connect to port 8888, while it should have been connecting to port 8889. Modifying the code to use port 8889 fixed the issue:
$conn = new PDO("mysql:host=$servername;port=8889;dbname=AppDatabase", $username, $password);
Additional Notes:
While using the IP address "127.0.0.1" for the servername resolved the connection refused error, the error "Connection failed: SQLSTATE[HY000] [2002] No such file or directory" was still encountered when using "localhost" as the servername. This suggests that the database configuration may require specific IP-based connections.
The above is the detailed content of Why is my PHP MySQL Connection Refused Despite Correct Credentials?. For more information, please follow other related articles on the PHP Chinese website!