部署后,MVC应用程序无法连接到MySQL

时间:2019-10-25 02:36:16

标签: mysql asp.net-mvc iis

我已经开发了ASP.Net MVC应用程序,该应用程序使用MySql作为数据库,并且在开发机器上运行良好。将其部署在服务器上后,它无法连接到数据库,并提供以下错误。

  

建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (提供者:SQL网络接口,错误:26-指定服务器/实例时出错)

我已经在服务器上安装了MySqlmysql-connector-net

但是,当我启动Workbench

时会收到警告
  

不受支持的操作系统

     
    

您正在不受支持的操作系统上使用Workbench。虽然它可能对您来说很好,但它并非旨在在您的平台上运行。如果遇到问题,请记住。

  

我需要以某种方式修改连接字符串吗?

当前采用以下格式。

<add name="main" connectionString="server=127.0.0.1;port=1234;uid=mysqluid;pwd=mysqlpwd;database=mydbname;"/>

这是我与数据库进行交互的示例代码。

internal static DataSet GetData(string sql, List<MySqlParameter> parameters = null, CommandType commandType = CommandType.Text)
        {
            var ds = new DataSet();
            using (var connection = GetConnection())
            {
                using (var command = new MySqlCommand(sql, connection))
                {
                    connection.Open();
                    command.CommandType = commandType;
                    if(parameters != null)
                    {
                        AddCommandParameters(command, parameters);
                    }
                    var adapter = new MySqlDataAdapter(command);
                    adapter.Fill(ds);
                }
                connection.Close();
            }
            return ds;
        }

private static void AddCommandParameters(MySqlCommand command, List<MySqlParameter> parameters)
{
    foreach (var parameter in parameters)
    {
        command.Parameters.AddWithValue(parameter.ParameterName, parameter.Value);
    }
}


  private static MySqlConnection GetConnection()
{
    return new MySqlConnection(ConfigurationManager.ConnectionStrings["main"].ToString());
}

Web配置:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>

    <add name="main" connectionString="server=127.0.0.1;port=3306;uid=XXX;pwd=XXX;database=XXX;" providerName="MySql.Data.MySqlClient"/>
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="ClientContentPath" value="C:\_App_Root\ClientContents\"/>
  </appSettings>
  <system.web>
    <customErrors mode="Off">
    </customErrors>

    <authentication mode="None" />
    <compilation targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
  </system.web>
  <system.webServer>

    <validation validateIntegratedModeConfiguration="false" />
    <directoryBrowse enabled="false" />
        <defaultDocument>
            <files>
                <clear />
                <add value="Default.asp" />
                <add value="index.html" />
                <add value="Default.htm" />
                <add value="index.htm" />
                <add value="iisstart.htm" />
                <add value="default.aspx" />
            </files>
        </defaultDocument>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>
<!--ProjectGuid: {9B0CBEB4-10D9-4FBC-BEBA-3EAEC7D033D0}-->

1 个答案:

答案 0 :(得分:1)

Workbench只是用于连接和管理MySQL服务器的GUI。我不认为这是您的问题(...但可能是...在确认代码方面的问题不是一个因素之后,我会再说一遍。)

您在上面发布的错误是指SQL Server,建议您在服务器端遇到某种配置问题。

作为参考,这是使用MySQL和'connector'nuget软件包的有效连接字符串。

<add name="[your_connection_name]" 
    connectionString="Server=[my_db_ip];Database=[your_db_name];Uid=[your_username];Pwd=[your_password];" 
    providerName="MySql.Data.MySqlClient" />

如何在本地拨打数据库?我们可以看到一些代码吗?

要检查的初始内容:

  • 您是否100%确定要连接到本地MySQL服务器并可以读取/更新数据?
  • 实时服务器接受127.0.0.1上的连接(例如有时可以是localhost
  • 检查web.config文件中是否正确设置了MySQL。

根据您的web.config更新

我已经看过了,而您的web.config和我的一个之间有一些区别,但是我的网站很旧(MVC3 / MySQL Connector 6.8.3),所以事情可能不会现在工作一样。我还使用自己的UnitOfWork / Dapper集成来访问数据库。我也使用Auth / Membership,但您似乎没有。

无论如何,我还注意到defaultConnectionFactory的条目似乎是指local的连接。我会检查一下。

您还可以在提供程序中使用System.Data.SqlClient(SQL Server)引用(这可能是实时服务器正在使用的引用。这可能应该是这样的(对于您的MySQL版本):

<entityFramework>
    <defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
    <providers>
        <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    </providers> 
</entityFramework>

可能的帮助吗? Entity Framework defaultConnectionFactory for MySQL

这里(供参考)是我的部分内容:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      ...
      <dependentAssembly>
        <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.8.3.0" newVersion="6.8.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="MySql.Data.Entity" publicKeyToken="c5687fc88969c44d" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.8.3.0" newVersion="6.7.5.0" />
      </dependentAssembly>
      ...
    </assemblyBinding>
  </runtime>

还:

我有:                                                    

我曾经有过一个案例,即实时服务器需要DbProviderFactories,但在本地却不需要。不过这是很久以前的事了!

希望有帮助!

相关问题