【问题标题】:Cannot connect to mysql db after docker compose updocker compose up后无法连接到mysql db
【发布时间】:2019-08-02 12:59:00
【问题描述】:

我创建了一个带有 mysql 数据库的容器,并且与 .net 应用程序的手动连接效果很好。现在我将使用docker-compose 自动构建mysql 容器和我的.net 应用程序映像。

我的 docker compose 看起来是这样的。

version: '3.1'

networks:
  overlay:
services:
  authentication_service:
    image: authentication_service:latest
    depends_on:
      - "authentication_service_db"
      - "adminer"
    build:
      context: .
      dockerfile: Dockerfile
    links:
      - authentication_service_db
    ports:
      - "5003:80"
    networks:
      - overlay
  authentication_service_db:
    image: mysql:8.0.3
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: authenticationdb
    ports:
      - 3305:3306
    restart: always
    volumes:
      - ./data-authentication:/var/lib/mysql
    networks:
      - overlay

当我在开发中启动应用程序时,连接字符串被打印出来,看起来像server=localhost;Port=3305;Database=authenticationdb;Uid=root;Pwd=secret。这行得通。

当我运行 docker-compose up -d --build 时,它会发生变化: server=authentication_service_db;Port=3305;Database=authenticationdb;Uid=root;Pwd=secret;

我的连接测试脚本:

private static void WaitForDbInit(string connectionString)
        {
            var connection = new MySqlConnection(connectionString);
            Console.WriteLine("Connecting with credentials: {0}", connectionString);

            var retries = 1;
            while (retries < 5)
            {
                try
                {
                    Console.WriteLine("Connecting to db. Trial: {0}", retries);
                    connection.Open();
                    connection.Close();
                    break;
                }
                catch (MySqlException)
                {
                    Thread.Sleep((int) Math.Pow(2, retries) * 1000);
                    retries++;
                }
            }
        }

日志:

Connecting to db. Trial: 1
Connecting to db. Trial: 2
Connecting to db. Trial: 3
Connecting to db. Trial: 4
[...]
application startup exception: MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
   at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 299

appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "MysqlConnection": {
    "connectionString": "server=authentication_service_db;Port=3305;Database=authenticationdb;Uid=root;Pwd=secret;"
 }
}

所以我不知道如何调试或下一步我能做什么。我使用 docker compose 时没有连接。

【问题讨论】:

    标签: mysql .net docker docker-compose database-connection


    【解决方案1】:

    您将端口 3306 到 3305 映射到您开发应用程序的主机。因此,您可以在端口 3305 上连接到 db。但是,您仍在容器内使用端口 3305。因为您的服务 (authentication_service_db) 正在侦听端口 3306,而不是 3305,所以您的应用程序无法连接到数据库。

    您必须更改连接字符串以使用端口 3306,而不是 3305。

    【讨论】:

    • 我会试试这个并报告。我完全不明白。我的映射意图是我可以运行超过 1 个 MySQL 数据库。将会有更多的服务,每个服务都应该有自己的数据库和自己的端口。怎么能每个端口都监听3306?请您详细解释一下。
    • 在 docker 中,您的容器和主机之间没有网络连接。因此,为了连接到运行在 mydb:3306 上的 mysql 服务,请将其映射到 localhost:3305。当您想添加另一个 mysql 实例(例如:mydb2:3306)时,您可以将其映射到 localhost:3304 然后通过它访问。但是,您的容器之间存在网络连接。所以在容器内运行的应用程序应该使用 container-ip/name:service-port 模式来连接到服务。
    猜你喜欢
    • 1970-01-01
    • 2019-08-28
    • 2021-08-15
    • 2020-02-12
    • 2022-08-11
    • 2021-06-16
    • 2017-07-05
    • 2021-08-29
    相关资源
    最近更新 更多