简体   繁体   English

MySQL 错误 mysqli::__construct(): (HY000/2002): 连接被拒绝

[英]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.

相关问题 警告:mysqli::__construct(): (HY000/2002): 连接被拒绝 - Warning: mysqli::__construct(): (HY000/2002): Connection refused in 警告:mysqli::__construct(): (HY000/2002): 连接被拒绝 - Warning: mysqli::__construct(): (HY000/2002): Connection refused 警告:mysqli_connect():( HY000 / 2002):拒绝连接 - Warning: mysqli_connect(): (HY000/2002): Connection refused in Docker MySQL mysqli::real_connect():(HY000/2002): 连接被拒绝 - Docker MySQL mysqli::real_connect():(HY000/2002): Connection refused 数据库错误 mysqli::real_connect(): (HY000/2002): Connection denied codeingiter - Datebase error mysqli::real_connect(): (HY000/2002): Connection refused codeingiter 实时服务器上的 Codeigniter 错误消息:mysqli::real_connect(): (HY000/2002): 连接被拒绝 - Codeigniter Error Message on Live server : mysqli::real_connect(): (HY000/2002): Connection refused Laravel MySQL SQLSTATE [HY000] [2002]连接被拒绝 - Laravel MySQL SQLSTATE[HY000] [2002] Connection refused 部署错误-SQLSTATE [HY000] [2002]连接被拒绝 - Deployment error - SQLSTATE[HY000] [2002] Connection refused 遇到 PHP 错误(HY000/2002):连接被拒绝 - A PHP Error was encountered (HY000/2002): Connection refused 在 docker 中获取与 MySQL 服务器相关的 php 错误:SQLSTATE[HY000] [2002] 连接被拒绝 - Getting php error related to MySQL server in docker: SQLSTATE[HY000] [2002] Connection refused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM