从asp.net core 2 web app whithin docker compose连接到mysql 8

n6lpvg4x  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(473)

我正在尝试使用docker编写脚本从我的asp.net core 2.1 web应用程序(在windows 10 pro上托管,所有容器都使用linux)连接到mysql容器,但我总是出现无法连接到任何指定的mysql主机的错误。
该应用程序使用两个服务,一个mongodb容器和一个mysql容器(使用mysql 8的最新docker映像)。api应用程序使用bash脚本等待mongodb服务(https://github.com/vishnubob/wait-for-it),这似乎奏效了。
至于mysql,我在google上添加了一个解决方案: command: --default-authentication-plugin=mysql_native_password (https://github.com/docker-library/mysql/issues/454) MYSQL_ROOT_HOST=% (例如。https://forums.docker.com/t/connect-to-container-create-by-docker-compose/44682/5).
我也听从了会议的建议https://github.com/docker-library/mysql/issues/275:我可以确认mysql根用户链接到任何ip( % ),而服务的ip地址配置为 * ,这似乎是表示“任意地址”的另一种方式,如0.0.0.0。
我在开始撰写和docker撰写脚本时得到的相关日志消息如下( lexminapi 正在记录接收到的环境变量以检查连接字符串):

lexmin-mysql    | [Entrypoint] MySQL Docker Image 8.0.13-1.1.8
   lexmin-mysql    | [Entrypoint] Initializing database
   lexmin-mysql    | 2018-11-20T07:59:11.930174Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.13) initializing of server in progress as process 21
   lexminapi_1     | ZAX__CONNECTIONSTRING=Server=lexmin-mysql;Database=zax_master;Uid=root;Pwd=password;SslMode=none
   lexmin-mysql    | 2018-11-20T07:59:22.077828Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
   lexminapi_1     | Application startup exception: MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
   lexminapi_1     |    at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 322
   lexminapi_1     |    at MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ConnectionPool.cs:line 112
   lexminapi_1     |    at MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ConnectionPool.cs:line 141
   lexminapi_1     |    at MySql.Data.MySqlClient.MySqlConnection.CreateSessionAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 405
   lexminapi_1     |    at MySql.Data.MySqlClient.MySqlConnection.OpenAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 175
   lexminapi_1     |    at MySql.Data.MySqlClient.MySqlConnection.Open() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 161
   lexminapi_1     |    at Zax.MySql.MySqlZaxAdmin.EnsureCatalogExists()
   lexminapi_1     |    at LexminApi.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in /src/LexminApi/Startup.cs:line 310
   lexminapi_1     | --- End of stack trace from previous location where exception was thrown ---
   lexminapi_1     |    at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
   lexminapi_1     |    at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
   lexminapi_1     |    at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
   lexminapi_1     |    at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
   lexminapi_1     | Unable to connect to any of the specified MySQL hosts.
   lexminapi_1     | MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
   lexminapi_1     |    at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 322
   lexminapi_1     |    at MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ConnectionPool.cs:line 112
   lexminapi_1     |    at MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ConnectionPool.cs:line 141
   lexminapi_1     |    at MySql.Data.MySqlClient.MySqlConnection.CreateSessionAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 405
   lexminapi_1     |    at MySql.Data.MySqlClient.MySqlConnection.OpenAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 175
   lexminapi_1     |    at MySql.Data.MySqlClient.MySqlConnection.Open() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 161
   lexminapi_1     |    at Zax.MySql.MySqlZaxAdmin.EnsureCatalogExists()
   lexminapi_1     |    at LexminApi.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in /src/LexminApi/Startup.cs:line 310
   lexminapi_1     | --- End of stack trace from previous location where exception was thrown ---
   lexminapi_1     |    at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
   lexminapi_1     |    at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
   lexminapi_1     |    at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
   lexminapi_1     |    at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
   lexminapi_1     |    at Microsoft.AspNetCore.Hosting.Internal.WebHost.StartAsync(CancellationToken cancellationToken)
   lexminapi_1     |    at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token, String shutdownMessage)
   lexminapi_1     |    at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token)
   lexminapi_1     |    at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host)
   lexminapi_1     |    at LexminApi.Program.Main(String[] args) in /src/LexminApi/Program.cs:line 64
   lexmin-mysql    | 2018-11-20T07:59:25.515319Z 5 [Warning] [MY-010315] [Server] 'user' entry 'mysql.infoschema@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:25.515408Z 5 [Warning] [MY-010315] [Server] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:25.515434Z 5 [Warning] [MY-010315] [Server] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:25.515458Z 5 [Warning] [MY-010315] [Server] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:25.515505Z 5 [Warning] [MY-010323] [Server] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:25.515540Z 5 [Warning] [MY-010323] [Server] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:25.515570Z 5 [Warning] [MY-010311] [Server] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:25.515826Z 5 [Warning] [MY-010330] [Server] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:25.515846Z 5 [Warning] [MY-010330] [Server] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:27.343300Z 0 [System] [MY-013170] [Server] /usr/sbin/mysqld (mysqld 8.0.13) initializing of server has completed
   lexmin-mysql    | [Entrypoint] Database initialized
   lexmin-mysql    | 2018-11-20T07:59:30.122415Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.13) starting as process 67
   lexmin-mysql    | 2018-11-20T07:59:31.233579Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
   lexmin-mysql    | 2018-11-20T07:59:31.270181Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.infoschema@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:31.270517Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:31.270675Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:31.270818Z 0 [Warning] [MY-010315] [Server] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:31.271038Z 0 [Warning] [MY-010323] [Server] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:31.272659Z 0 [Warning] [MY-010323] [Server] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:31.272734Z 0 [Warning] [MY-010311] [Server] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:31.285399Z 0 [Warning] [MY-010330] [Server] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:31.286316Z 0 [Warning] [MY-010330] [Server] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:31.299467Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.13'  socket: '/var/lib/mysql/mysql.sock'  port: 0  MySQL Community Server - GPL.
   lexmin-mysql    | 2018-11-20T07:59:31.474623Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock'
   lexmin-mysql    | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
   lexmin-mysql    | Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
   lexmin-mysql    | Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
   lexmin-mysql    | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
   lexmin-mysql    | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
   lexmin-mysql    | 2018-11-20T07:59:37.721908Z 9 [Warning] [MY-010315] [Server] 'user' entry 'healthchecker@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:37.721967Z 9 [Warning] [MY-010315] [Server] 'user' entry 'mysql.infoschema@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:37.721997Z 9 [Warning] [MY-010315] [Server] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:37.722022Z 9 [Warning] [MY-010315] [Server] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:37.722068Z 9 [Warning] [MY-010315] [Server] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:37.722135Z 9 [Warning] [MY-010323] [Server] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:37.722157Z 9 [Warning] [MY-010323] [Server] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:37.722203Z 9 [Warning] [MY-010311] [Server] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:37.722596Z 9 [Warning] [MY-010330] [Server] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:37.722642Z 9 [Warning] [MY-010330] [Server] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 
   lexmin-mysql    | [Entrypoint] ignoring /docker-entrypoint-initdb.d/*
   lexmin-mysql    | 
   lexmin-mysql    | 2018-11-20T07:59:37.765710Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.13).
   lexmin-mysql    | 2018-11-20T07:59:40.346144Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.13)  MySQL Community Server - GPL.
   lexmin-mysql    | [Entrypoint] Server shut down
   lexmin-mysql    | 
   lexmin-mysql    | [Entrypoint] MySQL init process done. Ready for start up.
   lexmin-mysql    | 
   lexmin-mysql    | [Entrypoint] Starting MySQL 8.0.13-1.1.8
   lexmin-mysql    | 2018-11-20T07:59:41.244150Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.13) starting as process 1
   lexmin-mysql    | 2018-11-20T07:59:42.257165Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
   lexmin-mysql    | 2018-11-20T07:59:42.286105Z 0 [Warning] [MY-010315] [Server] 'user' entry 'healthchecker@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:42.286148Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.infoschema@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:42.286179Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:42.286210Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:42.286236Z 0 [Warning] [MY-010315] [Server] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:42.286296Z 0 [Warning] [MY-010323] [Server] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:42.286318Z 0 [Warning] [MY-010323] [Server] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:42.286349Z 0 [Warning] [MY-010311] [Server] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:42.292963Z 0 [Warning] [MY-010330] [Server] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:42.293011Z 0 [Warning] [MY-010330] [Server] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
   lexmin-mysql    | 2018-11-20T07:59:42.303034Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.13'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server - GPL.
   lexmin-mysql    | 2018-11-20T07:59:42.329828Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
   lexminapi_1     | Application is shutting down...

脚本:

version: '3.4'

   services:
     # MongoDB
     lexmin-mongo:
       image: mongo
       container_name: lexmin-mongo
       environment:
         - MONGO_DATA_DIR=/data/db
         - MONGO_LOG_DIR=/dev/null
       ports:
         - 27017:27017
       command: mongod --smallfiles --logpath=/dev/null # --quiet
       ports:
         - 27017:27017
       networks:
         - lexmin-network

     # MySql
     lexmin-mysql:
       image: mysql/mysql-server
       container_name: lexmin-mysql
       command: --default-authentication-plugin=mysql_native_password
       environment:
         - MYSQL_ROOT_PASSWORD=password
         - MYSQL_ROOT_HOST=%
       ports:
         - 3306:3306
       networks:
         - lexmin-network

     lexminapi:
       image: XXX:lexminapi
       ports:
         - 34577:80
       depends_on:
         - lexmin-mongo
         - lexmin-mysql
       command: ["./wait-for-it.sh", "lexmin-mongo:27017", "--", "dotnet", "LexminApi.dll"]
       environment:
         # for Windows use : as separator, for non Windows use __
         # (see https://github.com/aspnet/Configuration/issues/469)
         - ZAX__CONNECTIONSTRING=Server=lexmin-mysql;Database=zax_master;Uid=root;Pwd=password;SslMode=none
       networks:
         - lexmin-network

   networks:
     lexmin-network
bjg7j2ky

bjg7j2ky1#

我终于找到了一个解决方案,包括对上述两个方面的修改:
在配置mysql环境变量时,我们似乎必须使用字典而不是数组,正如连接到docker compose mysql容器时发现的那样,该容器拒绝访问,但运行相同映像的docker不拒绝访问。所以我把我的 composer 改成这样:

environment:
      # the password that will be set for the MySQL root superuser account
      MYSQL_ROOT_PASSWORD: password
      MYSQL_ROOT_HOST: '%'

请注意,使用词典需要 % 在引号之间,否则会出现yaml错误。
一旦像上面那样改变了,我就得到了断断续续的结果:有时应用程序会启动,但大多数情况下它仍然会出现连接错误。当这指向一个超时问题时,我在google上搜索了一个好的重试模式,找到了polly。因此,我将mysql种子代码 Package 到polly中 Execute 方法,如下所示:

Policy.Handle()
    .WaitAndRetry(new[]
    {
        TimeSpan.FromSeconds(10),
        TimeSpan.FromSeconds(30),
        TimeSpan.FromSeconds(60)
    }, (exception, timeSpan, context) =>
    {
        string message = $"Unable to connect to MySql (sleep {timeSpan}): {exception.Message}";
        Console.WriteLine(message);
    }).Execute(() =>
{
  // ... MySql init code here...
});

请注意,asp.net核心2中种子设定的正确位置不是 Startup.cs ; 例如,参见如何在实体框架核心2?中播种?。
当我启动应用程序时,我现在可以看到第一次超时为10“,然后重试被触发,直到应用程序启动而不退出,因为 MySqlException .

相关问题