Troubleshooting MySQL Connection Issue: "Connection Refused"
When attempting to establish a MySQL connection using PHP's mysqli_connect function, you may encounter the error message "Connection refused." This typically indicates an issue with the connection parameters or server configuration.
Diagnosing the Problem
To resolve this issue, follow these steps:
Example:
The following code demonstrates a successful connection after adjusting the port:
$servername = "127.0.0.1"; $username = "root"; $password = "root"; // Set MySQL port to 3306 $port = 3306; // Create connection $conn = mysqli_connect($servername, $username, $password, null, $port); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully";
By following these steps, you should be able to establish a successful connection to your MySQL database and resolve the "Connection refused" error.
The above is the detailed content of Why Am I Getting a 'Connection Refused' Error When Connecting to MySQL with PHP?. For more information, please follow other related articles on the PHP Chinese website!