PHP Warning: mysqli_connect (): (HY000 / 2002): connection rejected

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 

Are there any configuration problems that I need to configure in MAMP or PHP?

+17
php mysql mamp
source share
5 answers

In case someone else comes along on this issue, the default port for 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 settings and change the mysql MAMP port to 3306 , and then restart the mysql server. Now the connection should be successful with host = localhost, user = root, pass = root.

+22
source share

Sometimes you need to specify the mysql db port id on the server.

 $serverName = "127.0.0.1:3307"; 
+9
source share

You must change the database port of the Mysql database to 8889.

+1
source share

To make me 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
source share

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

The most recent log data is at the bottom, and the log is organized by date, time, some number and text in brackets, which may contain β€œNote” or β€œError”. One that says β€œError” is probably causing the problem.

For me, my mistake was the tablespace that was causing the problem, so I deleted the database files in the specified location.

Note. The tablespace files for your XAMPP installation may be in a different place, but they were in /opt/lampp/var/mysql for me. I think this is typical of XAMPP in Debian based distributions. In addition, my instructions on what to click on in the control panel to view the log may be slightly different for you, because I am running XAMPP on a Linux distribution based on Ubuntu (Feren OS).

0
source share

All Articles