0

I have developed ASP.Net MVC application that uses MySql as database and it is running fine on the development machine. After I deployed it on the server, it is not able to connect to the database and provides the following error.

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)

I already have MySql and mysql-connector-net installed on the server.

However, I am getting warning when I am launching Workbench

Unsupported operating system

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.

Do I need to modify the connection string somehow?

it is currently in the below format.

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

Here is the sample code of how I am interacting with the database.

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 config:

<?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}-->
Imad
  • 7,126
  • 12
  • 55
  • 112

1 Answers1

0

Workbench is just a GUI to connect to and manage the MySQL server. I don't think that is your issue (...but it could be...I'd come back to that after confirming code-side issues aren't a factor).

The error you posted above is referring to SQL Server suggesting you have some kind of config issue server-side.

For reference, this is a working connection string using MySQL and the 'connector' nuget packages.

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

How do you make a call to your database locally? could we see some code?

Initial things to check:

  • are you 100% sure you are connecting to and can read/update data in your local MySQL server?
  • the live server accepts connections on 127.0.0.1 (sometimes it can be localhost for example)
  • check that MySQL is setup correctly in your web.config file.

update based on your web.config

I've taken a look and comparitively there are a few differences between your web.config and my one but my site is very old (MVC3 / MySQL Connector 6.8.3) so things might not work the same now. I also use my own UnitOfWork/Dapper integration to access the DB. I also use Auth/Membership where as you don't seem to.

Anyway, I've also noticed that entry for defaultConnectionFactory seems to refer to local connection. I'd check this.

You also have System.Data.SqlClient (SQL Server) reference in providers (which could be what the live server is picking up. This should probably be something like this (with your MySQL version):

<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>

Possible help? Entity Framework defaultConnectionFactory for MySQL

Here (for reference) are the sections I have:

<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>

also:

I have:

I did once have a case where the live server required DbProviderFactories but locally I didn't. This was a long time ago though!

Hope that helps!

scgough
  • 5,099
  • 3
  • 30
  • 48
  • Thanks for the answer. I have updated my question to include code snippet. - I am 100% sure I am able to read write data from MySql server on development machine. - Yes, server accepts `127.0.0.1` and I also tried `localhost` that also didn't work. - I am not sure how can I check that `MySql` is correctly configured in `web.config`. Can you please guide. – Imad Oct 26 '19 at 10:16
  • Ok. Can you post your web config as well? Also let us know the mysql packages you’ve installed from your packages.config. I note your connection string has no “providername” which made me think about your webconfig as a possible issue. – scgough Oct 26 '19 at 10:18
  • Yes, previously I didn't have it and it was still working. I have included it after reading your answer but no luck. What part of `web.config` you want me to post? – Imad Oct 26 '19 at 10:26
  • I think best to post all sections (private info redacted obvs) that have reference to MySql. If you used nuget all should be in order but can’t hurt to check. – scgough Oct 26 '19 at 10:27
  • Oh one other thought: does your live server have mysql on the standard port? – scgough Oct 26 '19 at 10:28
  • Yes, the port number is the same as one in the config. I have posted config also in question. – Imad Oct 26 '19 at 10:32
  • Just added some more thoughts. I'll be away from my computer for a while but hope that helps you dig for the answer! – scgough Oct 26 '19 at 10:45
  • Thanks a lot. Removed all the stuff related to entity framework and got it working. I was not using it, might be some auto-generated code. Updated answer helped – Imad Oct 26 '19 at 10:54
  • Great! Glad to help – scgough Oct 26 '19 at 10:59