Setup aspnetcore with MySQL database in docker

拥有回忆 提交于 2019-12-12 21:05:20

问题


I'm trying to set up a docker-compose file with a container for asp.net-core, mysql database and phpmyadmin. There is no problem setting up my mysql-server, I can access it with phpmyadmin. Also my asp.net-core application is running correctly and I can access it in the browser.

However, I am not able to make a connection with my MySQL database. The application keeps returning:

Unable to connect to any of the specified MySQL hosts

The application is connecting through a connection string in appsettings.json.

{
  "ConnectionStrings": {
    "FlowerAPIConnection": "server=localhost;userid=user;password=user;database=bloemenapi_db;Convert Zero Datetime=True"
  },
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

My guess is that in the docker container the app and mysql are running can't seem to find each other on localhost. I tried using the ip-adress of the mysql container in the connectionstring, but that also did not work.

I'm using following docker-compose.yml and Dockerfile.

version: '3'

services:
  db:
    image: mysql
    restart: always
    container_name: flowerapi-db
    environment:
      - MYSQL_USER=root
      - MYSQL_PASSWORD=user
      - MYSQL_ROOT_PASSWORD=user
      - MYSQL_DATABASE=bloemenapi_db
    ports:
      - "3306"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: flowerapi-pma
    ports:
      - "81:80"
    external_links:
      - db:mysql
    environment:
      PMA_HOST: "db"
      PMA_PORT: 3306
  web:
    image: flowerapi
    container_name: flowerapi-web
    build:
      context: ./FlowerAPI
      dockerfile: Dockerfile
    ports:
      - "5000:80"
    links:
      - db
    depends_on:
      - db

Dockerfile

FROM microsoft/aspnetcore:2.0
LABEL name "flower-api"
ARG source
WORKDIR /app
EXPOSE 5000/tcp
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "FlowerAPI.dll"]

Thanks for your help.


回答1:


The container, by default, does not allow anyone to log into the server as root from outside the container. This prevents other containers (or the host too) from connecting to the db (using root credentials). You can use flag MYSQL_ROOT_HOST to pass the IP of the container or host which should be allowed to connect to the server with root credentials. Eg. To allow the host to connect, you would set MYSQL_ROOT_HOST="172.17.0.1".

Also I see you created only root user, but in connection string you use userid=user. You can use server=127.0.0.1 as host name.




回答2:


Try this. Connection string is bit different then with MSSQL

"ConnectionStrings":{
        "FlowerAPIConnection":"Server=db;port=3306;Database=bloe‌menapi_db;User=user;Password=password;"
 }



回答3:


MySql connector is needed for connecting .NET apps with MySql db. You'd need to install that connector in your asp.net core app container




回答4:


localhost is inside each container, it won't work. Try to connect using db as hostname, and port 3306.



来源:https://stackoverflow.com/questions/46170736/setup-aspnetcore-with-mysql-database-in-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!