代码之家  ›  专栏  ›  技术社区  ›  kuzdu

docker合成后无法连接到mysql db

  •  1
  • kuzdu  · 技术社区  · 7 年前

    .net 应用程序工作得很好。现在我要用一个 docker-compose 构建mysql容器 自动应用程序映像。

    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;"
     }
    }
    

    0 回复  |  直到 7 年前
        1
  •  1
  •   Yarimadam    7 年前

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

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

    推荐文章