SQLSTATE[HY000] [2002] connection refused by target machine? Our SQL Server Support team restores secure, stable database access fast.

SQLSTATE[HY000] [2002] Connection Refused Error Explained and Solved

Database connection errors can break your application without warning, and the SQLSTATE HY000 2002 connection refused error is a common example. It appears when your app cannot reach the database server, even though the machine is accessible. This guide explains what the error means, why it occurs, and how to fix it quickly on both local and server environments.

What is the error

SQLSTATE[HY000] [2002] Connection Refused Error Explained and Solved

The error SQLSTATE HY000 2002 connection refused by target machine appears when an application tries to connect to a database, but the server rejects the request. The system reaches the machine, yet no database service responds on the given port, so the connection stops immediately.

Causes of the error

This error appears when the database connection fails at the network level. The most common reasons include the following.

  • MySQL service is stopped: The database service is not running, so no process listens on the configured port.
  • Wrong hostname or port: The application points to an incorrect host or uses a port where MySQL is not active.
  • Firewall blocking the connection: System or network firewalls block traffic to the MySQL port and reject the request.
  • MySQL is not bound to the correct address: The database listens only on a local socket or a different IP address.
  • Conflicting MySQL configuration files: Multiple my.cnf or my.ini files cause MySQL to start with incorrect settings.
  • Maximum connections reached: The database refuses new connections when the connection limit is exhausted.
  • Low server memory or CPU: Resource pressure forces MySQL to reject incoming requests.
  • Corrupt or failed MySQL startup: MySQL fails to start properly due to errors in logs or data files.

Get database access back fast

Chat animation


Solutions

  • Start or restart the MySQL service
  • On Windows with XAMPP or WAMP, open the control panel and start MySQL.
  • On Linux, run
sudo systemctl start mysql
  • Check whether MySQL is running
  • Use this command to confirm the service status
mysqladmin -u root -p status
  • Verify database host and port in app config
  • Make sure the host and port match the MySQL setup
DB_HOST=127.0.0.1
DB_PORT=3306
  • Match the port used in the code
  • If MySQL runs on a custom port, update the connection
$conn = new PDO("mysql:host=localhost;port=3307;dbname=database", "user", "password");
  • Check the MySQL listening port
  • Open the MySQL config file and confirm the port
port=3306
bind-address=0.0.0.0
  • Restart Apache and MySQL together
    • This fixes partial service startup issues.
  • Allow MySQL port through the firewall
  • On Linux systems using UFW
sudo ufw allow 3306
  • Remove conflicting MySQL config files
    • Search for extra my.ini or my.cnf files outside the main MySQL folder and rename them.
  • Reset corrupted MySQL log files
  • Stop MySQL and delete these files from the data directory
ib_logfile0
ib_logfile1

Then restart MySQL.

  • Clear the app cache if using Laravel
php artisan config:clear
php artisan cache:clear
  • Run database commands inside virtual machines
  • For Vagrant or Homestead, log into the VM first
vagrant ssh

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion 

The SQLSTATE[HY000] [2002] Connection refused by target machine error signals a service or network issue, not a credential problem. Restarting MySQL, fixing host or port settings, or removing blocks usually resolves it. For recurring issues, expert database support helps keep systems stable.