暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

MySQL 8.0 | 身份验证插件

数据库实用技能 2021-04-19
6742


MySQL 8.0默认的身份验证插件caching_sha2_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作为首选身份验证插件。

     caching_sha2_password和 sha256_password认证插件提供比mysql_native_password插件更安全的密码加密 ,并且 caching_sha2_password提供了比mysql_native_password更好的性能sha256_password。由于安全性和性能特性 caching_sha2_password它是MySQL 8.0首选的身份验证插件,而且也是默认的身份验证插件而不是 mysql_native_password。此更改会影响服务器和libmysqlclient 客户端库;目前来说和经常使用的客户端软件兼容性不好。

       新安装MySQL8.0的数据库默认是使用caching_sha2_password身份验证的。

若要更改root用户的身份认证方式,可使用命令:

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


 caching_sha2_password兼容性问题和解决方案


      你的MySQL安装必须服务于8.0之前的客户端,并且在升级到MySQL 8.0或更高版本后遇到兼容性问题,解决这些问题并恢复8.0之前的兼容性的最简单方法是重新配置服务器以恢复到以前的默认身份验证插件(mysql_native_password)。比如在配置文件my.cnf中使用以下行:

      [mysqld]

      default_authentication_plugin=mysql_native_password

       该设置允许8.0之前的客户端连接到8.0服务器,直到安装使用的客户端和连接器升级为了 caching_sha2_password。但是,该设置应被视为临时设置,而不是长期或永久性解决方案,因为它会导致使用有效设置创建的新帐户放弃提供的改进的身份验证安全性 caching_sha2_password

      注意:如果目前使用的客户端和连接器不支持caching_sha2_password,则可以使用修改后的数据目录初始化过程,该过程会在创建root帐户后mysql_native_password立即关联该帐户,可以在初始化数据库的时候使用以下的方法解决:

①:初始化数据库的时候使用参数:--default-authentication-plugin=mysql_native_password 加上--initialize或 --initialize-insecure 选项

②:在配置文件中设置 default_authentication_plugin 为mysql_native_password选项,并使用--defaults-file选项和--initializeor 或 --initialize-insecure。

(在这种情况下,如果您继续将该选项文件用于后续服务器启动,则将创建新帐户,mysql_native_password而不是 caching_sha2_password除非您default_authentication_plugin 从选项文件中删除该 设置。)


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)

 

MySQL5.7版本:

mysql> select version();

+------------+

| version()  |

+------------+

| 5.7.20-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 文件中配置并重启,因为此参数不可动态修改。

set global default_authentication_plugin='mysql_native_password';

写入my.cnf 文件后重启MySQL:

vi my8888.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 sysbench@'%' identified WITH mysql_native_password BY 'sysbench';              grant all privileges on *.* to sysbench@'%' with grant option;

flush privileges;


文章转载自数据库实用技能,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论