22
votes

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?

7
What if you change server name to 'localhost'? Are your credentials correct? Port?Luc Hendriks
I tried all combinations of localhost, 127.0.0.1 and password of root and empty string. The credentials and the port should be whatever defaults came with MAMP when I downloaded it earlier today.johncorser
connection refused = mysql isn't listening on port 3306, or isn't running at all, or isn't set up to allow TCP connections, or there's a firewall actively blocking port 3306.Marc B
Ah, MAMP defaults the MySQL port to 8889johncorser
If you made any changes to .ini or other system files, you'll need to restart all services in order for the changes to take effect.Funk Forty Niner

7 Answers

31
votes

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.

16
votes

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

$serverName = "127.0.0.1:3307";
4
votes

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)

1
votes

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

0
votes

In my case I was using XAMPP, and there was a log that told me the error. To find it, go to the XAMPP control panel, and click "Configure" for MySQL, then click on "Open Log."

The most current data of the log is at the bottom, and the log is organized by date, time, some number, and text in brackets that may say "Note" or "Error." One that says "Error" is likely causing the issue.

For me, my error was a tablespace that was causing an issue, so I deleted the database files at the given location.

Note: The tablespace files for your installation of XAMPP may be at a different location, but they were in /opt/lampp/var/mysql for me. I think that's typical of XAMPP on Debian-based distributions. Also, my instructions on what to click in the control panel to see the log may be a bit different for you because I'm running XAMPP on an Ubuntu-based distribution of Linux (Feren OS).

0
votes

In WAMP, right click on WAMP tray icon then change the port from 3308 to 3306 like this:

enter image description here

0
votes

MySQL worked perfectly until I updated to macOS Big Sur. After I updated to the latest version macOS, MySQL was not working as expected.

I added a port to the MySQL config file(I'm using AMPPS, but does not matter, find your MySQL config file), and it started to work.

/Applications/Ampps/phpMyAdmin/config.inc.php

from

$cfg['Servers'][$i]['port'] = '';

to

$cfg['Servers'][$i]['port'] = '3306';