27

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?

8
  • What if you change server name to 'localhost'? Are your credentials correct? Port? Commented Jan 12, 2015 at 15:11
  • 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
    Commented Jan 12, 2015 at 15:12
  • 3
    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
    Commented Jan 12, 2015 at 15:12
  • Ah, MAMP defaults the MySQL port to 8889
    – johncorser
    Commented Jan 12, 2015 at 15:14
  • 1
    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. Commented Jan 12, 2015 at 15:16

9 Answers 9

33

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.

2
  • Not using MAMP but when I change this in the 'dsn:' string in this CodeIgniter application at config/database.php to say port=3306 it works perfectly fine. What's telling it to look for the MAMP port?
    – IIllIIll
    Commented Nov 17, 2015 at 13:58
  • I am debugging this about 3 hours! You saved my day! :)
    – szatti1489
    Commented Aug 17, 2018 at 12:13
17

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

$serverName = "127.0.0.1:3307";
0
4

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)

3
  • I tried everything and it's the only thing that got my Sql Server back ... I know it's not the best thing to do but it worked for me.. Commented Feb 6, 2018 at 10:00
  • This worked for me too, after trying all the other solutions on this page.
    – JonJ
    Commented Feb 10, 2020 at 16:02
  • Works every time I've had this issue. Unsure why it keeps on happening. Perhaps by force stopping the program? Commented Sep 16, 2020 at 10:30
1

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

0

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

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

enter image description here

0

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';

1
  • This is not the mysql config file. This is the phpmyadmin config file. Completely different
    – Silloky
    Commented Jul 14, 2023 at 7:13
0

If you use docker, you may find it useful

I connected mysql container two separate network:

  1. in nodejs app network work fine

  2. but in phpproject network I had this error

so I got inspect from phpproject network by this command in PowherShell:

docker network inspect phpproject

and use IPv4Address from mysql container instead of localhost or 127.0.0.1 address like this:

$servername = "p:172.22.0.3:3306";
$username = "root";
$password = "secret";

$conn = mysqli_connect($servername, $username, $password,'database_name');

enter image description here and solved problem.

I hope help for you.

0

If you're using Docker, both your MySQL and PHP containers have to be on the same network in order to communicate.

Also, if you ran your containers with docker-compose I found that using the service name of mysql (you can find it in your docker-compose.yml file) as the server name was the way to connect in PHP (but not localhost):

$server = "mysql" // "service name"
$username = "root";
$password = "password";
$database = "testdb";

$conn = new mysqli($server, $username, $password, $database);

Not the answer you're looking for? Browse other questions tagged or ask your own question.