在Godaddy的Windows主机上使用MySql.Data.dll连接数据库

by shinichi_wtn 2011-02-20 00:21

由于Godaddy的Windows主机Deluxe方案只提供了2个200M的SQL Server 数据库,对于需要建立多个网站来说不是很方便。值得高兴的是,Godaddy提供了25个1G容量的MySQL数据库。而目前许多网站,或者自己建站都会考虑对不同的数据库的支持,BlogEngine.NET就是很好的例子(它几乎支持任何数据库,同时提供了XML作为数据源的支持),我们在编写网站的时候,也经常会使用ASP.NET中的System.Data.Common提供的工厂模式来连接数据库。

错误详情

当我们在本机上通过MySql.Data.dll能够正常运行网站,上传到Godaddy就会出现权限问题,提示如下错误:

Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

解决方案 1

遇到问题后,当然是到网上搜索解决方案,有意思的是一篇文章《MySQL vs. GoDaddy: The ASP.Net Work-Around》里说道Godaddy不让你轻松使用MySQL的,毕竟MySql.Data中部分功能会涉及到High Trust Level,而Godaddy限制了Trust Level为中级(Medium),所以通过MySql.Data来连接数据库就会报权限错误。

当然,知道了缘由,自然就有解决方案了。之前提到的那篇文章给出了一个比较有技术含量的方法,即重新编译MySql.Data.dll,让其能够在Medium trust environment下正常工作,并提供了一个已经编译好可以直接使用的MySql.Data.dll的文件供大家下载。虽然可行,但是不建议使用这个方案。

这个解决方法有很大的局限性,重新编译的MySql.Data.dll丧失了许多功能特性,同时也失去了对数据工厂的支持。我们只能在程序中显式指定前缀为MySql的数据库操作类来操作数据库(如MySqlConnection,MySqlCommand等等,下面代码显示了这样的局限性)

DataTable table = new DataTable();
string connectionString = "******";
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
    connection.Open();
    MySqlCommand command = connection.CreateCommand();
    command.CommandText = "******";
    MySqlDataAdapter adapter = new MySqlDataAdapter();
    adapter.SelectCommand = command;
    adapter.Fill(table);
}

一旦我们的WEB程序没有指定使用的数据库,而是在运行时由工厂模式从web.config读取connectionString中的providerName来确定使用的数据库,则无能为力了。如下的代码使用了工厂模式来实现数据库操作

DataTable table = new DataTable();
ConnectionStringSettings connectionSetting = ConfigurationManager.ConnectionStrings["BaikeConn"];
string connectString = connectionSetting.ConnectionString;
string providerName = connectionSetting.ProviderName;
DbProviderFactory dbFactory = DbProviderFactories.GetFactory(providerName);
using (DbConnection connection = dbFactory.CreateConnection())
{
    connection.Open();
    DbCommand command = connection.CreateCommand();
    command.CommandText = "******";
    DbDataAdapter adapter  = dbFactory.CreateDataAdapter();
    adapter.SelectCommand = command;
    adapter.Fill(table);
}

其中Web.config的ConnectionString设置如下

<connectionStrings>
  <add name="myConn" connectionString="user id=myUserid;password=myPassword;Database=myDatabase;Server=localhost;Connect Timeout=30" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>

解决方案 2

这里给出一个更好的解决方案,完全没有必要重新编译MySql.Data.dll,而是换回较早版本的MySql.Connector.NET,比如6.0.4和6.0.7版本。它们在使用的过程中不会有权限问题,加上是官方原版,所以功能也不会有任何缺失。点击<<-这里->>下载6.0.7版本的MySql.Data.dll,将这个文件拷贝至网站的BIN目录。

使用的时候需要在web.config中加上如下一段,清除已有的数据库Provider,使用我们自己提供的MySql.Data.dll(因为Godaddy的服务器上已经安装了更老的并且有权限问题的MySql.Connector.NET,有点狠吧……感谢很好很强大的web.config,几乎任何配置都能改)

<system.data>
    
  <
DbProviderFactories>
    <
clear/>
    <
add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.0.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
  </
DbProviderFactories>
</
system.data>

其他的配置就没有任何不同了,尽情在Godaddy上使用MySQL吧。测试网址http://www.bnubaike.cn

有任何疑问,欢迎留言!

Comments (3) -

catman
9/5/2011 9:06:47 AM #

方法很不错,把 MySql 的数据库相关操作,全部换成工厂模式后,Godaddy 的 MySql 部分是没问题了。

但又发现了一个严重的问题,我的程序使用了 theme,而 Godaddy 是 Medium 信任模式,这种模式下 theme 不允许使用,会产生 Security Exception, 查了好久才知道是 theme 引起的错误,虽然做了很多努力,但看来还是要不得不放弃 Godaddy 了。

Reply

shinichi_wtn
9/5/2011 10:55:23 PM #

嗯,Godaddy这样做可能也是为了确保它的服务器更加安全,但的确丧失了不少功能,想用SQLite也不行(System.Data.SQLite.dll也需要High Trust Level),还好MySQL老版本的Connector只使用了Medium Trust Level。

Reply

catman
9/8/2011 9:09:29 AM #

theme 功能解决了,是我的 theme 类库中直接访问 web.config 中某节点的问题,修改代码就可以了。但依然有不少问题:
1.序列化的问题,Medium 模式下,.NET 3.5 只能用 XML 序列化,没法对泛型序列化,我的类比较复杂,没法深度序列化,又懒得写一套序列化程序,考虑升到 .NET 4.0 是否可以解决。
2.大文件上载的问题,没法使用自己的能动态显示上载进度条的控件,依然是 Medium 模式的原因。
3.页面压缩的问题,不支持后台动态压缩页面内容的控件,这个简单,不用这个控件,不压缩就行了。
4.如果用免费空间,会有广告问题,这个当然可以用一些方法去除广告,但我的页面几乎都是 AJAX 页,Godaddy 把每个页面 Ajax 返回的内容都加上了一条广告代码,这个比较难解决,难道要修改 AJAX 的脚本文件来分析返回码?还是交钱租用吧。
5.往外部访问的功能也不能用了,想在后台发个 短信出去是没办法了。
6.目录中的文件没有写权限,写点日志啥的是不行了。

Reply

(仅用于Gavatar)

  Country flag

biuquote
  • Comment
  • Preview
Loading

About

shinichi_wtnI'm Shinichi_wtn

Software Engineering Manager at Microsoft

[More...]


Month List