Troubleshooting the mysqli_real_connect(): (HY000/2002) Connection Refused Error - Tips & Solutions

The mysqli_real_connect(): (HY000/2002) Connection Refused error is a common issue that developers face while working with MySQL and PHP. This error occurs when the PHP script is unable to establish a connection with the MySQL server. In this guide, we will walk you through various steps to troubleshoot and resolve this error.

Table of Contents

  1. Check MySQL Server Status
  2. Verify MySQL Server Configuration
  3. Check PHP Configuration
  4. Inspect Firewall Settings
  5. Allow Remote Connections
  6. FAQs

Check MySQL Server Status

The first step in resolving the error is to ensure that the MySQL server is running. You can check the status of the MySQL server using the following commands:

For Linux

sudo systemctl status mysql

Or

sudo service mysql status

For Windows

Open the "Services" application and look for "MySQL" in the list of services. Check if the service is running.

If the MySQL server is not running, start the server using the following commands:

For Linux

sudo systemctl start mysql

Or

sudo service mysql start

For Windows

Right-click on the "MySQL" service in the "Services" application and select "Start".

Source: MySQL Official Documentation

Verify MySQL Server Configuration

If the MySQL server is running, the next step is to verify the server configuration. The configuration file (my.cnf or my.ini) contains settings that the server uses when it starts. The default location of the configuration file varies depending on the operating system.

  • Linux: /etc/mysql/my.cnf
  • Windows: C:\ProgramData\MySQL\MySQL Server 8.0\my.ini

Open the configuration file and ensure that the following settings are correctly configured:

bind-address: This should be set to "127.0.0.1" for local connections or the IP address of the server for remote connections.

bind-address = 127.0.0.1

port: This should be set to the port number on which the MySQL server is listening for connections.

port = 3306

Source: MySQL Official Documentation

Check PHP Configuration

Ensure that your PHP configuration is set up correctly to connect to the MySQL server. Open your PHP script and check the following settings:

Hostname: Make sure the hostname is set to "localhost" for local connections or the IP address of the server for remote connections.

Port: Ensure that the port number matches the one configured in the MySQL server configuration.

Username and Password: Verify that the username and password are correct and have the necessary privileges to connect to the MySQL server.

Source: PHP Official Documentation

Inspect Firewall Settings

Firewalls can block incoming connections to the MySQL server. Check your firewall settings to ensure that the MySQL port is not being blocked.

For Linux

Use the ufw command to check the firewall settings:

sudo ufw status

If the MySQL port is not allowed, add a rule to allow the port:

sudo ufw allow 3306

For Windows

Check the "Windows Defender Firewall" settings and ensure that the MySQL port is allowed.

Source: Ubuntu Official Documentation

Allow Remote Connections

If you are trying to connect to the MySQL server from a remote location, ensure that the server is configured to allow remote connections. Follow these steps to allow remote connections:

Open the MySQL server configuration file (my.cnf or my.ini) and set the "bind-address" setting to the IP address of the server or "0.0.0.0" to allow connections from any IP address.

bind-address = 0.0.0.0

Restart the MySQL server to apply the changes.

Grant the necessary privileges to the user for remote connections:

GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

Source: MySQL Official Documentation

FAQs

Why am I getting the mysqli_real_connect(): (HY000/2002) Connection Refused error?

This error occurs when the PHP script is unable to establish a connection with the MySQL server. The possible reasons behind this error include:

  1. The MySQL server is not running.
  2. Incorrect MySQL server configuration.
  3. Incorrect PHP configuration.
  4. Firewall blocking the MySQL port.
  5. Remote connections not allowed on the MySQL server.

How do I check if the MySQL server is running?

Use the following commands to check the status of the MySQL server:

  • For Linux: sudo systemctl status mysql or sudo service mysql status
  • For Windows: Open the "Services" application and look for "MySQL" in the list of services.

How do I restart the MySQL server?

Use the following commands to restart the MySQL server:

  • For Linux: sudo systemctl restart mysql or sudo service mysql restart
  • For Windows: Right-click on the "MySQL" service in the "Services" application and select "Restart".

How do I check if the MySQL port is being blocked by a firewall?

  • For Linux: Use the sudo ufw status command to check the firewall settings.
  • For Windows: Check the "Windows Defender Firewall" settings.

How do I enable remote connections on the MySQL server?

Open the MySQL server configuration file (my.cnf or my.ini) and set the "bind-address" setting to the IP address of the server or "0.0.0.0" to allow connections from any IP address.

Restart the MySQL server to apply the changes.

Grant the necessary privileges to the user for remote connections using the following SQL commands:

GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.