.NET Core使用Pomelo连接远程MySQL数据库失败求助
使用Pomelo.EntityFrameworkCore.MySql连接远程MySQL数据库失败,本地及Workbench可正常连接
我尝试使用Pomelo.EntityFrameworkCore.MySql连接远程MySQL数据库,已查阅过类似问题但未找到适配的解决方案。目前可以通过Workbench正常访问该远程数据库,本地安装数据库并使用localhost而非IP地址时连接也完全正常,但使用IP连接远程库时始终失败。
配置与代码
appsettings.json(已移除敏感凭据,确认与Workbench使用的一致)
"ConnectionStrings": { "WebApiDatabase": "Server=<ipaddress>; port=3306; Database=<dbname>;Uid=<user>;pwd=<pw>;Connection Timeout=10;" },
Program.cs
string verbindungsString = builder.Configuration.GetConnectionString("WebApiDatabase"); System.Diagnostics.Debug.WriteLine(verbindungsString); builder.Services.AddDbContext<EtikettenContext>(options => options.UseMySql(verbindungsString, ServerVersion.AutoDetect(verbindungsString), options => options.EnableRetryOnFailure( maxRetryCount: 5, maxRetryDelay: System.TimeSpan.FromSeconds(30), errorNumbersToAdd: null)) .LogTo(Console.WriteLine, LogLevel.Information) .EnableSensitiveDataLogging() .EnableDetailedErrors());
错误信息
控制台输出
Microsoft.EntityFrameworkCore.Storage.RetryLimitExceededException: The maximum number of retries (5) was exceeded while executing database operations with 'MySqlRetryingExecutionStrategy'. See the inner exception for the most recent failure. ---> MySqlConnector.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. at MySqlConnector.Core.ServerSession.OpenTcpSocketAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/ServerSession.cs:line 1146 ...(省略重复栈信息)
浏览器端显示的错误信息
Microsoft.EntityFrameworkCore.Storage.RetryLimitExceededException: The maximum number of retries (5) was exceeded while executing database operations with 'MySqlRetryingExecutionStrategy'. See the inner exception for the most recent failure. ---> MySqlConnector.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. ...(省略重复栈信息) HEADERS ======= Accept: text/plain ...(省略头部信息)
实在无法理解为何本地及Workbench可正常连接远程库,但通过Pomelo.EntityFrameworkCore.MySql就连接失败,希望能得到解决思路。
内容的提问来源于stack exchange,提问作者Tobias
相关产品推荐
相关产品推荐

