25

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?

Script47
  • 14,230
  • 4
  • 45
  • 66
johncorser
  • 9,262
  • 17
  • 57
  • 102
  • What if you change server name to 'localhost'? Are your credentials correct? Port? – Luc Hendriks Jan 12 '15 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 Jan 12 '15 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 Jan 12 '15 at 15:12
  • Ah, MAMP defaults the MySQL port to 8889 – johncorser Jan 12 '15 at 15:14
  • Can you guys tell me, why do you says to change the host to localhost? Is it some mac thing? As I know, the `localhost` is only a name, what should be resolved. localhost could be any IP address. Isn't it? – vaso123 Jan 12 '15 at 15:15
  • Hm, changed the MySQL port on MAMP to 3306, still getting refused connection with both password and hostname combinations. It is definitely running because I can use it via phpmyadmin – johncorser Jan 12 '15 at 15:16
  • 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. – Funk Forty Niner Jan 12 '15 at 15:16
  • How do I restart all services? Just restart the computer? The only change I made in php.ini was to the timezone, which worked. Also, I noticed that when I use `localhost` instead of `127.0.0.1` I get a different error: `No such file or directory`. This seems like a step backward. – johncorser Jan 12 '15 at 15:19

8 Answers8

32

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.

johncorser
  • 9,262
  • 17
  • 57
  • 102
  • 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 Nov 17 '15 at 13:58
  • I am debugging this about 3 hours! You saved my day! :) – szatti1489 Aug 17 '18 at 12:13
17

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

$serverName = "127.0.0.1:3307";
Ronny K
  • 3,641
  • 4
  • 33
  • 43
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)

Pravin Vavadiya
  • 3,195
  • 1
  • 17
  • 34
  • 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.. – FrancisUster Feb 06 '18 at 10:00
  • This worked for me too, after trying all the other solutions on this page. – JonJ Feb 10 '20 at 16:02
  • Works every time I've had this issue. Unsure why it keeps on happening. Perhaps by force stopping the program? – 6754534367 Sep 16 '20 at 10:30
1

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

rashedcs
  • 3,588
  • 2
  • 39
  • 40
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).

StealthDroid
  • 365
  • 1
  • 4
  • 14
0

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

enter image description here

gadolf
  • 1,035
  • 11
  • 19
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';

kbhatta
  • 457
  • 3
  • 12
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.

asghar
  • 437
  • 5
  • 5