Cannot connect to mysql db after docker compose up

2

I created a container with a mysql database and the manual connection with a .net application works very good. Now I would use a docker-compose to build the mysql container and my .net application image automatically.

My docker compose looks like that.

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

When I start the application in development the connection string is printed and looks like that server=localhost;Port=3305;Database=authenticationdb;Uid=root;Pwd=secret. This works.

When I run docker-compose up -d --build it changes: server=authentication_service_db;Port=3305;Database=authenticationdb;Uid=root;Pwd=secret;

My connection test script:

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

The Log:

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

So I don't know how debug or what I can do next. I don't get a connection when I use docker compose.

mysql
.net
docker
docker-compose
database-connection
asked on Stack Overflow Mar 12, 2019 by kuzdu

1 Answer

1

You mapped port 3306 to 3305 to your host, where you develop the application. So you can connect to db on port 3305. However, you are still using port 3305 inside the container. Because your service (authentication_service_db) is listening on port 3306, not 3305, your application having trouble to connect to database.

You must change connection string to use port 3306, instead of 3305.

answered on Stack Overflow Mar 12, 2019 by Yarimadam

User contributions licensed under CC BY-SA 3.0