Bug #75917 Reading from the stream has failed
Submitted: 16 Feb 2015 11:59 Modified: 6 Aug 2015 12:27
Reporter: Félix de Lelis Email Updates:
Status: Can't repeat Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.9.5 OS:Windows
Assigned to: CPU Architecture:Any

[16 Feb 2015 11:59] Félix de Lelis
Description:
I am  using a service .Net that connect to Mysql server 5.6 and move data between two  databases of the server. The versions of all elements are:

-Mysql server 5.6 (Percona xtraback).
-Windows 6.1 7601
-Versión .NET Framework 4.5.51209
-MySQL.Data 6.9.5.0(connector .Net)

The problem is that randomly this process don't connect and lunch the following error:

"Reading from the stream has failed"

I have searched if could be a bug, but only I have seen bugs that would be solved: exceeded net_write_timeout no longer should lunch this exception. Then, I would comment that I can't open other connection by checking the last one because this connection should be the same.   

How I can solve this issue?

Thanks. 

How to repeat:
-Connect with .net connector to mysql server 5.6(Percona)
[16 Feb 2015 16:08] Félix de Lelis
Error

Attachment: error_mysql.png (image/png, text), 52.00 KiB.

[18 Feb 2015 16:10] hans depreitere
Same probleme:

Database: mysql5.6
Updated mysql connector to 6.9.5.0 (installed, recompiled sources)

Mysql error: user@hostip : Autohentication to host 'hostip' for 'user' using method 'mysql_native_password' failed with message: Reading from the stream has failed.
[27 Feb 2015 6:03] Chiranjeevi Battula
Hello Felix de Lelis,

Thank you for the bug report.
This is a duplicate of Bug #72110, please see the explaination by dev's in #72110.

Quoting from #72110 - [21 May 15:40] Francisco Alberto Tirado Zavala:

When you are working with a Client-Server application all the connections are performed by Sockets, when you create a connection you open a Socket between your client and your server, and an Id/ProccesId is assigned/reserved in the server for the connection requested by the client. Also you must consider that the server can terminate or close a connection if the connection is inactive after some time, so you need to create a new valid connection opening a new socket to get a valid Id from the server.

If you can't change the design in your application (having a global connection), as a workaround you can have a method to validate that the connection is valid, for example the following method ping the server and if the ping fails it open the connection again:

. . .
MySqlConnection _conn = new MySqlConnection("MyConnectionString");
private void CheckConnection()
{
   if(!_conn.Ping())
   {
     _conn.Open();
   }
}
. . .

As you can see the code is very simple and it works in the following way: when the connection try to ping the server if the connection is invalid, internally the stream that has the socket information will be terminated and the connection will be set on status closed as well as the invalid connection will be removed, then opening the connection again will create a new connection which means a new valid socket. If the ping succeed so you will continue using the same connection.

To use it you can call it before trying to perform an action to the database, and the connection will be re-opened just when is invalid:

. . .
private void button1_Click(object sender, EventArgs e)
{
  //assuming that the connection is already open
  CheckConnectionState();
  var cmd = new MySqlCommand("select 1", _conn);
  cmd.ExecuteNonQuery();
}
. . .

This is just a workaround, and as far I know all the connections to any database server will have the same behavior.

Thanks,
Chiranjeevi.
[28 Mar 2015 1:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
[3 Jul 2015 13:58] Mohd Sahar
Hi,
Below my stack trace

Stack Trace: 

[SocketException (0x2745): An established connection was aborted by the software in your host machine]
   System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) +249

[IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.]
   MySql.Data.Common.MyNetworkStream.HandleOrRethrowException(Exception e) +261
   MySql.Data.Common.MyNetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count) +424
   MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count) +220
   System.IO.BufferedStream.Read(Byte[] array, Int32 offset, Int32 count) +185
   MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count) +132
   MySql.Data.MySqlClient.MySqlStream.LoadPacket() +142

[MySqlException (0x80004005): Reading from the stream has failed.]
   MySql.Data.MySqlClient.MySqlStream.LoadPacket() +811
   MySql.Data.MySqlClient.MySqlStream.ReadPacket() +88
   MySql.Data.MySqlClient.NativeDriver.ReadPacket() +81
   MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket() +93

[MySqlException (0x80004005): Authentication to host 'localhost' for user 'root' using method 'mysql_native_password' failed with message: Reading from the stream has failed.]
   MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.AuthenticationFailed(Exception ex) +450
   MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket() +180
   MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.Authenticate(Boolean reset) +673
   MySql.Data.MySqlClient.NativeDriver.Authenticate(String authMethod, Boolean reset) +278
   MySql.Data.MySqlClient.NativeDriver.Open() +2794
   MySql.Data.MySqlClient.Driver.Open() +126
   MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings) +407
   MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() +53
   MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() +453
   MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() +265
   MySql.Data.MySqlClient.MySqlPool.GetConnection() +166
   MySql.Data.MySqlClient.MySqlConnection.Open() +2600
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +292
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +420
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +275
   AMS._default.Page_Load(Object sender, EventArgs e) in D:\Users\mrart\Dropbox\My Projects\OWN\AMS Solutions\AMS\default.aspx.cs:35
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178
[8 Jul 2015 12:06] scott mankowitz
I am having the same issue. I find that when I run the program from within visual studio, it is fine. When I run it as a scheduled task, it gives me the exception about 50% of the time. I don't think there is a problem with a stale connection, as the program opens the connection and uses it right away.

This is my source:

Imports System.Xml
Imports System.Xml.Linq
Imports MySql.Data.MySqlClient

Module Module1

    Sub Main()

        Dim MySQLConn As New MySqlConnection(My.Settings.MySQLConnectionString)
        Dim MySqlMasterRows As Integer = 0
        Dim watch As Stopwatch = Stopwatch.StartNew()

        If MySQLConn.Ping() = False Then
            MySQLConn.Open()
        End If

My connection string is: 
Server=localhost;Database=documenter;Uid=doc;Pwd=xxxxx;

The exception I get is this:

Unhandled Exception: MySql.Data.MySqlClient.MySqlException: Authentication to host 'localhost' for user 'doc' using method 'mysql_native_password' failed with message: Reading from the stream has failed. ---> MySql.Data.MySqlClient.MySqlException: Reading from the stream has failed. ---> System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at MySql.Data.Common.MyNetworkStream.HandleOrRethrowException(Exception e)
   at MySql.Data.Common.MyNetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.BufferedStream.Read(Byte[] array, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
   --- End of inner exception stack trace ---
   at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket()
   --- End of inner exception stack trace ---
   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.MySqlPool.GetPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
   at MySql.Data.MySqlClient.MySqlPool.GetConnection()
   at MySql.Data.MySqlClient.MySqlConnection.Open()
[29 Jul 2015 11:29] scott mankowitz
I thought that connection pooling or IP address might make a difference, but it appears that it does not. I changed my connection string to the following with no luck:

Server=127.0.0.1;Database=documenter;Uid=doc;Pwd=xxxx;pooling=false;
[29 Jul 2015 12:39] Chiranjeevi Battula
Hello scott mankowitz,

Thank you for your feedback.
I could not repeat the issue using with MySQL Connector/Net 6.9.6 and event scheduled tasks.
The error is due to server closing the socket due to timeout, so the client side can do little here, but you can certainly try:

a) Increase the net_write_timeout & net_read_timeout (like 999999).
b) Read the data in small chunks, like
select * from table1 limit 1,100000
select * from table1 limit 100000,199999
select * from table1 limit 199999,299999 
and so on.
Please check if the workaround suggested by dev's(pls see #57365 - [11 Oct 2012 3:27] Fernando Gonzalez Sanchez) works.
Feel free to open the bug if the workaround doesn't work(include details of log file,if having scheduler information and exact steps to reproduce the issue at our end)

Thanks,
Chiranjeevi.
[29 Jul 2015 17:37] scott mankowitz
Hi. I'm not sure you understand. I am getting the exception when I attempt to connect to the database, not when I try to read from it. Here is the exception:

Unhandled Exception: MySql.Data.MySqlClient.MySqlException: Authentication to host '127.0.0.1' for user 'doc' using method 'mysql_native_password' failed with message: Reading from the stream has failed. ---> MySql.Data.MySqlClient.MySqlException: Reading from the stream has failed. ---> System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at MySql.Data.Common.MyNetworkStream.HandleOrRethrowException(Exception e)
   at MySql.Data.Common.MyNetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.BufferedStream.Read(Byte[] array, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
   --- End of inner exception stack trace ---
   at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket()
   --- End of inner exception stack trace ---
   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()
[6 Aug 2015 8:14] Chiranjeevi Battula
Hello scott mankowitz,

Thank you for your feedback.
Try to login into your mysql database from the command prompt using your username/password, if it is working no problem in database side.
Please make sure your network having sufficient permissions or not.
Feel free to open the bug if your are having same issue(include details of log file and exact steps to reproduce the issue at our end).

Thanks,
Chiranjeevi.
[6 Aug 2015 11:05] scott mankowitz
Chiranjeevi Battula:

1. The problem is not with correct password. When I run the script from within visual studio, it works fine. The problem only arises when I run it from the task scheduler. 

2. I have found a possible solution. I found that if I explicitly close the connection at the end of the script, it seems to work. I don't know if there's more to it than that, but it has been working for the past few days.
[6 Aug 2015 12:27] Chiranjeevi Battula
Hello scott mankowitz,

Thank you for your feedback.
I'm glad that you found a solution for your problem and let us know if you are still having the issue with complete repeatable steps.

Thanks,
Chiranjeevi.
[22 Oct 2015 9:40] Chiranjeevi Battula
http://bugs.mysql.com/bug.php?id=78919 marked as duplicate of this one.
[1 Apr 2016 12:00] Chiranjeevi Battula
http://bugs.mysql.com/bug.php?id=80921 marked as duplicate of this one.
[22 May 2017 12:48] Chiranjeevi Battula
http://bugs.mysql.com/bug.php?id=86402 marked as duplicate of this one.
[23 Jun 2021 7:01] YAKUP ULUTAŞ
this problem still persists , details explained here

mysql version 5.6.28 , connector versions 8.0.23

https://stackoverflow.com/questions/68085943/mysql-reading-from-the-stream-has-failed
[14 Jun 15:11] Marcelo Peri
I have an MYSQL with ssl enabled. 
In the connection string I have no way to setup this password and key.

I use an ip:22
database :  mydatabase

ssl key file:  aafjafoi4ru9rui

hotname : localhost
port : 3306

username :   myuser
password:  mypassword

defaul_schema: mydatabase

Can Anybody help with this connection type?

Thanks