PHP 7.2.2连接MySQL 8.0 PDO报错:authentication method unknown to the client
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, andlocalhostwith 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_passwordis 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_passwordsupport).
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

