错误2002连接拒绝在PHP上连接到在MAMP上运行的MySQL [英] Error 2002 Connection refused on PHP connecting to MySQL running on MAMP

查看:72
本文介绍了错误2002连接拒绝在PHP上连接到在MAMP上运行的MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在安装ddev,Docker和MAMP之后,我刚刚在本地Mac上安装了Laravel.我使用了 https://ddev.readthedocs.io/en/stable/users/cli-usage/#laravel-quickstart 指南,一切看起来都还不错.但是,当我尝试运行从Internet下载的示例代码时,我开始收到错误消息

I just installed Laravel on my local Mac after installing ddev, Docker and MAMP. I used https://ddev.readthedocs.io/en/stable/users/cli-usage/#laravel-quickstart guide and everything looked ok. However, when I tried to run a sample code downloaded from the Internet, I started getting the error message

Illuminate \ Database \ QueryException(2002)

SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from `users` where `email` = frankie@example.com limit 1)

一开始,我认为这与内部Lavavel错误有关,因为我看到电子邮件地址中没有引号.但是,我尝试使用从MAMP中获取的此PHP脚本连接到MySQL(通过网络连接到MySQL 部分)

In the beginning, I thought it was something related to an internal Lavavel error as I saw the email address did not have any quotes. However, I tried to connect to MySQL from this PHP script I took from MAMP (section Connect via Network to MySQL)

<?php
  $db_host = 'localhost';
  $db_user = 'root';
  $db_password = 'root';
  $db_db = 'information_schema';
  $db_port = 8889;

  $mysqli = new mysqli(
    $db_host,
    $db_user,
    $db_password,
    $db_db
  );
    
  if ($mysqli->connect_error) {
    echo 'Errno: '.$mysqli->connect_errno;
    echo '<br>';
    echo 'Error: '.$mysqli->connect_error;
    exit();
  }

  echo 'Success: A proper connection to MySQL was made.';
  echo '<br>';
  echo 'Host information: '.$mysqli->host_info;
  echo '<br>';
  echo 'Protocol version: '.$mysqli->protocol_version;

  $mysqli->close();
?>

那是我意识到无法从MAMP(MySQL 5.7.32)上运行的任何PHP脚本/代码连接到MySQL的时候.这是我得到的错误:

And it was when I realised I cannot connect to MySQL from any PHP script/code run on MAMP (MySQL 5.7.32 ). This is the error I got:

Errno: 2002
Error: No such file or directory

然后在这里阅读其他类似的问题后,我将 localhost 更改为 127.0.0.1 ,并收到了此新消息

Then after reading other similar questions on here, I changed localhost for 127.0.0.1 and got this new message

Errno: 2002
Error: Connection refused

注释:MySQL正在监听端口 8889

Notes: MySQL is listening on port 8889

文件/Applications/MAMP/tmp/mysql/mysql.sock 存在

用户名和密码正确

数据库也存在

我的第一个想法是关于电子邮件地址缺少引号的原因,这就是为什么我在这里

My first thought was about lack of quotation for the email address, that's why I wrote my initial question here SQL/Laravel is not quoting text fields when building a query. However, the real issue is about trying to connect to MySQL server

推荐答案

电子邮件必须放在撇号中,请参见

email has to be in apostrophe see When to use single quotes, double quotes, and backticks in MySQL

喜欢

select * from `users` where `email` = 'frankie@example.com' limit 1

我也希望您其余的代码使用带有参数的准备好的语句来防止 sql注入

also i hope you rest of the code use prepared statements with parameters to prevent sql injection

这篇关于错误2002连接拒绝在PHP上连接到在MAMP上运行的MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆