【发布时间】:2019-06-30 21:01:06
【问题描述】:
我有一个问题,当我想将我的 PHP 代码连接到我的数据库时,这个错误显示在图片中 但是当我通过终端连接我的数据库时,这是正常的
https://i.imgur.com/cpIzV9E.png
<?php
$host='localhost';
$db='canda';
$user='root';
$pass='123456789';
try{
new PDO("mysql:host=".$host
."; dbname=".$db,
$user, $pass);
}catch (PDOException $e) {
echo $e;
}
PDOException: SQLSTATE[HY000] [2002] /Users/mac/Desktop/test.php:11 中没有这样的文件或目录 堆栈跟踪:#0 /Users/mac/Desktop/test.php(11): PDO ->__construct('mysql:host=loca...', 'root', 'password') #1 {main}
【问题讨论】:
-
Mysql 驱动程序将
localhost替换为本地 unix 套接字的假定路径,这似乎在 Mac 上不存在。改用127.0.0.1,所以它实际上使用了TCP堆栈。 -
当我将 localhost 更改为 127.0.0.1 时出现此错误消息 PDOException: PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in /Users/mac/Desktop/ test.php:11 堆栈跟踪:#0 /Users/mac/Desktop/test.php(11): PDO->__construct('mysql:host=127....', 'root', 'FriZerX11') # 1 {main} Next PDOException: SQLSTATE[HY000] [2054] 服务器在 /Users/mac/Desktop/test.php:11 中请求客户端未知的身份验证方法 堆栈跟踪:#0 /Users/mac/Desktop/test。 php(11): PDO->__construct('mysql:host=127....', 'root', 'FriZerX11') #1 {main}
-
@AflaYoussef 小心在公共论坛上显示您的凭据。您可能需要在与 PDO 连接字符串匹配的 mysql 用户表中创建 'root'@'127.0.0.1'。你应该在这里找到如何做到这一点没问题。另外:当发布任何类似的问题时(尤其是与 Apple 的问题),了解操作系统版本以及您用于安装导致问题的软件的方法会更有用。