Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 随机MySQL异常-';从流读取失败';在更改数据库时_C#_Mysql - Fatal编程技术网

C# 随机MySQL异常-';从流读取失败';在更改数据库时

C# 随机MySQL异常-';从流读取失败';在更改数据库时,c#,mysql,C#,Mysql,这个问题已经在我们的生产上很久了。 成千上万的用户机器一直试图同时连接到MySQL,并通过不断根据需要更改数据库来对多个数据库进行查询 要更改数据库,这是客户端计算机的功能- private MySqlConnection Conn; public void ChangeDatabase(string DatabaseName) { if (Conn.State != ConnectionState.Open) { Conn.Open(); }

这个问题已经在我们的生产上很久了。 成千上万的用户机器一直试图同时连接到MySQL,并通过不断根据需要更改数据库来对多个数据库进行查询

要更改数据库,这是客户端计算机的功能-

private MySqlConnection Conn;

public void ChangeDatabase(string DatabaseName)
{
    if (Conn.State != ConnectionState.Open)
    {
        Conn.Open();
    }
    Conn.ChangeDatabase(DatabaseName);
}
根据我们的猜测,在某些情况下,当数据库发生更改时,我们会得到验证-

[捕获的标准异常:MySql.Data.MySqlClient.MySqlException]对用户的主机“\uu.\uu.\uu”的身份验证------使用方法“MySql\u native\u password”失败,消息为:从流读取失败。

这是堆栈跟踪-

    Void Trace(System.Exception) [FileName: ; Line:0].
    Void TimerProcess_Elapsed(System.Object, System.Timers.ElapsedEventArgs) [FileName: ; Line:0].
    Void MyTimerCallback(System.Object) [FileName: ; Line:0].
    Void CallCallbackInContext(System.Object) [FileName: ; Line:0].
    Void RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) [FileName: ; Line:0].
    Void Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) [FileName: ; Line:0].
    Void CallCallback() [FileName: ; Line:0].
    Void Fire() [FileName: ; Line:0].
    Void FireNextTimers() [FileName: ; Line:0].
    Void AppDomainTimerCallback() [FileName: ; Line:0]. at
    MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.AuthenticationFailed(Exception ex) at
    MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket() at
    MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.Authenticate(Boolean reset) at
    MySql.Data.MySqlClient.NativeDriver.Authenticate(String authMethod, Boolean reset) at
    MySql.Data.MySqlClient.NativeDriver.Open() at
    MySql.Data.MySqlClient.Driver.Open() at
    MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings) at
    MySql.Data.MySqlClient.MySqlConnection.Open() at
    FSCCClientUploadServiceLive.ProcessClientUpload.TimerProcess_Elapsed(Object sender, ElapsedEventArgs e)
----------------------------------------------------------
Time stamp 18/09/2018 12:25:48.325
Thread details 27
Message:    [InnerException] Reading from the stream has failed.
Detail: [Source] MySql.Data
Stack Trace:
    Void Trace(System.Exception) [FileName: ; Line:0].
    Void TimerFinish_Elapsed(System.Object, System.Timers.ElapsedEventArgs) [FileName: ; Line:0].
    Void MyTimerCallback(System.Object) [FileName: ; Line:0].
    Void CallCallbackInContext(System.Object) [FileName: ; Line:0].
    Void RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) [FileName: ; Line:0].
    Void Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) [FileName: ; Line:0].
    Void CallCallback() [FileName: ; Line:0].
    Void Fire() [FileName: ; Line:0].
    Void FireNextTimers() [FileName: ; Line:0].
    Void AppDomainTimerCallback() [FileName: ; Line:0].
       at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket()
----------------------------------------------------------
Time stamp 18/09/2018 12:25:49.093
Thread details 27
Message:    [InnerException] Attempted to read past the end of the stream.
Detail: [Source] MySql.Data
Stack Trace:
    Void Trace(System.Exception) [FileName: ; Line:0].
    Void TimerFinish_Elapsed(System.Object, System.Timers.ElapsedEventArgs) [FileName: ; Line:0].
    Void MyTimerCallback(System.Object) [FileName: ; Line:0].
    Void CallCallbackInContext(System.Object) [FileName: ; Line:0].
    Void RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) [FileName: ; Line:0].
    Void Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) [FileName: ; Line:0].
    Void CallCallback() [FileName: ; Line:0].
    Void Fire() [FileName: ; Line:0].
    Void FireNextTimers() [FileName: ; Line:0].
    Void AppDomainTimerCallback() [FileName: ; Line:0].
       at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
这个问题不是经常出现的。当一切正常的时候,它突然从哪里冒出来


为此,我尝试了其他可用的解决方案,但没有一个对我们有帮助。正如我所猜测的,此异常主要出现在数据库正在更改的场景中。

解决方案1:如果不需要SSL。因为它是由SSL引起的,所以我们可以通过在连接字符串中附加“SslMode=None”来关闭SSL

解决方案2:如果需要SSL,服务器标识很重要,需要验证。服务器需要internet连接才能进行证书验证。请注意,加密API不会为每个进程更新CTL。CTL保持在操作系统级别。将服务器连接到internet并与服务器建立SSL数据库连接后,CTL将自动更新。然后您可以断开internet连接。请再次注意,CTL有其到期日期,之后Windows需要再次更新它。这可能会在几个月后发生

解决方案3:如果需要SSL,但服务器标识并不重要。在这种情况下,SSL通常仅用于加密网络传输。我们可以关闭CTL更新:

  • 按Win+R打开“运行”对话框,键入“gpedit.msc”(不带引号),然后按Enter键
  • 在“本地组策略编辑器”中,展开“计算机配置”,展开“管理模板”,展开“系统”,展开“Internet通信管理”,然后单击“Internet通信设置”
  • 在详细信息面板中,双击“关闭自动根证书更新”,单击启用,然后单击确定。此更改将立即生效,无需重新启动

  • 清楚地解释了根本原因和解决方案。

    我读了很多解决方案。最让人困惑的是,到处都出现了这个链接,因为它被引用了很多。

    但这个问题相当直截了当,即这种联系无法建立

    在我的例子中,本地服务器在连接到mySQL服务器时运行良好。 但当部署到Windows Server 2012 R2时,我遇到了错误

    在谷歌上搜索一段时间后,解决方案是:

    通过将以下内容添加到连接字符串来增加连接超时: 连接超时=30


    因此,我认为两台服务器需要时间进行协商和握手,因此默认的时间框架是不够的。

    异常调用堆栈看起来很奇怪/不完整。请使用所有内部异常及其调用堆栈进行更新。(MySQL Connector/NET可以抛出许多外观相似的异常,但内部异常不同。)@BradleyGrainger嗨,我也保留了内部异常。请查看是否可以在这方面帮助我。请在此处查看我的答案: