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 for mysql 的默认端口是 8889
,但是 php 期望的端口用于 mysql 的是 3306
.所以你需要打开MAMP,进入preferences,将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屋!