view raw
johncorser johncorser - 9 months ago 206
MySQL Question

PHP Warning: mysqli_connect(): (HY000/2002): Connection refused

I'm surprised that I wasn't able to find a good answer searching through StackOverflow for this already.

I am using PHP5.5 and MAMP (downloaded from here).

I have a basic script like this:

$servername = "";
$username = "root";
$password = "root";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
echo "Connected successfully";

and when I run this script I get this error:

PHP Warning: mysqli_connect(): (HY000/2002): Connection refused in /Applications/MAMP/htdocs/test/test.php on line 7

Is there some configuration issue that I need to set up within MAMP or PHP?


In case anyone else comes by this issue, the default port on MAMP for mysql is 8889, but the port that php expects to use for mysql is 3306. So you need to open MAMP, go to preferences, and change the MAMP mysql port to 3306, then restart the mysql server. Now the connection should be successful with host=localhost, user=root, pass=root.