Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

mamp

I am using PHP 5.5 and MAMP (downloaded from here):

I have a basic script like this:

<?php
$servername = "127.0.0.1";
$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?

like image 324
johncorser Avatar asked Jan 12 '15 15:01

johncorser


4 Answers

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.

like image 76
johncorser Avatar answered Oct 18 '22 11:10

johncorser


Sometimes you need to include mysql db port id in the server like so.

$serverName = "127.0.0.1:3307";
like image 31
Ronny K Avatar answered Oct 18 '22 09:10

Ronny K


For me to make it work again I just deleted the files

ib_logfile0

and

ib_logfile1

.

from :

/Applications/MAMP/db/mysql56/ib_logfile0 

Mac 10.13.3
MAMP:Version 4.3 (853)

like image 23
FrancisUster Avatar answered Oct 18 '22 10:10

FrancisUster


You have to change the mamp Mysql Database port into 8889.

like image 40
rashedcs Avatar answered Oct 18 '22 09:10

rashedcs