Bug #10498 Issue with connecting to MySQL server with .NET in Win XP and Win 2003
Submitted: 10 May 2005 6:33 Modified: 21 Jul 2005 1:47
Reporter: p t Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S1 (Critical)
Version:MySql 4.0.24 OS:Windows (XP SP2)
Assigned to: CPU Architecture:Any

[10 May 2005 6:33] p t
Description:
The problem only occurs on Windows XP and 2003, but works fine on Windows 2000. We have tried using MySql Connector .NET 1.0.4, MySqlDriverCS and MYODBC 3.51, but none of them have resolved the problem.
The Problem: The ASP.NET and Windows applications run fine at the start but after a while when too many queries are made it refuses to connect to mysql and MySql connector .NET gives me the following error "Unable to connect to any of the specified MySQL hosts". When this happens, if I try to login manually to mysql it says: "ERROR 2003: Can't connect to MySQL server on 'localhost' (10048)". After I leave it idle for a while it goes back to running fine but then crashes again when too much load is put on. 
Like I said at the start this is only an issue with Win XP and Win 2003, but not Win 2000.

How to repeat:
If using MySql Connector .NET, try the following code to connect to MySql

public DataSet SelectQuery(string sql)
{
 MySqlConnection conn = new MySqlConnection();
 DataSet dset1 = new DataSet();
 DataTable dtab1 = new DataTable();
 MySqlDataAdapter da = new MySqlDataAdapter();

 string connStr = "server=localhost;user id= USERNAME; password= PASSWORD; database=DotnetDB; pooling=false";

 try 
 {
  conn = new MySqlConnection(connStr);
  conn.Open();
  da = new MySqlDataAdapter(sql,conn);
  conn.Close();
  da.Fill(dset1);
  return dset1;
 }
 catch (MySqlException ex) 
 {
  string temp = ex.ToString();
  conn.Close();
  return null;
 }
}

And then set up an infinite loop that calls the above function with a basic sql statement "Select * from DotnetDB"

You must let it run for a while (say 50 seconds).

Suggested fix:
Unsure of the exact problem to offer any possible fix.
[10 May 2005 6:35] p t
I have also tried MySql 5.0 and still had the same problem
[21 May 2005 5:04] MySQL Verification Team
Please read the bug:

http://bugs.mysql.com/bug.php?id=6580

then you will notice that isn't a MySQL issue.

Thanks in advance.
[21 Jun 2005 23: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".
[18 Jul 2005 8:24] p t
I have added the TcpTimewaitDelay key as suggested by microsoft and set it to 30 seconds but I am still having the same problem.
[19 Jul 2005 7:49] Vasily Kishkin
Tested on Win 2003, Microsoft Visual C# .NET , Connector .NET 1.0.4 Test case is attached.
[19 Jul 2005 7:49] Vasily Kishkin
Test case

Attachment: 10498.zip (application/x-zip-compressed, text), 5.66 KiB.

[19 Jul 2005 18:11] MySQL Verification Team
Could you please say us how many connection exist when begin the
error message for new connections?

Thanks in advance.
[21 Jul 2005 1:47] p t
Sorry, turns out it was the Windows issue: "Unable to Connect from TCP Ports Above 5000". I set the MaxUserPort to 65534 and TcpTimedWaitDelay to 30 and all seems fine.
[21 Sep 2007 12:13] Rajesh Nair
Hi All,
Can't connect to MySQL server on 'localhost' (10048)
I am using Windows 2000 server with MySQL 4 database. According to MySQL this problem will not happen in windows 2000. But i am getting the above error message while connecting MySQL. I am handling with a database of 25,000 records.

What is the solution for that.
Rajesh Nair
[15 Apr 2010 21:01] Bruno Carnaes
Dude, you are getting this error due to the amount of connections you are establishing with the server. 

I had the same message here, and calculating how many trys I was trying, got something about 900*6! 

So.. how I fixed it? You can change your script to do just ONE try of connection, BUT if you did as I did, this is something near the "impossible". 

Try it.. Change the function to connect with the server to use a persistent one. 

Eg.Using PHP you have the "mysql_connect" and the alternative is "mysql_pconnect". 

You can find more information about this alternative function here: 
http://php.net/manual/en/function.mysql-pconnect.php 

It is simples, when you try to connect, the system looks for a previous "connection", if it exists, thats the magic, no one is created! But the same old connection is used. 

Hope helped in any way...