You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP 7.2.2连接MySQL 8.0 PDO报错:authentication method unknown to the client

Fixing the "caching_sha2_password" Authentication Error Between PHP 7.2.2 and MySQL 8.0

Hey there, let’s work through this authentication issue you’re facing. The root cause is straightforward: MySQL 8.0 defaults to using the caching_sha2_password authentication plugin, but PHP 7.2.2’s PDO MySQL driver (mysqlnd) doesn’t support this plugin natively out of the box. Here are two reliable fixes to get your connection up and running:

Option 1: Switch Your MySQL User to Use mysql_native_password

This is the quickest, most practical fix for older PHP versions like 7.2.2.

  • Log into your MySQL server with an admin account (like root):
    mysql -u root -p
    
  • Update your user’s authentication method (replace your_username, your_password, and localhost with your actual details—adjust the host if your app connects remotely):
    ALTER USER 'your_username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
    
  • Flush privileges to apply changes right away:
    FLUSH PRIVILEGES;
    
  • Test your PDO connection again—this should resolve the error, as mysql_native_password is fully supported by PHP 7.2.2’s driver.

Option 2: Upgrade PHP’s MySQLND Driver (Advanced)

If you want to keep using caching_sha2_password, you can try upgrading the mysqlnd extension PHP relies on. Note that this is more involved for PHP 7.2.2 (which is end-of-life):

  • For Linux systems, check if your package manager offers an updated mysqlnd package compatible with PHP 7.2.
  • For Windows, you may need to download a newer PHP build that includes an updated mysqlnd driver (though official 7.2.x builds may not have full caching_sha2_password support).

This option is less recommended unless you absolutely need to retain MySQL’s default authentication plugin.

Quick Check: Verify Your PDO Connection String

Make sure your DSN doesn’t have conflicting settings. A basic working connection should look like this:

$dsn = 'mysql:host=localhost;dbname=your_database;charset=utf8mb4';
$pdo = new PDO($dsn, 'your_username', 'your_password', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

Double-check that you aren’t setting any authentication flags that might interfere.

内容的提问来源于stack exchange,提问作者Michele Carino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:05:27