1. 程式人生 > 資料庫 >詳解關於MySQL 8.0走過的坑

詳解關於MySQL 8.0走過的坑

今天手賤更新了MySQL 8.0

第一個問題:Navicat連線不上資料庫

安裝的mysql為localhost:3306,配置一切預設,安裝後開啟Navicat 12 新建連線,直接報錯

authentication plugin 'caching_sha2_password'

身份驗證外掛不能被載入

查了下官方文件6.5.1.3 Caching SHA-2 Pluggable Authentication

原來在MySQL 8.0中,caching_sha2_password取代了mysql_native_password成為預設的身份驗證外掛,官方給出的解決方案如下

1、重新配置伺服器以恢復到以前的預設身份驗證外掛(mysql_native_password)。

[mysqld]
default_authentication_plugin=mysql_native_password

該設定允許8.0之前的客戶端連線到8.0伺服器,但是,該設定應被視為臨時設定,而不是長期或永久性解決方案,因為它會導致使用有效設定建立的新帳戶放棄提供的改進的身份驗證安全性 caching_sha2_password。

2、將根管理帳戶的身份驗證方式更改為mysql_native_password。

對於新的MySQL 8.0安裝,在初始化資料目錄時,將建立帳戶'root'@'localhost',並且該帳戶將預設使用caching_sha2_password。連線到伺服器root並使用ALTER USER 如下更改帳戶身份驗證外掛和密碼:

ALTER USER 'root'@'localhost'
 IDENTIFIED WITH mysql_native_password
 BY 'password';

至此,解決了MySQL 8.0的預設身份校驗更換問題。

第二個問題:Caused by: java.sql.SQLException: Unknown initial character set index '255'...

在更新完資料庫後,本地啟了一個java小工程,連線資料庫跑了個測試程式直接丟擲異常,叕查了一下官方文件 Changes in MySQL 8.0.1 (2017-04-10,Development Milestone) ,原來是8.0.1的版本將Unicode字符集支援中進行了幾項重要更改,預設字符集已從更改latin1為 utf8mb4。而這個這個系統預設 collation_server 和 collocation_database 系統變數由 latin1_swedish_ci 變為 utf8mb4_0900_ai_ci。

解決辦法:所有這些更改都已經在新版本的MySQL聯結器Java中進行了處理,不需要配置MySQL。所以只需要升級MYSQL的版本即可,將5.1.6更改為5.1.44,問題完美解決。

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.44</version>
  </dependency> 

問題三安裝完成後進入資料庫show databases;、或者嘗試更改許可權時報錯

ERROR 1449 (HY000): The user specified as a definer ('mysql.infoschema'@'localhost') does not exist
Table 'mysql.role_edges' doesn't exist

解決方法

mysql_upgrade -u root -p;

問題四:在客戶端成功連線資料庫之後,發現專案裡的pdo連線mysql又報錯了。

Next PDOException: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client [caching_sha2_password] in /vendor/yiisoft/yii2/db/Connection.php:687

這個錯可能是mysql預設使用caching_sha2_password作為預設的身份驗證外掛,而不再是mysql_native_password,但是客戶端暫時不支援這個外掛導致的。官方文件說明

In MySQL 8.0,caching_sha2_password is the default authentication plugin rather than mysql_native_password. For information about the implications of this change for server operation and compatibility of the server with clients and connectors,see caching_sha2_password as the Preferred Authentication Plugin.

在MySQL 8.0中,caching_sha2_password是預設的身份驗證外掛,而不是mysql_native_password。有關此更改對伺服器操作的影響以及伺服器與客戶端和聯結器的相容性的資訊,請參閱caching_sha2_password作為首選身份驗證外掛。

解決方法

編輯my.cnf檔案,更改預設的身份認證外掛。

$ vi /etc/my.cnf

在[mysqld]中新增下邊的程式碼

default_authentication_plugin=mysql_native_password

然後重啟mysql

$ service mysqld restart

網站終於正常打開了。。。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。