2

This issue has been on our production since a long time. There are thousands of users's machine who keep trying to connect to MySQL simulteneously and fire queries on multiple databases by keep changing the database according to the requirements.

To change the database this is what the client machine is having -

private MySqlConnection Conn;

public void ChangeDatabase(string DatabaseName)
{
    if (Conn.State != ConnectionState.Open)
    {
        Conn.Open();
    }
    Conn.ChangeDatabase(DatabaseName);
}

According to our guess, On some occassions, when Database Change happens we get the Exeption -

[Standard Exception Being Caught : MySql.Data.MySqlClient.MySqlException] Authentication to host '_._._._' for user '----' using method 'mysql_native_password' failed with message: Reading from the stream has failed.

Here is the stack trace -

    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()

This issue doesn't come regularly. It just come up suddenly from nowhere when everything was working fine.

I have tried other solution available on SO for this, But none of them is helping us. As I have guessed, This exception mostly comes in the scenario when the database is changing.

Arpit Gupta
  • 1,209
  • 1
  • 22
  • 39
  • The exception call stack looks strange/incomplete. Please update with _all_ inner exceptions and their call stacks. (MySQL Connector/NET can throw a lot of similar-looking exceptions with different inner exceptions.) – Bradley Grainger Sep 17 '18 at 13:22
  • @BradleyGrainger Hi, I have kept inner exception also. Please check out if you can help me out in this. – Arpit Gupta Sep 18 '18 at 11:34
  • Please see my answer here: https://stackoverflow.com/a/52379213/23633 – Bradley Grainger Sep 18 '18 at 14:21

2 Answers2

1

Solution 1: If SSL is not required. Since it is caused by SSL, we can turn off SSL by appending "SslMode=None" to the connection string.

Solution 2: If SSL is required, server identity is important and needs to be verified. The server needs a internet connection to do the cert verification. Please note the crypto API doesn't update CTL for every process. The CTL is maintained at operating system level. Once you connect the server to internet and make an SSL database connection to the server, the CTL will be updated automatically. Then you may disconnect the internet connection. Note again the CTL has its expiration date and after that the Windows needs to update it again. This will occur probably after several months.

Solution 3: If SSL is required but the server identity is not important. Typically SSL is only used to encrypt the network transport in this case. We can turn off CTL update:

  1. Press Win+R to open the "Run" dialog Type "gpedit.msc" (without quotes) and press Enter
  2. In the "Local Group Policy Editor", expand "Computer Configuration", expand "Administrative Templates", expand "System", expand "Internet Communication Management", and then click "Internet Communication settings".
  3. In the details panel, double-click "Turn off Automatic Root Certificates Update", click Enabled, then click OK. This change will be effective immediately without restart.

https://blog.csdn.net/fancyf/article/details/78295964 Clearly explains the root cause and solution.

Bharathi
  • 131
  • 1
  • 2
0

I read a lot solution the most confuse and appeared everywhere is this link because it was referred alot. https://blog.csdn.net/fancyf/article/details/78295964

But the issue quite straight forward that the connection cannot be established.

In my case the local works well when connecting to mySQL server. But when deployed to Windows Server 2012 R2 I got the error.

The solution after search on google for a while is:

Increase the connection time out by adding this to the connection string: Connection Timeout = 30 https://www.connectionstrings.com/mysql-connector-net-mysqlconnection/specifying-connection-attempt-timeout/

So I think 2 servers need time to negotiate and make a handshake, so the default timeframe is not enough.

Poppy
  • 41
  • 6