升級至 MySQL 5.0.1 或 8.0 後,舊版 PHP/MySQL 擴展(php_mysql.dll)連線報錯:
Client does not support authentication protocol requested by server
MySQL 4.1 引入 41 位元組 SHA1 雜湊(caching_sha2_password 為 8.0 預設),舊驅動僅支援 16 位元組 mysql_old_password。
mysql -u root -p
-- MySQL 5.x / 8.0 通用
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpass';
-- 或指定舊式(不推薦長期使用)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_old_password BY 'newpass';
mysql> FLUSH PRIVILEGES;
說明:mysql_native_password 為 41 位元組 SHA1,相容 PHP 5.3+ mysqli、PDO 及多數現代驅動;mysql_old_password 為 16 位元組,僅相容極舊驅動。
| 語言/平台 | 舊驅動 | 新驅動(支援 caching_sha2_password) |
|---|---|---|
| PHP | ext/mysql (php_mysql.dll) | mysqli、PDO_MySQL (PHP 5.3+) |
| Java | Connector/J 3.x/5.0 | Connector/J 8.0+ |
| Python | MySQLdb | mysqlclient / PyMySQL |
| Node.js | mysql (舊版) | mysql2 / @mysql/xdevapi |
| .NET | MySQL Connector/NET 6.x | MySQL Connector/NET 8.0+ |
[mysqld]
default-authentication-plugin=mysql_native_password
# 或針對特定帳號
ALTER USER 'app_user'@'%' IDENTIFIED WITH mysql_native_password BY 'pass';
MySQL 8.0 預設 caching_sha2_password,設定上述參數可全域回退至 mysql_native_password,賜予遷移緩衝期。
mysql -u root -p -h 127.0.0.1;