Home > Database > Mysql Tutorial > How to Fix 'PHP Warning: mysqli_connect(): (HY000/2002): Connection refused' in MAMP?

How to Fix 'PHP Warning: mysqli_connect(): (HY000/2002): Connection refused' in MAMP?

Susan Sarandon
Release: 2024-11-10 18:58:03
Original
197 people have browsed it

How to Fix

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:

  1. 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.

  2. 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).

  3. 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.

  4. 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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template