MySQL身份验证插件(mysql_native_password、sha256_password、caching_sha2_password)

mysql_native_password

mysql_native_password插件使用SHA1哈希, 将密码(SHA1(SHA1(password))存储在mysql.user表中,

优点是允许使用质询-响应机制进行身份验证,可以在未加密的通道上验证客户端的身份,无需发送实际密码。

旧的mysql_old_password插件在5.6中弃用,5.7中删除;从5.7开始默认使用mysql_native_password插件

Pre-4.1 password hashes and the mysql_old_password plugin are deprecated in MySQL 5.6 and support for them is removed in MySQL 5.7. They provide a level of security inferior to that offered by 4.1 password hashing and the mysql_native_password plugin. https://dev.mysql.com/doc/refman/5.6/en/account-upgrades.html

查看默认认证方式

1
show variables like 'default_authentication_plugin';

查看用户认证方式

1
select host,user,plugin from mysql.user;

更改用户认证方式

1
ALTER USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password  BY 'password';

sha256_password

sha256_password插件使用SHA256哈希。在mysql8.0+版本中改为使用caching_sha2_password插件。

caching_sha2_password

mysql5.7开始引入caching_sha2_password插件,使用SHA256哈希,

mysql8.0开始默认使用caching_sha2_password身份验证,caching_sha2_password

相比sha256_password在服务器端使用缓存以获得更好的性能, 同时提供了对使用Unix套接字文件和共享内存协议的客户端连接的支持,这些仅在mysql8.0服务端实现支持.

caching_sha2_password兼容性问题和解决方案

  1. 修改my.cnf配置恢复使用mysql_native_password
1
2
[mysqld]
default-authentication-plugin=mysql_native_password
  1. 初始化数据库的时候使用参数:--default-authentication-plugin=mysql_native_password 加上--initialize--initialize-insecure 选项

How caching_sha2_password works?

参考