Resolving mysqli_connect Error: Target Machine Actively Refused Connection

Nov 22, 2025 · Programming · 8 views · 7.8

Keywords: mysqli_connect | port_configuration | database_connection_error

Abstract: This technical paper provides an in-depth analysis of the (HY000/2002) connection error in PHP mysqli_connect function, focusing on port configuration errors, firewall settings, and MySQL service status. Through detailed code examples and system configuration analysis, it offers comprehensive troubleshooting solutions to help developers quickly identify and resolve database connection issues.

Problem Background and Analysis

During PHP development, when using the mysqli_connect function to connect to MySQL databases, developers often encounter the error message "No connection could be made because the target machine actively refused it". This error code HY000/2002 indicates that the client can locate the target machine but the connection request is explicitly rejected.

Core Issue Diagnosis

Through analysis of the user's provided code and configuration information, the main issue centers on port configuration errors. The user's code specifies $dbhost = 'localhost:3360', while the XAMPP control panel shows the MySQL service actually runs on port 3306. This port mismatch is the direct cause of the connection refusal.

Correct Connection Configuration

MySQL uses port 3306 by default. When using the default port, there's no need to explicitly specify the port number in the hostname. The correct configuration approach is as follows:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'test_db13';

$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $db);

If you need to specify a non-default port, you should use the fifth parameter of the mysqli_connect function:

$conn = mysqli_connect('localhost', 'root', '', 'test_db13', 3306);

Comprehensive Error Handling Mechanism

To provide better debugging information, it's recommended to implement a comprehensive error handling mechanism:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'test_db13';

// Establish database connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $db);

// Check if connection is successful
if (!$conn) {
    die('Connection failed: ' . mysqli_connect_error());
}

echo 'Database connection successful';

// Execute SQL query
$sql = 'SELECT 1';
$result = mysqli_query($conn, $sql);

if (!$result) {
    die('Query failed: ' . mysqli_error($conn));
}

echo 'Query executed successfully';

// Close connection
mysqli_close($conn);
?>

System Environment Check

In addition to port configuration, the following system environment factors need to be checked:

Troubleshooting Steps

  1. Check XAMPP control panel to confirm MySQL service status
  2. Verify port settings in MySQL configuration files
  3. Test database connection using command line
  4. Check system firewall and antivirus software settings
  5. Review MySQL error logs for detailed information

Preventive Measures

To avoid similar issues, it's recommended to:

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.