PHP 警告:mysqli_connect():(HY000/2002):连接被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27905049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:36:39  来源:igfitidea点击:

PHP Warning: mysqli_connect(): (HY000/2002): Connection refused

phpmysqlmamp

提问by johncorser

I am using PHP 5.5 and MAMP (downloaded from here):

我正在使用 PHP 5.5 和 MAMP(从这里下载):

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?

我需要在 MAMP 或 PHP 中设置一些配置问题吗?

回答by johncorser

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.

如果其他人遇到此问题,mysql 的 MAMP 上的默认端口是8889,但 php 希望用于 mysql 的端口是3306. 所以需要打开MAMP,进入preferences,将MAMP mysql端口改为3306,然后重启mysql服务器。现在连接应该成功,host=localhost, user=root, pass=root。

回答by Ronny Kibet

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

有时您需要像这样在服务器中包含 mysql db 端口 id。

$serverName = "127.0.0.1:3307";

回答by FrancisUster

For me to make it work again I just deleted the files

为了让它再次工作,我只是删除了文件

ib_logfile0

ib_logfile0

and

ib_logfile1

ib_logfile1

.

.

from :

从 :

/Applications/MAMP/db/mysql56/ib_logfile0 

Mac 10.13.3
MAMP:Version 4.3 (853)

Mac 10.13.3
MAMP:版本 4.3 (853)

回答by rashedcs

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

您必须将 mamp Mysql 数据库端口更改为 8889。

回答by StealthDroid

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."

就我而言,我使用的是 XAMPP,并且有一个日志告诉我错误。要找到它,请转到 XAMPP 控制面板,然后单击 MySQL 的“配置”,然后单击“打开日志”。

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/mysqlfor 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).

注意:用于安装 XAMPP 的表空间文件可能位于不同的位置,但它们是/opt/lampp/var/mysql我的。我认为这是基于 Debian 的发行版上 XAMPP 的典型特征。另外,我关于在控制面板中单击什么来查看日志的说明对您来说可能有点不同,因为我在基于 Ubuntu 的 Linux 发行版(Feren OS)上运行 XAMPP。

回答by gadolf

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

WAMP 中,右键单击 WAMP 托盘图标,然后将端口从 3308 更改为3306,如下所示:

enter image description here

在此处输入图片说明