mysql8.0出現的2059 - authentication plugin 'caching_sha2_password' -navicat連接異常問題
問題描述:
在navicat鏈接mysql8以后的版本時會出現2059的錯誤,這個錯誤出現的原因是在mysql8之前
的版本中加密規則為mysql_native_password
,而在mysql8以后
的加密規則為caching_sha2_password
。
解決方案:
- 將mysql8.0以后驗證方式改為以前版本使用的驗證方式
mysql_native_password
。
找到mysql對應的安裝目錄下my-default.ini
文件,
將default_authentication_plugin=caching_sha2_password
改為default_authentication_plugin=mysql_native_password
。
以下是我的my-default.ini
的內容:[mysqld] # 設置3306端口 port=3306 # 設置mysql的安裝目錄 basedir=C:\Program Files\MySQL\MySQL Server 8.0 # 設置mysql數據庫的數據的存放目錄 datadir=C:\Program Files\MySQL\MySQL Server 8.0\data # 允許最大連接數 max_connections=200 # 允許連接失敗的次數。這是為了防止有人從該主機試圖攻擊數據庫系統 max_connect_errors=10 # 服務端使用的字符集默認為UTF8 character-set-server=utf8 # 創建新表時將使用的默認存儲引擎 default-storage-engine=INNODB # 默認使用“mysql_native_password”插件認證 default_authentication_plugin=mysql_native_password [mysql] # 設置mysql客戶端默認字符集 default-character-set=utf8 [client] # 設置mysql客戶端連接服務端時默認使用的端口 port=3306 default-character-set=utf8
- 以
管理員身份
運行cmd(win10右鍵左下角開始按鈕選擇以管理員身份運行cmd即可) - 以管理員身份運行cmd進入mysql的安裝目錄下的bin文件夾
C:\>cd C:\Program Files\MySQL\MySQL Server 8.0\bin
- 如果C:\Program Files\MySQL\MySQL Server 8.0\bin 目錄下沒有data文件夾,執行以下命令:
- 輸入
mysqld -install
(如果不用管理員身份運行,將會因為權限不夠而出現錯誤:Install/Remove of the Service Denied!) - 運行
mysqld --initialize
即可,此時查看已有data文件夾。
- 輸入
- 登錄數據庫
命令行執行mysql -u root -p
然后輸入數據庫密碼,出現Welcome to the MySQL monitor. .. 字樣則登錄成功。 - 修改加密規則
執行ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
- 更新一下用戶的密碼
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'xxxxx';
其中的xxxxx 為新設置的密碼。 - 刷新權限
執行FLUSH PRIVILEGES; - 然后打開Navicat連接Mysql,發現連接成功了,完美解決問題。
- 具體操作如下:
C:\>cd C:\Program Files\MySQL\MySQL Server 8.0\bin C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql -u root -p Enter password: ****** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.13 MySQL Community Server - GPL Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; Query OK, 0 rows affected (0.07 sec) mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'abcd'; Query OK, 0 rows affected (0.06 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) mysql>
經過一系列操作最終解決了問題,Navicat順利連接Mysql。