Search code examples
mysqlentity-frameworkasp.net-coreblazor-server-sidepomelo-entityframeworkcore-mysql

Pomelo MySqlException: Unable to connect to any of the specified MySQL hosts


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

I am using C# and the Pomelo.EntityFrameWorkCode.MySql nuget package (v3.1.1) to connect to a MySQL database running on the Linux server.

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

Pomelo MySqlException: Unable to connect to any of the specified MySQL hosts

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.

More detailed error message is:

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

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)

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

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)

Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlRelationalConnection.Open(bool errorsExpected)

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)

The Configuration in the Startup class looks like:

services.AddDbContext<LabDataDbContext>(options => {             
    options.UseMySql(Configuration.GetConnectionString("LabDbConnection"));
        options.EnableDetailedErrors();
});

My Connection String is:

"server=server.grafton.internal;port=3306;database=labdb;user=******;password=******;charset=utf8"

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.

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

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.

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

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.

If more code samples are wanted, the app is on Github at https://github.com/SteveTeece/Lab.

This one has got me stumped. Any ideas on how to fix this and get it running on the Linux server is greatly appreciated.

Thanks


Solution

  • Thanks everyone for your feedback and comments.

    After another few days of fiddling, I've resolved this issue by using

    Server=localhost;
    

    in the production connection string instead of the FQDN.