我正在使用带有 MySQL 的 ASP.NET MVC 5(使用连接器/网络)。我可以连接到数据库并且一切正常......但是一旦服务器繁忙,我就会收到以下异常:
MySql.Data.MySqlClient.MySqlException (0x80004005):无法连接到任何指定的 MySQL 主机。
我正在使用 NinjectDbContext
在 MVC中注入Controller
:
Bind<ApplicationDbContext>().ToSelf().InRequestScope();
我在 Galera 集群中有 3 个 MySQL 服务器实例,这是我的 ConnectionString:
<add name="MyDB" connectionString="Server=IP1,IP2,IP3; Port=3306; Database=dbname; Uid=u1; Pwd=pswd1;" providerName="MySql.Data.MySqlClient" />
当机器人开始抓取网站时,通常会发生此异常。
我见过类似的这个问题,但是问题是他们永远无法连接到 MySQL(我相信他们ConnectionString
是不正确的)但是这个答案(在同一个问题上)似乎描述了我遇到的问题:
如果您的连接最初可以正常工作,但在多次成功连接后您开始看到此错误,则可能是这个问题。
总之:如果您打开和关闭连接,Windows 会出于某些愚蠢的原因保留 TCP 端口以供将来使用。多次执行此操作后,它会耗尽可用端口。
答案完美地描述了这个问题,但是他们的解决方案并没有真正的帮助:
- 他们建议使用连接池(连接器/网络默认使用连接轮询- 所以这没有帮助)
- 他们建议更改 Windows 设置并分配更多端口:
netsh int ipv4 set dynamicport tcp start=10000 num=50000
本文档还解释了一个类似的错误。
但是,我不确定这个问题是否是因为 Windows 端口不足而引起的?
有没有人遇到过类似的问题?
这是我收到的错误的堆栈跟踪:
Exception: UserId: 0 Url: http://www.my-site.com/house-rental/display/109149
UserAgent: facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)
Message: The underlying provider failed on Open.
InnerException: MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
at MySql.Data.Failover.FailoverManager.AttemptConnection(MySqlConnection connection, String originalConnectionString, String& connectionString, Boolean mySqlPoolManager)
at MySql.Data.MySqlClient.MySqlConnection.Open() at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
at System.Data.Entity.Core.EntityClient.EntityConnection.Open() Exception: System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts.
at MySql.Data.Failover.FailoverManager.AttemptConnection(MySqlConnection connection, String originalConnectionString, String& connectionString, Boolean mySqlPoolManager)
at MySql.Data.MySqlClient.MySqlConnection.Open() at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
at System.Data.Entity.Core.EntityClient.EntityConnection.Open() --- End of inner exception stack trace ---
at System.Data.Entity.Core.EntityClient.EntityConnection.Open() at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass41_0.b__0() at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery`1..GetEnumerator>b__31_0() at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at my-site.Infrastructure.Main.UmbrellaServices.AdPersister`1.GetActiveOrInactiveAd(Int64 adBaseId)
in C:\my-site\my-site.source\my-site.Infrastructure.Main\UmbrellaServices\AdPersister.cs:line 46
at my-site.Application.Main.Services.AdService`2.GetActiveOrInactiveAd(Int64 adBaseId)
更新
正如@MarcAlff 所建议的,我运行了以下命令:
SELECT * from performance_schema.host_cache;
结果如下:
| IP | HOST | HOST_VALIDATED | SUM_CONNECT_ERRORS | COUNT_HOST_BLOCKED_ERRORS | COUNT_NAMEINFO_TRANSIENT_ERRORS | COUNT_NAMEINFO_PERMANENT_ERRORS | COUNT_FORMAT_ERRORS | COUNT_ADDRINFO_TRANSIENT_ERRORS | COUNT_ADDRINFO_PERMANENT_ERRORS | COUNT_FCRDNS_ERRORS | COUNT_HOST_ACL_ERRORS | COUNT_NO_AUTH_PLUGIN_ERRORS | COUNT_AUTH_PLUGIN_ERRORS | COUNT_HANDSHAKE_ERRORS | COUNT_PROXY_USER_ERRORS | COUNT_PROXY_USER_ACL_ERRORS | COUNT_AUTHENTICATION_ERRORS | COUNT_SSL_ERRORS | COUNT_MAX_USER_CONNECTIONS_ERRORS | COUNT_MAX_USER_CONNECTIONS_PER_HOUR_ERRORS | COUNT_DEFAULT_DATABASE_ERRORS | COUNT_INIT_CONNECT_ERRORS | COUNT_LOCAL_ERRORS | COUNT_UNKNOWN_ERRORS | FIRST_SEEN | LAST_SEEN | FIRST_ERROR_SEEN | LAST_ERROR_SEEN |
+--------------+-------------------------------------------------+----------------+--------------------+---------------------------+---------------------------------+---------------------------------+---------------------+---------------------------------+---------------------------------+---------------------+-----------------------+-----------------------------+--------------------------+------------------------+-------------------------+-----------------------------+-----------------------------+------------------+-----------------------------------+--------------------------------------------+-------------------------------+---------------------------+--------------------+----------------------+---------------------+---------------------+---------------------+---------------------+
| 1.2.3.4 | ip-1-2-3-4.ap-southeast-2.compute.internal | YES | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 26 | 4010 | 0 | 0 | 8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2020-06-14 11:55:18 | 2022-02-15 07:00:13 | 2020-06-14 23:11:17 | 2022-02-15 01:00:02 |
这些是有错误的列:
COUNT_AUTH_PLUGIN_ERRORS: 26
COUNT_HANDSHAKE_ERRORS: 4010
COUNT_AUTHENTICATION_ERRORS: 8