My head hurts. I'm porting a .NET application to Linux/Mono, and everything was working out fine, except the connectivity to the MySql database.  I don't have experience as an administrator, so I had a machine set up, I installed mono (after so many time I still have to compile stuff on Linux? Come on!), the application worked, but I had the following issues:

  • I could connect to mysql on the production server using
    mysql -u user -p
  • The application couldn't connect to mysql on the production server (using localhost)
  • The application could connect to mysql on another machines
  • The same application on another machines could connect to mysql on the production server

At this time I didn't know if the problem was with mono/application or with mysql configuration. So I run a Python script that connected to localhost and it worked fine... Then I downloaded the MySQL .NET provider sources and started to break some rocks (Portuguese expression).

The provider only said something like:

Unable to connect to any of the specified MySQL hosts.
---> System.Exception:
Exception of type 'System.Exception' was thrown.

Very usefull information as you can see. After digging up, this exception was being thrown like this:

C#:
  1. StreamCreator sc = new StreamCreator(Settings.Server, Settings.Port, pipeName);
  2. baseStream = sc.GetStream(Settings.ConnectionTimeout);
  3. if (baseStream == null)
  4.     throw new Exception();

Really classy material. The actual exception was being thrown by:

C#:
  1. Socket socket = ...
  2. try {
  3.      socket.EndConnect(ias);
  4. } catch (Exception) {
  5.      socket.Close();
  6.      return null;
  7. }

Oh yeah! What more can I say?

After patching the mysql .net provider with some logging, I had the real exception:

System.Net.Sockets.SocketException: Connection refused

The I did something that I should have done a long time ago:

# telnet localhost 3306
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

Well, it's proved that it wasn't an application / mono problem. Why did it work with direct mysql on the console and Python? Maybe because they connect using a pipe, instead of the TCP stream that the provider uses, I really don't know.

And wouldn't know how to fix this, but my colleague Paulo Pires came and saved the day. It seams that mysql was configured to use a binding to local named address, and because of that refused connections to localhost.

Related Posts

6 Responses to “Long Adventure on Mono/MySQL Land!”

  1. Alcides Says:

    Logo ao mesmo tempo no Prt.sc estava o post da Andreia Gaita em como usar o apt-get para instalar o moonlight: http://blog.worldofcoding.com/2009/02/moonlight-shining-on-ubuntu.html

    Logo o mono tb tem de dar:

    sh# sudo apt-get install mono mono-gmcs mono-gac mono-utils monodevelop

  2. Pedro Santos Says:

    Sim, o problema é se a versão que pretendes ainda não está disponível em pacotes, ou então estás numa distro que não tem pacotes…

    :)

  3. Carlos Rodrigues Says:

    “I’m porting a .NET application to Linux/Mono”

    But… But… I thought there was no need to port stuff between MS’ .NET and Mono…

    Sorry, couldn’t resist. :)

  4. Pedro Santos Says:

    You’d be surprised Carlos. :)

    All the major problems I’m having are related with linux configurations.

  5. Paulo Pires Says:

    The problem is that you’re alone migrating an environment and not just an application code to a platform which administration, apparently you’re not much aware of. As you could see it was pretty easy to solve and no code changes were needed.

    Now, getting to a problem is most of the time trickier than finding its solution :-)

    Btw, do you want me to send you an Ubuntu CD for installation? :-D

  6. Nuno Silva Says:

    Touché!

Leave a Reply