PHP Warning: mysqli_connect(): (HY000/2002): Connection refused
Asked Answered
L

9

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?

Lalapalooza answered 12/1, 2015 at 15:9 Comment(8)
What if you change server name to 'localhost'? Are your credentials correct? Port?Antennule
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.Lalapalooza
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.Intreat
Ah, MAMP defaults the MySQL port to 8889Lalapalooza
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?Precocity
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 phpmyadminLalapalooza
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.Grief
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.Lalapalooza
L
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.

Lalapalooza answered 12/1, 2015 at 15:22 Comment(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?Ropy
I am debugging this about 3 hours! You saved my day! :)Catton
M
17

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

$serverName = "127.0.0.1:3307";
Musa answered 22/7, 2017 at 5:23 Comment(0)
C
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)

Combatant answered 6/2, 2018 at 9:49 Comment(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..Combatant
This worked for me too, after trying all the other solutions on this page.Thracophrygian
Works every time I've had this issue. Unsure why it keeps on happening. Perhaps by force stopping the program?Conifer
N
1

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

Nineteen answered 19/8, 2017 at 11:57 Comment(0)
C
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).

Confidence answered 27/7, 2019 at 13:8 Comment(0)
M
0

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

enter image description here

Mossy answered 14/2, 2020 at 20:54 Comment(0)
S
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';

Spoondrift answered 12/4, 2021 at 21:10 Comment(1)
This is not the mysql config file. This is the phpmyadmin config file. Completely differentInterplead
R
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.

Rowan answered 30/6, 2022 at 11:33 Comment(0)
J
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);
Justinn answered 17/3 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.