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:
- MySQL Service Status: Ensure MySQL service is running
- Firewall Settings: Confirm firewall is not blocking MySQL port
- Port Occupation: Check if port 3306 is being used by other programs
- Permission Configuration: Verify database user permission settings
Troubleshooting Steps
- Check XAMPP control panel to confirm MySQL service status
- Verify port settings in MySQL configuration files
- Test database connection using command line
- Check system firewall and antivirus software settings
- Review MySQL error logs for detailed information
Preventive Measures
To avoid similar issues, it's recommended to:
- Use default port configuration in development environments
- Implement unified configuration management mechanisms
- Establish standardized connection testing procedures
- Record detailed connection log information