首頁  >  問答  >  主體

php mysqli_connect:客戶端未知的身份驗證方法

我正在使用 php mysqli_connect 登入 MySQL 資料庫(全部在本機上)

<?php
//DEFINE ('DB_USER', 'user2');
//DEFINE ('DB_PASSWORD', 'pass2');
DEFINE ('DB_USER', 'user1');
DEFINE ('DB_PASSWORD', 'pass1');
DEFINE ('DB_HOST', '127.0.0.1');
DEFINE ('DB_NAME', 'dbname');

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if(!$dbc){
    die('error connecting to database');    
}
?>

這是 mysql.user 表:

MySQL 伺服器 ini 檔案:

[mysqld]
# The default authentication plugin to be used when connecting to the server
default_authentication_plugin=caching_sha2_password
#default_authentication_plugin=mysql_native_password

在MySQL Server ini檔案中使用caching_sha2_password,根本不可能用user1或user2登入;

錯誤:mysqli_connect():伺服器請求客戶端 [caching_sha2_password] 未知的身份驗證方法...

在MySQL Server ini檔案中使用mysql_native_password,可以用user1登錄,但用user2登錄,同樣的錯誤;


如何在 mySql 伺服器上使用 caching_sha2_password 登入?

P粉170858678P粉170858678341 天前578

全部回覆(2)我來回復

  • P粉727416639

    P粉7274166392023-10-14 16:49:08

    我透過 SQL 指令解決了這個問題:

    ALTER USER 'mysqlUsername'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mysqlUsernamePassword';

    https://dev.mysql 引用。 com/doc/refman/8.0/en/alter-user.html

    如果您正在建立新使用者

    CREATE USER 'jeffrey'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

    https://dev.mysql 引用。 com/doc/refman/8.0/en/create-user.html

    這對我有用

    回覆
    0
  • P粉225961749

    P粉2259617492023-10-14 15:47:19

    從 PHP 7.4 開始,這不再是問題。 mysqlnd 新增了對 caching_sha2 驗證方法的支援。


    目前,PHP mysqli 擴充功能不支援新的 caching_sha2 驗證功能。 您必須等到他們發布更新。

    查看 MySQL 開發人員的相關貼文:https: //mysqlserverteam.com/upgrading-to-mysql-8-0-default-authentication-plugin-considerations/

    他們沒有提到 PDO,也許你應該嘗試使用 PDO 連線。

    回覆
    0
  • 取消回覆