[英]MySQL Error mysqli::__construct(): (HY000/2002): Connection refused
I'm trying to run a PHP Script that contains a MySQL connection, when I run the command I get the following error in the terminal:我正在尝试运行包含 MySQL 连接的 PHP 脚本,当我运行该命令时,在终端中出现以下错误:
'Warning: mysqli::__construct(): (HY000/2002): Connection refused'
Even with PDO, I get the error:即使使用 PDO,我也会收到错误消息:
'Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in ...'
I'm running the script on Mac OS X, and I have MAMP as APACHE/MySQL local server.我在 Mac OS X 上运行脚本,我有 MAMP 作为 APACHE/MySQL 本地服务器。
The command that I use for running the script is:我用于运行脚本的命令是:
'php main.php' 'php main.php'
and the content of the 'main.php' file with PDO is:带有 PDO 的“main.php”文件的内容是:
$db = new PDO('mysql:host=localhost;dbname=test',root,root);
with mysqli connector:使用 mysqli 连接器:
$db = new mysqli("localhost", "test", "root", "root");
I tried with '127.0.0.1' instead of 'localhost' and I get the same errors in both cases.我尝试使用 '127.0.0.1' 而不是 'localhost',在这两种情况下我都遇到了相同的错误。
First of all, using localhost
as a domain name isnot supported in PHP .首先,PHP 不支持使用
localhost
作为域名。 It will not be resolved as 127.0.0.1
.它不会被解析为
127.0.0.1
。
Second, even using 127.0.0.1
might not work in servers with multiple interfaces or with special routing rules.其次,即使使用
127.0.0.1
也可能不适用于具有多个接口或具有特殊路由规则的服务器。 Run mysqlcheck --help
and take the IP address shown at the bottom in the "host" field.运行
mysqlcheck --help
并获取显示在“主机”字段底部的 IP 地址。 For example:例如:
port 3306
host 192.168.1.23
Use the IP and port that MySQL recognizes:使用 MySQL 识别的 IP 和端口:
$db = new mysqli("192.168.1.23:3306", "test", "root", "root");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.