A Detailed Resolution for "PHP Warning: mysqli_connect(): (HY000/2002): Connection refused"
Encountering the error "PHP Warning: mysqli_connect(): (HY000/2002): Connection refused" while using MySQL in PHP is a common issue that can arise due to a connection failure between PHP and the MySQL server.
If you have set up a MySQL database using MAMP and are using PHP to connect to it, you may face this error. The issue often stems from a mismatch between the port used by MAMP and the default port expected by PHP for MySQL connections.
Solution:
To resolve this issue, follow these steps:
Check MAMP MySQL Port:
Open MAMP and navigate to "Preferences" > "MySQL". Check the "MySQL Port" field to see which port is configured. By default, MAMP uses port 8889 for MySQL.
Configure PHP to Use MAMP MySQL Port:
Locate your PHP configuration file (usually named "php.ini"). Find the line starting with "mysqli.default_port" and change the port number to match the one used by MAMP (e.g., mysqli.default_port=8889).
Restart MAMP MySQL Server:
After making the changes to the PHP configuration file, restart the MAMP MySQL server. This ensures that the new port configuration takes effect.
Try Connecting Again:
Once the MAMP MySQL server has been restarted, try connecting to the database again using your PHP script. You should now be able to connect successfully.
Note: If you are still encountering the error, double-check your database credentials (username, password, and database name) and ensure that the PHP script has the correct permissions to access the database.
The above is the detailed content of How to Fix 'PHP Warning: mysqli_connect(): (HY000/2002): Connection refused' in MAMP?. For more information, please follow other related articles on the PHP Chinese website!