我开发了ASP.Net MVC
使用 MySql
的应用程序作为数据库,它在开发机器上运行良好。我将其部署到服务器上后,无法连接到数据库并提供以下错误。
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
我已经有 MySql
和mysql-connector-net
安装在服务器上。
但是,当我启动 Workbench
时收到警告
Unsupported operating system
block 引用>You are using Workbench on unsupported operating system. While it may work for you just fine, it wasn't designed to run on your platform. Please keep in mind if you run into problems.
我需要以某种方式修改连接字符串吗?
目前采用以下格式。
<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()); }
网络配置:
<?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=\"Web\" /optionInfer+" /> </compilers> </system.codedom> </configuration> <!--ProjectGuid: {9B0CBEB4-10D9-4FBC-BEBA-3EAEC7D033D0}-->
最佳答案
Workbench 只是一个用于连接和管理 MySQL 服务器的 GUI。我不认为这是你的问题(...但可能是...在确认代码端问题不是一个因素后我会回到这个问题)。
您上面发布的错误是指 SQL Server 表明您在服务器端存在某种配置问题。
作为引用,这是使用 MySQL 和“连接器”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 和我的 web.config 之间存在一些差异但是我的网站非常旧(MVC3/MySQL Connector 6.8.3),所以事情可能不会现在工作一样。我还使用自己的 UnitOfWork/Dapper 集成来访问数据库。我还使用身份验证/成员(member)身份,而您似乎没有这样做。
无论如何,我还注意到 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
但在本地我不需要。不过这已经是很久以前的事了!
希望有帮助!
关于mysql - MVC 应用程序在部署后无法连接到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58551454/