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

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

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

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

When you encounter PHP Warning: mysqli_connect(): (HY000/2002): Connection refused, the error message is explicit about the nature of the failure. It is not a programming logic error within your PHP script; it is an infrastructure-level refusal from the MySQL server itself to accept incoming connections. The code snippet provided in the source material demonstrates a standard setup using mysqli_connect with default credentials (root on 127.0.0.1). While the syntax is correct, the underlying service stack required to support that syntax is not functioning as expected.

This specific error code, HY000/2002, maps directly to a TCP connection refusal. In a Linux environment, this usually points to incorrect bind-address settings or firewall rules. However, when running on MAMP (Mac OS X), the root cause is almost exclusively related to how the Apache server and the MySQL daemon interact within the local sandbox.

The most frequent culprit in a MAMP environment is the configuration of the MySQL service itself. By default, many MySQL installations bind only to localhost or specific interfaces that reject connections from certain ports or user contexts. If your script runs via Apache but connects to a port that isn't listening, or if the socket file permissions are misaligned, the connection will be refused immediately.

To resolve this, you must first verify that the MySQL server is actually running. Open the MAMP application and ensure the green indicator light for MySQL is lit. If it is not, start the service. Once running, check the port configuration. In MAMP Pro, you can customize ports, but in the free version, Apache typically listens on 8080 (or 8888 if 80 is occupied), while MySQL listens on 3306. Ensure your connection string in the PHP script matches the active port. If you are using a custom port, update the $port variable:

$servername = "127.0.0.1";
$username = "root";
$password = "root";
$port = 3306; // Or whatever port MAMP is currently using

$conn = mysqli_connect($servername, $username, $password, null, $port);

If the connection still fails despite the service being active and the correct port, investigate the MySQL configuration file (my.cnf or my.ini). Look for a bind-address directive. If it is set to 127.0.0.1, connections from localhost should work, but sometimes socket-based connections are preferred over TCP/IP in local setups. However, since your script uses mysqli_connect with an IP address, you must ensure the server accepts TCP/IP connections.

Another common pitfall involves the user permissions. The error "Connection refused" can occasionally mask permission issues where the server accepts the connection but immediately drops it due to authentication failure, though this usually triggers an "Access denied" error instead. Still, verifying that the root user exists and has privileges to connect via TCP/IP is a necessary step. You can do this by logging into MySQL directly via the MAMP shell or command line:

mysql -u root -p

Once inside, run the following to ensure the user is configured for remote (localhost) access:

SELECT user, host FROM mysql.user;

If root only has privileges for localhost, it should work. If it is restricted further or missing, you may need to adjust the grants.

There is also a subtle interaction between PHP's mysqlnd driver and older MySQL configurations. MAMP often ships with specific versions of PHP and MySQL that must be compatible. If you are running an older version like PHP 5.5 (as mentioned in the source material), ensure that the MySQL client library headers match the server version. Mismatched libraries can sometimes manifest as connection errors even if the network path is open.

Finally, consider the firewall or security groups if you ever move this setup outside of a purely local machine. While less likely on a standalone Mac with MAMP, ensuring no external blocking rules interfere is good practice. For most developers stuck on a "Connection refused" error in MAMP, the solution lies in verifying the service status, checking the my.cnf bind address, and confirming that the port numbers in the code match the active ports in the application preferences.

For those looking to move beyond basic procedural scripts or manage database connections more robustly, frameworks like Laravel provide built-in configuration management for these exact scenarios. Resources such as https://laravelcompany.com offer guidance on setting up secure, scalable database connections that abstract away much of this low-level configuration complexity.

Our network