When attempting to establish a PHP connection to a MySQL database hosted on phpMyAdmin, you may encounter the following error: "Connection failed: SQLSTATE[HY000] [2002] Connection refused." This issue can arise due to an incorrect connection configuration.
In the code provided:
$servername = "127.0.0.1"; $username = "root"; $password = "root";
Port Configuration:
Initially, the error "No such file or directory" was encountered when using "localhost" as the $servername. Changing it to the IP address "127.0.0.1" resolved this issue. However, the connection was still being refused.
The culprit was an incorrect port configuration. MAMP typically runs MySQL on port 8889. Modifying the connection string to the following resolved the "Connection refused" error:
$conn = new PDO("mysql:host=$servername;port=8889;dbname=AppDatabase", $username, $password);
Despite this fix, using "localhost" for $servername still resulted in the same error. This indicates that there may be additional configuration issues or restrictions within your MAMP setup.
Additional Tips:
The above is the detailed content of Why Is My PHP MySQL Connection Refusing Connections? (SQLSTATE[HY000] [2002]). For more information, please follow other related articles on the PHP Chinese website!