参考 [!https://www.cnblogs.com/hegn/p/15129171.html]
造成这个问题的原因是:
mysql 8.0 版本默认使用 caching_sha2_password 身份验证插件机制。
mysql 8.0以前版本(5.7)使用 mysql_native_password。
低版本升级到8.0 验证方式不变,新用户使用8.0验证方式改变,而客户端不支持新的加密方式。
随后我根据文中的方法将密码加密方式改成 mysql_native_password,但是并没有生效。
接着又尝试修改my.ini 文件 在[mysqld]下添加 default_authentication_plugin=mysql_native_password,还是没有生效。
然后又把这两个都改回了caching_sha2_password 尝试一下还是不行。
事情陷入僵局,但是在反复的尝试中发现这好像并不是密码验证的问题。如果验证方式为caching_sha2_password而密码的加密方式为mysql_native_password,那么错误提示应该是Authentication to host 'localhost' for user 'myuser' using method 'caching_sha2_password' failed with message: Access denied for user 'XXX'@'localhost' (using password: YES).
而我一开始的错误提示是Authentication to host 'localhost' for user 'myuser' using method 'caching_sha2_password' failed with message: reading from the stream has failed;额看来是一开始搜的方法就不对,根本就不是同一个错误。
最终在数据库的连接字符串后加上 SslMode=None; 问题就解决了。
还有在多次的尝试中发现一个问题就是我在本地调试项目 连服务器的数据库时在连接字符串中不加上 SslMode=None也能连接成功而同样的连接方法在服务器上的项目就会偶发性的报错,
SslMode 参数
SslMode 参数用于指定客户端与MySQL服务器之间连接时使用的SSL/TLS加密模式。当设置为 None 时,表示客户端与服务器之间的连接不使用SSL/TLS加密。