Bug #61042 Can not connect to mySQL server from windows CE using .NET connector
Submitted: 3 May 2011 16:38 Modified: 3 Sep 2011 0:57
Reporter: Peter Micuch Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:6.2.4 (any) OS:Windows (Win Server 2008)
Assigned to: Assigned Account CPU Architecture:Any
Tags: Compact Framework, Unable to connect, unresolved IP address, widnows CE

[3 May 2011 16:38] Peter Micuch
Description:
In our production network there is windows CE device that is using Connector .NET and it is not able to make connection to the mySQL server. If I try to connect using my laptop then the connection works. I have also TCP/IP custom server running on the same machine where also mySQL server resides and the client on my windows CE device is able to make TCP/IP connection to that server. It means that TCP connection works fine and I can ping the server without any issues (I would rule out any routing issues). Also when the windows CE device is connected (synchronized) with my laptop I am able to connect to mySQL server. 

The topology of the network is as follows:

Server A (running mySQL server 5.1.42) (3 LAN cards)
1LAN - 192.168.99.x 
2LAN - 199.199.100.x (connection to Server B)
3LAN - 213.81.x.x (connection to internet via cisco router)
Server B (running other mySQL server 5.5.11) (3 LAN cards)
1LAN - 10.236.16.x (separated private network)
2LAN - 199.199.100.x (connection to Server A)
3LAN - not connected

There is Wireless network running on 192.168.99.x where our windows CE devices are connected. Even if we tried to use newer version of mySQL server (by connecting to Server B instead of A) we got the same error 'Unable to connect to any of the specified MySQL hosts'. Again, the laptop and desktop PCs were able to connect without issues. I suspect this is only a problem of .NET compact framework version of mySQL connector.
One more thing to mention. As a workaround we installed mySQL server on another machine (let' call it server C) and we connected this one to 192.168.99.x and the windows CE device is able to connect to that mySQL server. The problem seems to be in the cross connection between Server A and Server B, but we are not able to explain what is going on there. It was running fine until recently when we have restarted all the servers. The biggest mystery is the fact, that the regular .NET application is able to connect but the .NET compact framework is not able to connect to mySQL server (even though ping and TCP/IP connection works just fine).

Do you have any suggestions what could be the difference between compact framework version of .NET connector and the regular one? (btw, I was using connectors 6.2.2 and also 6.2.4 both with the same result)  

How to repeat:
see above, but it will be quite difficult to get the same setup.
[4 May 2011 9:36] Peter Micuch
I have had today a debug session on the system again. When my device is trying to connect to MySQL server running on the server A it gets into the method StreamCreator.GetStream(uint timeout) where I found this code snippet:

IPHostEntry ipHE = GetHostEntry(dnsHosts[index]);
foreach (IPAddress address in ipHE.AddressList)
{
    // MySQL doesn't currently support IPv6 addresses
    if (address.AddressFamily == AddressFamily.InterNetworkV6)
        continue;
    stream = CreateSocketStream(address, false);
    if (stream != null) break;
}

The problem is, that ipHE.AddressList contains all 3 addresses of the server in this order: 199.199.100.3, 213.81.x.x, 192.168.99.7. So the actual address that was used in the connection string (192.168.99.7) is at the last place. When I skipped first two addresses in the foreach loop, the connection was established! Why is not the IP address that was used in the connection string used in the first place? Please have a look to this issue and if possible, provide fix in the next release.
[5 May 2011 15:51] Peter Micuch
As a temporary fix I modified the method ParseIPAddress so that it actually tries to parse given IP address also in case of running in compact framework environment. Since in my case I do not expect to get anything else than real IP address it is OK to use the "try catch" approach, but should be reconsidered for other environments.

private IPHostEntry ParseIPAddress(string hostname)
{
    IPHostEntry ipHE = null;
    IPAddress addr;
#if !CF
    if (IPAddress.TryParse(hostname, out addr))
    {
#else
    try
    {
        addr = IPAddress.Parse(hostname);
#endif
        ipHE = new IPHostEntry();
        ipHE.AddressList = new IPAddress[1];
        ipHE.AddressList[0] = addr;
    }
#if CF
    catch(FormatException)
    {
        //Host name is not a valid IP address, kindly return null
    }
#endif
    return ipHE;
}
[6 May 2011 8:25] Peter Micuch
I change the version to 6.2.4 as this concerns Connector version and not version of MySQL server itself.

I also changed Severity to critical, since this problem will be visible to everyone that is using MySQL on the server with multiple network cards (multiple IP addresses).
[13 May 2011 14:48] Valeriy Kravchuk
Please, check if this problem happens with a newer version of Connector/Net, 6.3.6 or 6.4.0.
[18 May 2011 12:27] Peter Micuch
While installing 6.3.6 on Win7 I get an error and the installer exits. But if the code of the problematic method haven't changed since older version, I guess the problem will as well be visible in 6.3.6.
[31 May 2011 20:41] Julio Casal
Please provide a small sample Visual Studio project where this problem is easily reproduced so that I can take a look at it and work on a solution.
[1 Jul 2011 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".
[3 Sep 2011 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".