第一种:在数据库连接 URL 中添加参数
在 JDBC 连接 URL 里添加 allowPublicKeyRetrieval=true 参数,以此允许客户端从服务器获取公钥。
若你使用的是 application.properties 文件,可按如下方式修改:
spring.datasource.url=jdbc:mysql://192.168.163.129:3306/share-system?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
若使用的是 application.yml 文件,则修改如下:
spring:
datasource:
url: jdbc:mysql://192.168.163.129:3306/share-system?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
方法二:修改 MySQL 用户的身份验证插件
把 MySQL 用户的身份验证插件从 caching_sha2_password 改为 mysql_native_password,此插件无需公钥检索。
登录 MySQL 服务器:
mysql -u root -p
修改用户的身份验证插件:
ALTER USER 'your_username'@'your_host' IDENTIFIED WITH mysql_native_password BY 'your_password';
这里的 your_username
是你的数据库用户名,your_host
是允许连接的主机,your_password
是用户密码。
刷新权限:
FLUSH PRIVILEGES;
评论区