I have a docker stack with a .NET app and a MySQL server.
mysql:
image: mysql
ports:
- 3306:3306
restart: always
environment:
MYSQL_ROOT_PASSWORD: "Password@1234"
MYSQL_DATABASE: "db"
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: "root"
MONGO_INITDB_ROOT_PASSWORD: "Password@1234"
ports:
- 27017:27017
app:
image: ${DOCKER_REGISTRY-}app
build:
context: .
dockerfile: ../App/Dockerfile
ports:
- 7040:443
environment:
- ASPNETCORE_URLS=https://+:443;
depends_on:
- mysql
- mongo
When I try to connect to the server with the following connection string:
"Server=mysql;User=root;Pwd=Password@1234;Database=db"
I get an exception like so:
System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the UseMySq' call.
Inner Exception: MySqlException: Unable to connect to any of the specified MySQL hosts.
Quite weird since I can connect to the database from Azure Data Studio.
Can't figure out what's wrong here. Not sure how this is transient, since when I use DbContext.Database.CanConnect()
it returns false.
Tried using different users in yaml, different values of the con string, adding SSL. Nothing worked.
Very stupid reason for the issue.
When using
options.UseMySql(ServerVersion.AutoDetect(conString))
I didn't add the con string as the first property. Now it works with this:
options.UseMySql(conString, ServerVersion.AutoDetect(conString))
I thought since
public static DbContextOptionsBuilder UseMySql([NotNull] this DbContextOptionsBuilder optionsBuilder, [CanBeNull] string connectionString, [NotNull] ServerVersion serverVersion, [CanBeNull] Action<MySqlDbContextOptionsBuilder> mySqlOptionsAction = null)
says that the ConString can be null it will somehow understand what the con string is but it was a baseless asumption.