在Linux上使用Mono连接MySQL数据库

我的测试平台是Ubuntu+Mono1.2.4+MySQL5+Connector/Net 5.1.4,其中Mono和MySQL都是使用apt-get方式安装很方便。MySQL Connector/Net 5.1.4从http://dev.mysql.com/downloads/connector/net/5.1.html 下载Windows Source and Binaries, no installer (ZIP)解压缩得到MySql.Data.dll。
使用gacutil -i MySql.Data.dll把MySql.Data.dll安装到Mono的全局组件目录下。
具体内容参考http://www.mono-project.com/MySQL的例子。
特别提出来的是我第一次编译运行的时候出现这样的错误提示:
MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts
in <0x006c5> MySql.Data.MySqlClient.NativeDriver:Open ()
in <0x00027> MySql.Data.MySqlClient.Driver:Create (MySql.Data.MySqlClient.MySqlConnectionString settings)
in <0x0008d> MySql.Data.MySqlClient.MySqlPool:CreateNewPooledConnection ()
in <0x001b0> MySql.Data.MySqlClient.MySqlPool:GetPooledConnection ()
in <0x0003f> MySql.Data.MySqlClient.MySqlPool:GetConnection ()
in <0x000e5> MySql.Data.MySqlClient.MySqlPoolManager:GetConnection (MySql.Data.MySqlClient.MySqlConnectionString settings)
in <0x00050> MySql.Data.MySqlClient.MySqlConnection:Open ()

不思不得其解,后来找到一个国外的论坛,得到这样的提示:
The most likely cause of this is that MySQL is not configured to accept tcp/ip connections.
See here http://dev.mysql.com/doc/mysql/en/can-not-connect-to-server.html
make sure your /etc/mysql/my.cnf file does not have the "skip-networking" feature enabled. This is the default in some installations and disables tcp/ip connections.
根据提示我把my.cnf中的
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           = 172.22.60.126
bind-address注释掉,然后重新启动mysql服务器程序sudo /etc/init.d/mysql restart就可以正常连接查询了。
GOOD Luck!
posted @ 2008-01-22 13:40 天下³ 阅读(370) 评论(16)  编辑 收藏 所属分类: Midapex ORM Library

  回复  引用  查看    
#1楼 2008-04-12 16:41 | egmkang      
lz.
我现在想在linux上面用mono连接mysql数据库,但是一直报错:Unable to connect to any of the specified MySQL hosts.
System.Exception: Unable to connect to any of the specified MySQL hosts.
at Discuz.Install.install.CheckConnection () [0x00000]
at Discuz.Install.install.ClearDBInfo_Click (System.Object sender, System.EventArgs e) [0x00000]
at (wrapper delegate-invoke) System.MulticastDelegate:invoke_void_object_EventArgs (object,System.EventArgs)
at System.Web.UI.WebControls.Button.OnClick (System.EventArgs e) [0x00000]
at System.Web.UI.WebControls.Button.RaisePostBackEvent (System.String eventArgument) [0x00000]
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent (System.String eventArgument) [0x00000]
at System.Web.UI.Page.RaisePostBackEvent (IPostBackEventHandler sourceControl, System.String eventArgument) [0x00000]
at System.Web.UI.Page.RaisePostBackEvents () [0x00000]
at System.Web.UI.Page.ProcessRaiseEvents () [0x00000]
at System.Web.UI.Page.InternalProcessRequest () [0x00000]
at System.Web.UI.Page.ProcessRequest (System.Web.HttpContext context) [0x00000]

不知道你能解决不?或是还需要什么信息?我均提供给你.希望你能帮我把问题解决,谢谢
  回复  引用  查看    
#2楼 [楼主]2008-04-12 17:10 | Midapex Village      
我在上面提到了解决的方法,你试试看看,可能也是这个原因。
  回复  引用    
#3楼 2008-04-16 22:07 | Amy Liu [未注册用户]
楼主你好
我用mono连接mysql的时候出现
1.WARNING **: The class System.Data.Common.DbConnection could not be loaded, used in System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

2.TestExample_mysql.cs(20,20): error CS0246: The type or namespace name `MySqlConnection' could not be found. Are you missing a using directive or an assembly reference?

我的程序是参考 http://www.mono-project.com/MySQL
#mcs TestExample_mysql.cs -r:System.Data.dll -r:/home/amy/mono/related_software/MySql.Data.dll

using System;
using System.Data;
using MySql.Data.MySqlClient;

public class Test
{
public static void Main(string[] args)
{
string connectionString=
"Server=localhost;"+
"Database=sysinfo;"+
"User ID=root;"+
"Password=;"+
"Pooling=false";

IDbConnection dbcon;
dbcon = new MySqlConnection(connectionString);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();

string sql = "SELECT firstname, lastname " +
"FROM employee";
dbcmd.CommandText = sql;

IDataReader reader = dbcmd.ExecuteReader();
while(reader.Read()) {
string FirstName = (string) reader["firstname"];
string LastName = (string) reader["lastname"];
Console.WriteLine("Name: " +
FirstName + " " + LastName);
}
// clean up
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
}
}

第一个错误,是因为我的mono没安装好吗?
第二个,我gacutil -i MySql.Data.dll,并且using的

呵呵,谢谢了!







  回复  引用  查看    
#4楼 [楼主]2008-04-17 00:21 | Midapex Village      
你确定你的MySql.Data.dll放在/home/amy/mono/related_software/目录下吗?一般的默认安装是不会放在这里的。请查看。
  回复  引用    
#5楼 2008-04-17 10:35 | Amy Liu [未注册用户]
呵呵,谢谢楼主
我下载的MySql.Data.dll是放在/home/amy/mono/related_software/的
默认安装在 /usr/local/lib/mono/gac 下
#mcs TestExample_mysql.cs -r:System.Data.dll -r:/usr/local/lib/mono/gac/MySql.Data/5.1.5.0__c5687fc88969c44d/MySql.Data.dll

还是出现同样的问题
  回复  引用  查看    
#6楼 [楼主]2008-04-17 12:33 | Midapex Village      
晕了,你需要用的是Mono自带的MySQl驱动,不要使用网上下的驱动,两者不兼容。
  回复  引用  查看    
#7楼 [楼主]2008-04-17 12:35 | Midapex Village      
详细的方法请参看:http://www.cnblogs.com/dyj057/archive/2008/01/24/1051072.html中的Mono连接是怎么做的。
  回复  引用    
#8楼 2008-04-17 19:26 | Amy Liu [未注册用户]
呵呵,好的,谢谢了
  回复  引用    
#9楼 2008-04-17 20:47 | Amy Liu [未注册用户]
我看了楼主写的Smart ORM.NET的Mono版本
mono只自带 sqlite 的驱动吧,
在SQLiteSession.cs中 using Mono.Data.SqliteClient;
而在MySqlSession.cs中是 using MySql.Data.MySqlClient;

好像mono不带mysql驱动

楼主在文章中写到“MySQL Connector/Net 5.1.4从http://dev.mysql.com/downloads/connector/net/5.1.html 下载Windows Source and Binaries, no installer (ZIP)解压缩得到MySql.Data.dll”

是不是我漏掉了什么步骤?谢谢了


  回复  引用    
#10楼 2008-04-17 21:14 | Amy Liu [未注册用户]
是不是因为没有安装MonoDevelop,像vs2005一样,还要在工程中,引用?
在.cs文件中引用就可以了吧?
  回复  引用  查看    
#11楼 [楼主]2008-04-17 22:35 | 天下叁      
Mono自带了驱动的,就用那个自带的驱动,我记得很清楚,它里面有个MySQL的驱动。如果你的没有,说明没有装全。
  回复  引用  查看    
#12楼 [楼主]2008-04-17 22:36 | 天下叁      
如果是在Linux中使用,不能用http://dev.mysql.com/downloads/connector/net/5.1.html下载的驱动。Windows可以用。
  回复  引用    
#13楼 2008-04-18 15:13 | Amy Liu [未注册用户]
呵呵,谢谢了
我重新安装mono看看
  回复  引用  查看    
#14楼 2008-04-18 15:36 | egmkang      
lz是说在Linux下面不能用Mysql给的那个.NET 驱动连接mysql????
得用mono的那个sqlite驱动?
  回复  引用  查看    
#15楼 [楼主]2008-04-18 15:44 | 天下叁      
是的,它那里面有很多平台调用的东西,不能保证在Linux上运行。
  回复  引用    
#16楼 2008-04-19 15:48 | Amy Liu [未注册用户]
呵呵,问题解决,using ByteFX.Data.MySqlClient;
谢谢各位朋友!

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      


相关链接: