【问题标题】:PHP 7.2.2 + mysql 8.0 PDO gives: authentication method unknown to the client [caching_sha2_password] [duplicate]PHP 7.2.2 + mysql 8.0 PDO 给出:客户端未知的身份验证方法 [caching_sha2_password] [重复]
【发布时间】:2018-08-11 12:30:30
【问题描述】:

我正在使用 php 7.2.2 和 mysql 8.0。

当我尝试使用正确的凭据连接时,我收到此错误:

PDOException::("PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]")

需要帮助来解决问题。

【问题讨论】:

  • MySQL 8.0 不是生产就绪的喷气机。 PHP 开发人员可能没有在较新的 PHP 版本中实现 MySQL 的 8.0 连接协议(auth with caching_sha2_password)。如果您将 MySQL 8.0 配置为在 mysql_native_password 模式下运行,您也许可以解决此问题。
  • 没关系。似乎没有选项 mysql_native_password=1 可配置到 mycnf 中,也可以通过命令行从 mysql 客户端使用它,并且似乎在 pdo 中也可以作为选项使用。只是无法弄清楚如何将事物组合在一起。
  • 在 MySQL 8 中,引入了一个新的身份验证插件:caching_sha2_password。该插件不适用于 PHP,因为 PHP 的 mysqlnd 尚未更新。如果您将 mysql 用户帐户 PHP 连接到数据库转换为旧身份验证,PHP 7.2.4 或更高版本将与旧身份验证插件 (mysql_native_password) 一起使用! ALTER USER 'user_name'@'hostname' IDENTIFIED WITH mysql_native_password BY 'password';在您的 /etc/my.cnf 文件中,您可以添加: default_authentication_plugin = mysql_native_password 但此默认值仅适用于以后创建的所有新帐户。

标签: php mysql pdo


【解决方案1】:
alter user 'username'@'localhost' identified with mysql_native_password by 'password';

正如ailionx 所建议的那样:https://github.com/laradock/laradock/issues/1390

【讨论】:

【解决方案2】:

在某个地方找到了这个,不记得在哪里(或者我会相信),但它对我有用,直到 MySQL 8 变得更好。

在 MySQL shell 中(命令提示符,随便):

CREATE USER username@localhost identified with mysql_native_password by 'password';

其中 username 是用户名,password 是该用户的密码。

然后您可以在 shell - 或 Workbench 中添加权限,因为用户现在存在身份验证类型:标准。

【讨论】:

  • 谢谢!我正在修补 PHP 7.2.5 和 MySQL 8。无法通过工作台界面创建用户进行连接,但运行此查询为我完成了这项工作。
  • 谢谢,它在 MySQL 8.0.13 上对我有用。
  • 这应该是公认的答案。
  • 用 mysqld --default-authentication-plugin=mysql_native_password --console 启动你的 mysql
  • @Reid " PHP 已准备好使用 MySQL 的缓存_sha2_password。"这句话是错误的/误导性的。我建议你从你的答案中删除这句话。事实上,您继续使用 mysql_native_password 插件创建一个新用户,而不是 cache_sha2_password。 PHP 不能与 caching_sha2_password 插件一起使用,但它可以与 mysql_native_password 插件一起使用。参考文献:php.net/manual/en/ref.pdo-mysql.phpdev.mysql.com/doc/refman/8.0/en/…
【解决方案3】:

如果你想使用 MySQL >= 8 并且不关心caching_sha2_password,安装时选择第二个选项即可。

【讨论】:

  • 如果您提供在 Linux 上使用 bash 获得相同结果的说明,我会选择此答案。
  • 经过数小时的尝试,这是唯一对我有用的方法
  • 不卸载 MYSQL 的方法:只需下载 MySql 安装程序,然后执行它。按照说明进行操作,选择“重新配置 MySql Server”并将身份验证方法更改为“使用传统身份验证方法”。 (无需停止mysql服务,简单快捷)
【解决方案4】:

为了使用 mySQL 8(使用新的身份验证方法),您只需升级您的 PHP。

MySQL 8

当运行7.1.16之前的PHP版本,或者7.2.4之前的PHP 7.2时,将MySQL 8 Server的默认密码插件设置为mysql_native_password,否则你会看到类似The server requested authentication method unknown to the client [caching_sha2_password]的错误即使没有使用 caching_sha2_password。

参考:https://www.php.net/manual/en/ref.pdo-mysql.php

【讨论】:

  • MySQL 8 的 caching_sha2_password 身份验证插件目前还不能与 PHP 一起使用。如果您将 PHP 使用的 mysql 用户帐户转换为 OLD 身份验证,PHP 7.2.4 或更高版本将与 MySQL 8 一起使用:ALTER USER 'user_name'@'hostname' IDENTIFIED WITH mysql_native_password BY 'password';或者您可以创建一个引用旧身份验证的新帐户。 [mysqld] default_authentication_plugin = mysql_native_password 指令仅影响新帐户。它不会影响现有的用户帐户。
  • @James PHP PDO 中caching_sha2_password 的状态是否有任何更新?
  • @Meglio MySQL 仍在报告:“PHP:PDO_MySQL 和 ext/mysqli 扩展不支持caching_sha2_password。”点击此链接dev.mysql.com/doc/refman/8.0/en/… 并搜索 PDO。
  • @Meglio 另外,PHP 文档指出 PDO 不支持缓存sha2_password。参考:php.net/manual/en/ref.pdo-mysql.php
猜你喜欢
  • 2019-04-24
  • 2019-06-19
  • 2020-12-03
  • 2019-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
  • 2020-09-03
相关资源
最近更新 更多