简体   繁体   中英

MySqlException: Unable to connect to any of the specified MySQL hosts

I got the error below after running my .Net core API project in Docker Desktop . It creates all of my Images but it seems to be unable to connect to MySQL instance:

MySqlException: Unable to connect to any of the specified MySQL hosts.
MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, int startTickCount, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in ServerSession.cs, line 413

I have completed docker-compose file as below. (This file contains some other services that I decided not to remove, because I guessed this problem might stem from those):

version: "3"

volumes:
  datafiles:

services:

  database:
    image: mysql:8.0.28
    ports:
      - "3307:3306"
    volumes:
      - datafiles:/var/lib/mysql
      - "~/sql-scripts/setup.sql:/docker-entrypoint-initdb.d/1.sql"
    restart: always
    environment: 
      MYSQL_ROOT_PASSWORD: ******
      MYSQL_USER: sa
      MYSQL_PASSWORD: ******
      MYSQL_DATABASE: Pand
    networks:
       - hub_network
  chrome:
    image: selenium/node-chrome:4.3.0-20220706
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
    networks:
       - hub_network

  selenium-hub:
    image: selenium/hub:4.3.0-20220706
    container_name: selenium-hub
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"
    networks:
       - hub_network
  web:
     build: .
     ports: 
       - "8090:80"
     depends_on:
       - database
       - selenium-hub
       - chrome
     restart: always
     networks:
       - hub_network
     #Specify Environment Variables for the Api Service
     environment: 
       - DBHOST=database
       - ASPNETCORE_ENVIRONMENT=Development
networks:
 hub_network:
  external: false

And also it is useful to share my appsettings.Development.json :

{
  "ConnectionStrings": {
    "MYSQL_PASSWORD": "******",
    "MYSQL_USER": "sa",
    "MYSQL_DATABASE": "Pand"
  }
}

And here is a piece of code in which I create my Context :

    public void ConfigureServices(IServiceCollection services)
    {
        var host = "database";
        var port = "3307";
        var password = Configuration["MYSQL_PASSWORD"] ?? Configuration.GetConnectionString("MYSQL_PASSWORD");
        var userid = Configuration["MYSQL_USER"] ?? Configuration.GetConnectionString("MYSQL_USER");
        var usersDataBase = Configuration["MYSQL_DATABASE"] ?? Configuration.GetConnectionString("MYSQL_DATABASE");

        var connectionString = $"server={host}; userid={userid};pwd={password};port={port};database={usersDataBase}";
        services.AddDbContext<EFDbContext>(
            options => options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString))
                .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking), ServiceLifetime.Transient);
    }

If any other code snippet needed please ask me to share.

Did you try to connect mysql from the host using mysql client? Does that work? You have defined host as "database" try to connect same using mysql client and post the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM