我花了几天的时间才想办法解决这个问题... 我在运行Mojave(10.14.3)的Mac OS上设置了Apache Web服务器(版本2.4.34),并安装了当前版本的MySQL Community Server(8.0.15)。 正如我所能确定的(从命令行),两者都运行良好。 我已经安装了PHP 7.1.23。 当我使用以下代码创建.php文件并将其放在我的Sites目录中,然后在Safari中转到http://localhost/~dave/hello.php时,出现此错误::
"Warning: mysqli::__construct(): (HY000/2002): No such file or directory in /Users/dave/Sites/hello.php on line 7 Connection failed: No such file or directory".
hello.php中的代码:
<?php
$servername = "localhost";
$username = "dave";
$password = "*****";
//Create connection
$conn = new mysqli($servername, $username, $password);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
我已经尝试了所有我能想到的一切,包括其他两篇类似的文章中的建议(这一篇:我的数据库用户存在,但是我仍然得到(HY000 / 2002):没有这样的文件或目录,而这一篇:PHP警告: mysqli :: construct():(HY000 / 2002):没有这样的文件或目录(Debian GNU / Linux 9),并且我无法使用php连接到MySQl。 特别是,我检查了php info页面,并给出了mysqli的套接字为 /tmp/mysql.sock
。 我尝试通过添加每个套接字(代替空值)来更改php.ini.default文件,但两个都不起作用(仍然产生相同的错误)。 我以为2002 MySQL套接字错误可能是问题所在,所以我做了一个符号链接is:
sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
但是这样做只会产生一个新的错误:
Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in /Users/dave/Sites/hello.php on line 7
Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in /Users/dave/Sites/hello.php on line 7 Connection failed: The server requested authentication method unknown to the client
所以我删除了符号链接...并返回到原始错误。 我还尝试使用IP地址127.0.0.1以及其他问题线程建议的“ localhost:3306”替换“ localhost”。 这里也没有任何作用。 请帮助我,我对这种计算机/网页设计的使用是陌生的,我没有明显的选择(谷歌/可理解)。s.