PHP 连接失败:SQLSTATE[HY000] [2002] 连接被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29395452/
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-26 00:23:10  来源:igfitidea点击:

PHP Connection failed: SQLSTATE[HY000] [2002] Connection refused

phpmysqlpdo

提问by Jonck

I am trying to use a PHP connection to connect MySQL Database which is on phpmyadmin. Nothing fancy about the connection just trying to see whether the connection is successful or not. I am using MAMP to host the database, the connection I am trying to use is this:

我正在尝试使用 PHP 连接来连接 phpmyadmin 上的 MySQL 数据库。连接没有什么特别的,只是想看看连接是否成功。我正在使用 MAMP 来托管数据库,我尝试使用的连接是这样的:

<?php
$servername = "127.0.0.1";
$username = "root";
$password = "root";

try {
    $conn = new PDO("mysql:host=$servername;dbname=AppDatabase", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
}
catch(PDOException $e)
{
    echo "Connection failed: " . $e->getMessage();
}
?>

I have been using postman to test to see if the connection is working, but I keep receiving this error message:

我一直在使用邮递员来测试连接是否正常,但我不断收到此错误消息:

Connection failed: SQLSTATE[HY000] [2002] Connection refused

连接失败:SQLSTATE[HY000] [2002] 连接被拒绝

Before I was receiving an error message of:

在我收到以下错误消息之前:

Connection failed: SQLSTATE[HY000] [2002] No such file or directory

连接失败:SQLSTATE[HY000] [2002] 没有那个文件或目录

This was because I had set the servername to localhost, through changing this to the IP address it has given me connection refused and I have no idea what is wrong.

这是因为我已将服务器名设置为 localhost,通过将其更改为 IP 地址,它拒绝了我的连接,我不知道出了什么问题。

Any help regarding this would be appreciated.

对此的任何帮助将不胜感激。

采纳答案by Jonck

I found the reason why the connection was not working, it was because the connection was trying to connect to port 8888, when it needed to connect to port 8889.

我找到了连接不工作的原因,是因为连接试图连接到端口 8888,而它需要连接到端口 8889。

$conn = new PDO("mysql:host=$servername;port=8889;dbname=AppDatabase", $username, $password); 

This fixed the problem, although changing the server name to localhost still gives the error.

这解决了问题,尽管将服务器名称更改为 localhost 仍然会出现错误。

Connection failed: SQLSTATE[HY000] [2002] No such file or directory

连接失败:SQLSTATE[HY000] [2002] 没有那个文件或目录

But it connects successfully when the IP address is entered for the server name.

但是当为服务器名称输入 IP 地址时,它连接成功。

回答by vinod

In my case MySQL sever was not running. I restarted the MySQL server and issue was resolved.

在我的情况下,MySQL 服务器没有运行。我重新启动了 MySQL 服务器并解决了问题。

//on ubuntu server
sudo /etc/init.d/mysql start

To avoid MySQL stop problem, you can use the "initctl" utility in Ubuntu 14.04 LTS Linux to make sure the service restarts in case of a failure or reboot. Please consider talking a snapshot of root volume (with mysql stopped) before performing this operations for data retention purpose[8]. You can use the following commands to manage the mysql service with "initctl" utility with stop and start operations.

为避免 MySQL 停止问题,您可以使用 Ubuntu 14.04 LTS Linux 中的“initctl”实用程序来确保服务在出现故障或重启时重新启动。在执行此操作之前,请考虑讨论根卷的快照(mysql 已停止)以保留数据[8]。您可以使用以下命令通过具有停止和启动操作的“initctl”实用程序来管理 mysql 服务。

$ sudo initctl stop mysql
$ sudo initctl start mysql

To verify the working, you can check the status of the service and get the process id (pid), simulate a failure by killing the "mysql" process and verify its status as running with new process id after sometime (typically within 1 minute) using the following commands.

要验证工作,您可以检查服务的状态并获取进程ID(pid),通过杀死“mysql”进程来模拟失败,并在一段时间后(通常在1分钟内)验证其状态为使用新进程ID运行使用以下命令。

$ sudo initctl status mysql         # get pid
$ sudo kill -9 <pid>                # kill mysql process
$ sudo initctl status mysql         # verify status as running after sometime

回答by Quentin Vaisala

Using MAMP I changed the host=localhostto host=127.0.0.1. But a new issue came "connection refused"

使用 MAMP 我host=localhosthost=127.0.0.1. 但是出现了一个新问题“连接被拒绝”

Solved this by putting 'port' => '8889', in 'Datasources' => [

解决了这个'port' => '8889'问题'Datasources' => [

回答by Mahdiyeh

For me was php version from mac instead of MAMP, PATH variable on .bash_profile was wrong. I just prepend the MAMP PHP bin folder to the $PATH env variable. For me was:

对我来说是来自 mac 而不是 MAMP 的 php 版本,.bash_profile 上的 PATH 变量是错误的。我只是将 MAMP PHP bin 文件夹添加到 $PATH env 变量中。对我来说是:

/Applications/mampstack-7.1.21-0/php/bin
  1. In terminal run vim ~/.bash_profileto open ~/.bash_profile

  2. Type i to be able to edit the file, add the bin directory as PATH variable on the top to the file:

    export PATH="/Applications/mampstack-7.1.21-0/php/bin/:$PATH"

  3. Hit ESC, Type :wq, and hit Enter

  4. In Terminal run source ~/.bash_profile
  5. In Terminal type which php, output should be the path to MAMP PHP install.
  1. 在终端运行vim ~/.bash_profile打开~/.bash_profile

  2. 键入 i 以便能够编辑文件,将 bin 目录作为 PATH 变量添加到文件的顶部:

    导出路径="/Applications/mampstack-7.1.21-0/php/bin/:$PATH"

  3. 点击ESC,输入:wq,然后点击Enter

  4. 在终端运行 source ~/.bash_profile
  5. 在终端类型中which php,输出应该是 MAMP PHP 安装的路径。

回答by Prosanta Chaki

I get the solution by running this command in terminal.

我通过在终端中运行此命令来获得解决方案。

sudo killall mysqld

须藤killall mysqld