天天看點

default_authentication_plugin 參數的設定

前言:最近做了個​

​MySQL​

​的壓力測試,使用的是​

​sysbench​

​測試工具大家感興趣可以看看  ​​使用這個工具的使用發現了一個之前沒有遇到的問題,因為使用​

​sysbench​

​的時候需要遠端登入​

​MySQL​

​,

使用​

​-u root -p​

​的時候彈出了錯誤​

​error 2059: Authentication plugin 'caching_sha2_password' cannot be loaded​

去官網查了查,發現是版本的問題,原來​

​Mysql​

​在之前的版本的關于​

​password​

​的加密方法都是使用的  ​

​mysql_native_password​

不過到​

​MySQL8.0​

​的時候換成了​

​caching_sha2_password​

​,是以就會報錯,需要我們安裝額外的插件,下面我們就來示範一下如何不用裝插件的方法來規避這個錯誤。

示範:

  運作環境:​

​Centos7.4​

​ +​

​MySQL 8.0.11​

​版本

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.11    |
+-----------+
1 row in set (0.00 sec)

mysql> show variables like 'default_authentication_plugin';
+-------------------------------+-----------------------+
| Variable_name                 | Value                 |
+-------------------------------+-----------------------+
| default_authentication_plugin | caching_sha2_password |
+-------------------------------+-----------------------+
1 row in set (0.01 sec)

mysql> select host,user,plugin from mysql.user;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| %         | root             | caching_sha2_password |
| localhost | mysql.infoschema | mysql_native_password |
| localhost | mysql.session    | mysql_native_password |
| localhost | mysql.sys        | mysql_native_password |
| localhost | root             | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
--------------------- 
      

​Centos7.4​

​MySQL 5.7​

mysql> select version();
+------------+
| version()  |
+------------+
| 5.7.15-log |
+------------+
1 row in set (0.00 sec)

mysql> show variables like 'default_authentication_plugin';
+-------------------------------+-----------------------+
| Variable_name                 | Value                 |
+-------------------------------+-----------------------+
| default_authentication_plugin | mysql_native_password |
+-------------------------------+-----------------------+
1 row in set (0.01 sec)

mysql> select host,user,plugin from mysql.user;
+-----------+-----------+-----------------------+
| host      | user      | plugin                |
+-----------+-----------+-----------------------+
| localhost | root      | mysql_native_password |
| localhost | mysql.sys | mysql_native_password |
| %         | root      | mysql_native_password |
--------------------- 
      

  可以看到​

​MySQL8.0.11​

​版本預設的認證方式是​

​caching_sha2_password​

​,而在​

​MySQL5.7​

​版本則為​

​mysql_native_password​

​。若想在​

​MySQL8.0​

​版本中繼續使用舊版本中的認證方式需要在​

​my.cnf​

​檔案中配置并重新開機,因為此參數不可動态修改。

mysql> set global default_authentication_plugin='mysql_native_password';
ERROR 1238 (HY000): Variable 'default_authentication_plugin' is a read only variable
      

  寫入​

​my.cnf​

​檔案後重新開機​

​MySQL​

​:

vim my.cnf
[mysqld]
default_authentication_plugin=mysql_native_password
      

  另一種解決方法:相容新老版本的認證方式。

ALTER USER 'root'@'localhost' IDENTIFIED BY 'root' PASSWORD EXPIRE NEVER; #修改加密規則 
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'; #更新一下使用者的密碼 
FLUSH PRIVILEGES; #重新整理權限
--建立新的使用者:
create user root@'%' identified WITH mysql_native_password BY 'root';
grant all privileges on *.* to root@'%' with grant option;
flush privileges;
--在MySQL8.0建立使用者并授權的語句則不被支援:
mysql> grant all privileges on *.* to root@'%' identified by 'root' with grant option;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by 'root' with grant option' at line 1
mysql> 
      

總結:

繼續閱讀