PHP警告:mysqli_connect():(HY000/2002):连接被拒绝 [英] PHP Warning: mysqli_connect(): (HY000/2002): Connection refused
问题描述
我正在使用PHP 5.5和MAMP(从此处下载):
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
在MAMP或PHP中是否需要设置一些配置问题?
Is there some configuration issue that I need to set up within MAMP or PHP?
推荐答案
如果其他人遇到此问题,则MAMP上mysql的默认端口为8889
,但是php希望用于mysql的端口为3306
.因此,您需要打开MAMP,转到首选项,然后将MAMP的mysql端口更改为3306
,然后重新启动mysql服务器.现在,应该可以使用host = localhost,user = root,pass = root来成功建立连接.
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.
这篇关于PHP警告:mysqli_connect():(HY000/2002):连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!