使用 MySql 遇到的问题
1. Entity Framework Core 连接 MySql
连接数据库出现以下错误:
System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseMySql' call.
版本问题,使用自动检测版本方法
var connectionString = configuration.GetConnectionString(connectStr); services.AddDbContext<T>(options => { options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)); });
2. 认证问题
MySql 8.0 默认使用 caching_sha2_password 插件认证,解决方法:
. https://mysqlconnector.net/troubleshooting/retrieval-public-key/#:~:text=To%20retrieve%20the%20server%E2%80%99s%20public%20key%2C%20connect%20securely,to%20the%20connection%20string%3B%20this%20is%20potentially%20insecure.
. 修改认证
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'Your New Password'; FLUSH PRIVILEGES;
4. 无法远程连接
可能的原因:
- 设置对应用户的 Host 为 %
- MySql 配置中注释掉 bind-address = 127.0.0.1
重启 MySql 服务
自律 平静 思考 实践