使用 sha256_password 時如何使用 PHP 的 mysqli 連接到 MySQL(訪問被拒絕) (How do you connect to MySQL using PHP's mysqli when using sha256_password (access denied))


問題描述

使用 sha256_password 時如何使用 PHP 的 mysqli 連接到 MySQL(訪問被拒絕) (How do you connect to MySQL using PHP's mysqli when using sha256_password (access denied))

我正在使用 MySQL 5.7.24 和 PHP 7.2.10。MySQL 配置了 SSL。我有一個用戶 my_user,密碼為 abc123。我嘗試使用兩個身份驗證插件進行配置:

ALTER USER 'my_user'@'192.168.192.150' IDENTIFIED WITH sha256_password BY 'abc123';
ALTER USER 'my_user'@'192.168.192.150' IDENTIFIED WITH mysql_native_password BY 'abc123';

從我的 PHP 服務器,我可以使用 mysql 命令行客戶端成功連接,而無需指定主機、用戶和密碼。但是,從 PHP 中,我只能在使用 mysql_native_password 時進行連接。使用 sha256_password 時不會。

以下 PHP 代碼適用於 mysql_native_password

$mysqli = mysqli_init();
$mysqli‑>options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);

$con = $mysqli‑>real_connect('192.168.192.100', 'my_user', 'abc123', 'my_db', 
3306, null, MYSQLI_CLIENT_SSL | MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT);
var_dump($mysqli‑>query('SELECT 1;'));

但是,當我使用 sha256_password,我收到錯誤“拒絕用戶訪問”


參考解法

方法 1:

For what I understand from documentation, only one auth_option can be active at a time.

So when your are doing

ALTER USER 'my_user'@'192.168.192.150' IDENTIFIED WITH sha256_password BY 'abc123';
ALTER USER 'my_user'@'192.168.192.150' IDENTIFIED WITH mysql_native_password BY 'abc123';

your second resquest actually redefines sha256_password auth to mysql_native_password

(by Nick WilliamsWill)

參考文件

  1. How do you connect to MySQL using PHP's mysqli when using sha256_password (access denied) (CC BY‑SA 2.5/3.0/4.0)

#mysqli #sha256 #MySQL #PHP #SSL






相關問題

如何第一次創建 mysqli 數據庫? (how can I creates a mysqli database for first the first time?)

使用 jquery autosuggest 從多個表中選擇 (select from multiple tables with jquery autosuggest)

無法連接 - 數據庫或編碼錯誤 (Could Not Connect - Database or Coding Error)

mysqli忽略表中的第一行 (mysqli ignoring the first row in a table)

從數據庫導入的鏈接以 UTF8 截斷 (imported link from database cut off at UTF8)

試圖獲取非對象的屬性 (Trying to get property of non-object)

獲取 php 和 mysqli 中插入數據的 ID (Get Id of inserted data in php and mysqli)

WAMP 和 mysqli::real_connect(): (HY000/2002)? (WAMP and mysqli::real_connect(): (HY000/2002)?)

如何將 MySQLi 準備好的語句的結果放入關聯數組中? (How can I put the results of a MySQLi prepared statement into an associative array?)

測試選擇查詢是否找到結果 (Testing if a Select Query found results)

為什麼我的正確連接嘗試在 apache2 下失敗但在 cli 下失敗? (Why is my correct connection attempt failing under apache2 but not under cli?)

在帶有數組的 IN 語句之後,在 WHERE 語句中使用更多佔位符 (Use more placeholders in WHERE statement after IN statement with arrays)







留言討論