Unable to connect to MySQL database with C# and Mono
First off; I've been an avid reader of SO for a good while now. This is my first question. I'm surprised it's going to be related to databases! Note that I think the problem might somehow be specific to my setup so I'm not sure if you'll be able to help me out, but I thought I'd give it a shot!
Problem:
So, I'm completely new to databases. I'm using C# with Mono (v2.6.7) and MonoDevelop (v2.4) for Mac (OS X 10.6.4). Mainly because of the simple setup, I want to use XAMPP for playing around. Now, I wish to connect to my test database using MySQL .NET Connector (v6.3.5), as recommended by Mono.
I have followed the recommended on Mono's page (I've also reviewed MySQL's own installation guide) and I have installed the necessary files to the GAC. I have added all the necessary references to my project and my code compiles fine. I'm still not able to connect to my XAMPP database through code.
The interesting thing is that I am able to access the database by the Terminal (./mysql -u tester -p
and doing use test
) without any problems. Also, mysql_client_test
also runs fine and everything I can think of indicates that the server is operative.
Here's my code:
string connectionString = "Server=localhost;User ID=tester;Password=******;Database=test;Port=3306;Pooling=false";
MySqlConnection connection = new MySqlConnection( connectionString );
try
{
Console.WriteLine( "Trying to open database connection ..." );
connection.Open();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
connection.Close();
Here's the exception I get when I try to connect from code:
Trying to open database connection ... MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts. ---> System.Net.Sockets.SocketException: Connection refused at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in :0 at System.Net.Sockets.Socket+Worker.Connect () [0x00000] in :0 --- End of inner exception stack trace --- at MySql.Data.MySqlClient.NativeDriver.Open () [0x00000] in :0 at MySql.Data.MySqlClient.Driver.Open () [0x00000] in :0 at MySql.Data.MySqlClient.Driver.Create (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) [0x00000] in :0 at MySql.Data.MySqlClient.MySqlConnection.Open () [0x00000] in :0
Any input would be greatly appreciated. Also, if you have any suggestions on an easier setup开发者_高级运维 for playing around with MySQL - that would be greatly appreciated as well! Thanks in advance!
Edits: (Information requested by Phil Hunt)
netstat -na | grep LIST
tcp4 0 0 *.21 *.* LISTEN
tcp46 0 0 *.80 *.* LISTEN
tcp4 0 0 *.63912 *.* LISTEN
tcp4 0 0 127.0.0.1.26164 *.* LISTEN
tcp4 0 0 *.17500 *.* LISTEN
tcp4 0 0 127.0.0.1.631 *.* LISTEN
tcp6 0 0 ::1.631 *.* LISTEN
Found the problem!
It was basically a problem with how XAMPP sets up the MySQL Server. For some reason, there were two lines, skip-networking
in .../XAMPP/xamppfiles/etc/my.cnf
. Below is a snippet from the file, showing the lines I'm talking about. It's an "out-of-the-box bug" with the Mac version, I guess. Commenting out both of the skip-networking
lines fixes the problem!
From my.cnf
:
# commented in by xampp security
#skip-networking
#skip-networking <- This line was left uncommented by default, for some reason.
server-id = 1
精彩评论