Bug #6580 server connection errors
Submitted: 11 Nov 2004 15:45 Modified: 12 Nov 2004 0:26
Reporter: David Edwards Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S1 (Critical)
Version:4.1.7 OS:Windows (Win32 (XP, 2003))
Assigned to: CPU Architecture:Any

[11 Nov 2004 15:45] David Edwards
Description:
The mysql server intermittently fails to allow connections from clients, with no obvious pattern to when or why connections are refused. The clients are local to the server machine, and the problem occurs with both the MySQL C API and ODBC (using the .NET ODBC classes). When using MyODBC 3.51.10 the error returned is HYT00, though it fails before the login timeout should have expired.

How to repeat:
Easiest method of reproducing this is to do a simple loop of connecting to and disconnecting from the server:

int main(int argc, char* argv[])
{
    for (int i=0; i<10000; i++)
    {
        MYSQL* mysql = NULL;
        bool b_ok = true;
        if (b_ok)
        {
            mysql = mysql_init(NULL);
            b_ok &= (NULL != mysql);
        }
        if (b_ok)
        {
            b_ok &= (NULL != mysql_real_connect(mysql,NULL,NULL,NULL,NULL,MYSQL_PORT,NULL,0));
        }
        if (!b_ok)
        {
            printf("%d: failed to connect to mysql server\n",i);
        }
        mysql_close(mysql);
    }
    return 0;
}

Note that behaviour (when it fails) is different on different machines.
[12 Nov 2004 0:26] MySQL Verification Team
I tested your sample and the error I got around 4000 connect/closing.
However this a Windows behavior, please read:

http://support.microsoft.com/kb/q196271/

Subject: Unable to Connect TCP Ports above 5000

Thank you for the bug report.
[12 Nov 2004 14:17] David Edwards
I'm using port 3306 every time, not ports above 5000, so the max port issue on Windows doesn't apply - and implementing the fix in the MS article doesn't solve the problem. When using MyODBC 3.51.10 the error is:

[HYT00] [MySQL][ODBC 3.51 Driver]Can't connect to MySQL server on 'localhost' (10048)

If the 10048 relates to a socket error code, this would be WSAEADDRINUSE rather than the WSAENOBUFS mentioned in Q196271.
[12 Nov 2004 16:40] David Edwards
OK, sorry, it's the Win32 delay in releasing a port after closing it that's the problem. Thanks.
[12 Mar 2009 21:17] yves sucaet
I'm experiencing the same problem, though with PHP. Here's the script I use to test:

<?php
$p = 0;
$n = 0;
for ($i = 0; $i < 120; $i++) {
	$conn = mysql_connect("www.metnetdb.org", "metnet3_editor", "my");
	if ($conn) {
		echo "Success!\n";
		mysql_close($conn);
		$p++;
	} else {
		echo "Failed test $i\n";
		$n++;
	}
	sleep(1);
}
echo "\n$p successes, $n failures\n";
?>

The results are unpredictable. Out of 120 attempts, maybe 114 will be successful, 6 will fail. 

I'm using WinXP Pro x64 edition and PHP 5.2.6
[17 Apr 2011 7:50] MySQL Verification Team
folks seeing problems could first read/implement
http://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html#can-not-connect-to-s...

that might solve it.