使用 MySql 遇到的问题

在使用EntityFrameworkCore连接MySql时遇到由于瞬态故障导致的异常,可以通过启用重试机制解决。对于MySql8.0的caching_sha2_password认证问题,可以切换回mysql_native_password插件进行身份验证,具体操作包括修改用户认证方式并刷新权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 '用户名'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
FLUSH PRIVILEGES;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值