无法在 php 中连接到 mysql 8

mar*_*bin 7 php mysql

我正在尝试使用 php 连接到 mysql 服务器,但出现以下错误

\n\n
Connection failed: The server requested authentication method unknown to the client\n
Run Code Online (Sandbox Code Playgroud)\n\n

mysql 服务器版本是 8.0.12,php 版本是 7.2.9。\n我的代码连接到 mysql 服务器

\n\n
<?php\n$servername = "127.0.0.1";\n$username = "root";\n$password = "mypassword";\n// Create connection\n$conn = new mysqli($servername, $username, $password);\n// Check connection\nif ($conn->connect_error) {\n    die("Connection failed: " . $conn->connect_error);\n} \necho "Connected successfully";\n?>\n
Run Code Online (Sandbox Code Playgroud)\n\n

这个问题之前曾在这里被问过,但该解决方案对我不起作用,所以我再次询问这个问题。\n谢谢

\n\n

编辑:我重新安装了 php,现在它为相同的代码提供了以下内容

\n\n
This page isn\xe2\x80\x99t working\n127.0.0.1 is currently unable to handle this request.\nHTTP ERROR 500\n
Run Code Online (Sandbox Code Playgroud)\n

小智 3

我遇到了类似的问题,发现可以通过 Workbench 运行以下代码来更改 mysql 身份验证

ALTER USER root@localhost IDENTIFIED WITH mysql_native_password BY 'new-password-here'
Run Code Online (Sandbox Code Playgroud)