question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to connect to MySQL from API project using Tye

See original GitHub issue

Describe the bug

I tried to convert my docker-compose to tye and ran into an issue where the backend api is unable to connect to mysql service, but it is able to connect to redis service.

When running docker-compose up everything works fine. When running tye run the backend fails to connect to mysql.

Check logs for api or navigate to /api/products

To Reproduce

I have a repo here: https://github.com/Matthiee/NgDockerComposeTest/tree/tye/DockerTest

Contains both the working docker-compose.yml and the converted tye.yml

Stacktrace:

[api_1ba45eef-f]:D:\Source\Repos\NgDockerComposeTest\DockerTest\API\bin\Debug\netcoreapp3.1\API.exe
[api_1ba45eef-f]: info: API.Program[0]
[api_1ba45eef-f]: Db connection string: Server=localhost;Port=3306;Database=dockertest;Uid=dockeruser;Pwd=password;
[api_1ba45eef-f]: info: API.Program[0]
[api_1ba45eef-f]: Migrating database..
[api_1ba45eef-f]: fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
[api_1ba45eef-f]: An error occurred using the connection to database '' on server 'localhost'.
[api_1ba45eef-f]: fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
[api_1ba45eef-f]: An error occurred using the connection to database '' on server 'localhost'.
[api_1ba45eef-f]: fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
[api_1ba45eef-f]: An error occurred using the connection to database '' on server 'localhost'.
[api_1ba45eef-f]: fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
[api_1ba45eef-f]: An error occurred using the connection to database '' on server 'localhost'.
[api_1ba45eef-f]: fail: API.Program[0]
[api_1ba45eef-f]: An error occured during migration
[api_1ba45eef-f]: MySql.Data.MySqlClient.MySqlException (0x80004005): Host '172.17.0.1' is not allowed to connect to this MySQL server
[api_1ba45eef-f]: at MySqlConnector.Core.ServerSession.ReceiveReplyAsyncAwaited(ValueTask`1 task) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 774
[api_1ba45eef-f]: at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 354
[api_1ba45eef-f]: at MySql.Data.MySqlClient.MySqlConnection.CreateSessionAsync(ConnectionPool pool, Nullable`1 ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 657
[api_1ba45eef-f]: at MySql.Data.MySqlClient.MySqlConnection.OpenAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 312
[api_1ba45eef-f]: at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnectionAsync(Boolean errorsExpected, CancellationToken cancellationToken)
[api_1ba45eef-f]: at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnectionAsync(Boolean errorsExpected, CancellationToken cancellationToken)
[api_1ba45eef-f]: at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
[api_1ba45eef-f]: at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlRelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
[api_1ba45eef-f]: at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlDatabaseCreator.<>c__DisplayClass20_0.<<ExistsAsync>b__0>d.MoveNext()
[api_1ba45eef-f]: --- End of stack trace from previous location where exception was thrown ---
[api_1ba45eef-f]: at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlDatabaseCreator.<>c__DisplayClass20_0.<<ExistsAsync>b__0>d.MoveNext()
[api_1ba45eef-f]: --- End of stack trace from previous location where exception was thrown ---
[api_1ba45eef-f]: at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlDatabaseCreator.<>c__DisplayClass20_0.<<ExistsAsync>b__0>d.MoveNext()
[api_1ba45eef-f]: --- End of stack trace from previous location where exception was thrown ---
[api_1ba45eef-f]: at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
[api_1ba45eef-f]: at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
[api_1ba45eef-f]: at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.ExistsAsync(CancellationToken cancellationToken)
[api_1ba45eef-f]: at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
[api_1ba45eef-f]: at API.Program.Main(String[] args) in D:\Source\Repos\NgDockerComposeTest\DockerTest\API\Program.cs:line 33
[api_1ba45eef-f]: info: API.Program[0]
[api_1ba45eef-f]: Running application..
[api_1ba45eef-f]: info: Microsoft.Hosting.Lifetime[0]
[api_1ba45eef-f]: Now listening on: http://localhost:9404
[api_1ba45eef-f]: info: Microsoft.Hosting.Lifetime[0]
[api_1ba45eef-f]: Now listening on: https://localhost:9405
[api_1ba45eef-f]: info: Microsoft.Hosting.Lifetime[0]
[api_1ba45eef-f]: Application started. Press Ctrl+C to shut down.

Further technical details

❯ tye --version
0.2.0-alpha.20258.3+bf3bc0913b37e77f5f3ef14f409b3feb794cc447
❯ [System.Environment]::OSVersion

Platform ServicePack Version      VersionString
-------- ----------- -------      -------------
 Win32NT             10.0.18362.0 Microsoft Windows NT 10.0.18362.0

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jkotalikcommented, May 29, 2020

I think we were considering having dependencies for services, specifically for the scenario where you have databases you want to have started first. However, I’m still hesitant due to how this will be checked for deployment scenarios. Do we check that a secret is set before deploying?

0reactions
rynowakcommented, Jun 2, 2020

So what’s different between your docker-compose case and Tye is where the connection comes from. That’s the hint that the error message gives you - Host '172.17.0.1' is not allowed to connect to this MySQL server.

172.17.0.1 is the IP address used by docker for the host gateway - meaning that the connection is coming from the host machine, rather than another container on the same network. In docker-compose your application is running in another container - with Tye it’s running on your host (your host OS, not in a container).

Can you try adding the following to env for the mysql container:

- name: MYSQL_ROOT_HOST
   value: '%'

If the docs are to be believed this allows connections from any IP.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't connect to Mysql from asp.net api
I tried to do dotnet ef migrations remove but i have this error now : Method 'Create' in type 'Pomelo.EntityFrameworkCore.MySql.Query.
Read more >
4.22 Troubleshooting Problems Connecting to MySQL
Make sure that the server is running. If it is not, clients cannot connect to it. For example, if an attempt to connect...
Read more >
How to Connect to MySQL from .NET Core
1. Install MySqlConnector ... Or right-click your project, choose Manage NuGet Packages…, in the Search box enter MySqlConnector , and install the package...
Read more >
Connecting to a MySQL Database - Apache NetBeans
Right-click the Databases node in the Services window and choose Register MySQL Server to open the MySQL Server Properties dialog box. · Confirm...
Read more >
Connect using a MySQL client
You can use the MySQL command-line client to connect to Cloud SQL. This page describes how to connect a mysql client to your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found