'dotnet ef database update' command gives error: Host 'host.docker.internal' is not allowed to connect to this MySQL server

0

Hi StackOverflow Community,

I have a code first application in .netcore 3.0 that connects to SQL Server in the back-end. I am trying to migrate my existing SQL Server Database to MySQL.

dotnet ef migrations add InitialCreation command worked well.

However,

dotnet ef database update command gives an error:

Can anyone explain what is going on and why host.docker.internal is not allowed to connect?

This is the Migrations reference document and here is the error:

MySql.Data.MySqlClient.MySqlException (0x80004005): Host 'host.docker.internal' is not allowed to connect to this MySQL server
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.Open()
   at MySql.Data.MySqlClient.Driver.Open()
   at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
   at MySql.Data.MySqlClient.MySqlPool.GetConnection()
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at MySql.Data.EntityFrameworkCore.MySQLDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext c, TState s)
   at MySql.Data.EntityFrameworkCore.Storage.Internal.MySQLExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation)
   at MySql.Data.EntityFrameworkCore.MySQLDatabaseCreator.Exists(Boolean retryOnNotExists)
   at MySql.Data.EntityFrameworkCore.MySQLDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Host 'host.docker.internal' is not allowed to connect to this MySQL server
mysql
entity-framework
docker
.net-core
ef-migrations
asked on Stack Overflow May 21, 2020 by sum1

1 Answer

0

It's look like you try to use a MySQL user that is not allowed to connect from this hostname.

On MySQL, when you create a user, you specify a username and a host user@host. If the hostname doesn't fit, the user can't connect to the server.

If you want a user that could connect from anywhere, you need to use the hostname % like 'username'@'%'.

For more information, you can read the MySQL doc about account names: https://dev.mysql.com/doc/refman/8.0/en/account-names.html

You can check the user list with this query:

mysql> select Host, User from mysql.user;
+-----------+---------------+
| Host      | User          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
| %         | julien        |
+-----------+---------------+
4 rows in set (0.00 sec)

On this example, the user julien can connect to MySQL from anywhere but user root could connect to mysql only from localhost.

In your case, the connection come from host.docker.internal, the hostname of your host from the point of vue of the MySQL container. The MySQL container doesn't have the some localhost than your host or than an another container.

If the user used for the connection is not authorized in MySQL to connect from host.docker.internal or from anywhere, MySQL will reject the connection with the error Host 'host.docker.internal' is not allowed to connect to this MySQL server

answered on Stack Overflow May 21, 2020 by jmaitrehenry • edited May 22, 2020 by jmaitrehenry

User contributions licensed under CC BY-SA 3.0