Home  >  Article  >  Database  >  What Causes the 'mysqli_connect(): Connection Refused' Error in PHP and How to Fix It?

What Causes the 'mysqli_connect(): Connection Refused' Error in PHP and How to Fix It?

Susan Sarandon
Susan SarandonOriginal
2024-11-08 06:04:01196browse

What Causes the

PHP Warning: mysqli_connect(): Connection Refused (HY000/2002)

This error occurs when PHP fails to establish a connection to the MySQL database. The cause is indicated by the error code "HY000/2002," which typically signifies that the connection was refused due to incorrect server configuration.

To resolve this issue, verify the following settings within MAMP:

  • MySQL Server

    Ensure that the MySQL server is running. In MAMP, this can be done by selecting the "Start MySQL Server" option from the menu bar.

  • MySQL Port

    The default MySQL port used by MAMP is 8889. However, PHP typically uses port 3306 by default. To resolve this conflict:

    1. Open MAMP's preferences by selecting "Preferences" from the menu bar.
    2. In the "Ports" tab, change the "MySQL" port to 3306.
    3. Click "OK" to save the changes.
  • Database Credentials

    Verify that the host ("127.0.0.1"), username ("root"), and password ("root") specified in the PHP script are correct.

After making these adjustments, restart the MySQL server in MAMP. Reconnecting should now be successful with the host, user, and password as specified in the PHP script.

The above is the detailed content of What Causes the 'mysqli_connect(): Connection Refused' Error in PHP and How to Fix It?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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