title: Pomelo MySqlException: Unable to connect to any of the specified MySQL hosts
body: <p>I am working on Blazor Server-Side project that needs to run in an Ubuntu Linux 18.04LTS server. (NOTE: This project is not using Web Assembly.)</p>
  
  <p>I am using C# and the <code>Pomelo.EntityFrameWorkCode.MySql</code> nuget package (v3.1.1) to connect to a MySQL database running on the Linux server.</p>
  
  <p>When I run the app locally on my Windows development system, everything works correctly. When I publish the files to the Linux server, it works about 5% of the time - the remainder I get an error message</p>
  
  <blockquote>
    <p>Pomelo MySqlException: Unable to connect to any of the specified MySQL hosts</p>
  </blockquote>
  
  <p>I am connecting to the same MySQL database on both my Windows system and the Linux server. The MySQL is running on Linux, correct ports are open, etc.</p>
  
  <p>More detailed error message is:</p>
  
  <blockquote>
    <p>An unhandled exception occurred while processing the request.
    MySqlException: Unable to connect to any of the specified MySQL hosts.
    MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs, line 442</p>
    
    <p>InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseMySql' call.
    Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.Execute(TState state, Func operation, Func> verifySucceeded)</p>
    
    <p>MySqlException: Unable to connect to any of the specified MySQL hosts.
       MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in 
    C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs
       MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, IOBehavior ioBehavior, CancellationToken cancellationToken) in > C:\projects\mysqlconnector\src\MySqlConnector\Core\ConnectionPool.cs
       MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, IOBehavior ioBehavior, CancellationToken cancellationToken) in > C:\projects\mysqlconnector\src\MySqlConnector\Core\ConnectionPool.cs
       System.Threading.Tasks.ValueTask.get_Result()
     System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable+ConfiguredValueTaskAwaiter.GetResult()
       MySql.Data.MySqlClient.MySqlConnection.CreateSessionAsync(ConnectionPool pool, Nullable ioBehavior, CancellationToken cancellationToken) in 
    C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs
    System.Threading.Tasks.ValueTask.get_Result()</p>
    
    <blockquote>
      <p>System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable+ConfiguredValueTaskAwaiter.GetResult()
          MySql.Data.MySqlClient.MySqlConnection.OpenAsync(Nullable ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs
         MySql.Data.MySqlClient.MySqlConnection.Open() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs
      Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(bool errorsExpected)
          Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(bool errorsExpected)</p>
    </blockquote>
    
    <p>Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlRelationalConnection.Open(bool errorsExpected)</p>
    
    <p>Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
    Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable+Enumerator.InitializeReader(DbContext _, bool result)
    Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.Execute(TState state, Func operation, Func> verifySucceeded)</p>
  </blockquote>
  
  <p>The Configuration in the Startup class looks like:</p>
  
  <pre><code>services.AddDbContext&lt;LabDataDbContext&gt;(options =&gt; {             
      options.UseMySql(Configuration.GetConnectionString("LabDbConnection"));
          options.EnableDetailedErrors();
  });
  </code></pre>
  
  <p>My Connection String is:</p>
  
  <pre><code>"server=server.grafton.internal;port=3306;database=labdb;user=******;password=******;charset=utf8"
  </code></pre>
  
  <p>The interesting thing is I can use MySQL Workbench and connect to the database from either my Windows dev system, or the Linux server using exactly the same credentials.</p>
  
  <p>This application is designed to be run in house only and the server running the app is not visible on the Internet, so I am not too concerned about security issues (if someone wants to know what a bunch of autistic kids have as equipment and activities, they can have it!!)</p>
  
  <p>While trying to isolate the error, I have completely removed and redeployed the app to a different linux server on the network with the same result, but as I have stated earlier, about 5% of the time, the app works as expected on the linux server.</p>
  
  <p>Other pages in the app that do NOT use a database are working correctly, so I do not think it is an issue with the web server (NGinx as a reverse proxy to Kestrel).</p>
  
  <p>I also want  to stress that the app works about 5% of the time running on linux, and 100% of the time running on Windows - both connecting to the same MySQL server running on linux.</p>
  
  <p>If more code samples are wanted, the app is on Github at <a href="https://github.com/SteveTeece/Lab" rel="nofollow noreferrer">https://github.com/SteveTeece/Lab</a>. </p>
  
  <p>This one has got me stumped. Any ideas on how to fix this and get it running on the Linux server is greatly appreciated.</p>
  
  <p>Thanks</p>