在docker中使用MySQL数据库设置aspnetcore [英] Setup aspnetcore with MySQL database in docker

查看:283
本文介绍了在docker中使用MySQL数据库设置aspnetcore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用asp.net-core,mysql数据库和phpmyadmin的容器设置一个docker-compose文件.设置我的mysql服务器没有问题,我可以使用phpmyadmin访问它.另外,我的asp.net核心应用程序正在正确运行,并且可以在浏览器中访问它.

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.

但是,我无法与MySQL数据库建立连接.该应用程序不断返回:

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

无法连接到任何指定的MySQL主机

Unable to connect to any of the specified MySQL hosts

应用程序正在通过appsettings.json中的连接字符串进行连接.

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

我的猜测是,在docker容器中,正在运行的应用程序和mysql似乎无法在localhost上找到彼此.我尝试在连接字符串中使用mysql容器的ip地址,但这也不起作用.

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.

我正在使用以下docker-compose.yml和Dockerfile.

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

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"]

感谢您的帮助.

推荐答案

默认情况下,容器不允许任何人从容器外部以root用户身份登录服务器.这样可以防止其他容器(或主机)也无法连接到数据库(使用根凭据).您可以使用标志MYSQL_ROOT_HOST传递容器或主机的IP,应允许该容器或主机使用根凭据连接到服务器.例如.要允许主机连接,请设置MYSQL_ROOT_HOST="172.17.0.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".

我还看到您仅创建了root用户,但是在连接字符串中使用了userid=user.您可以使用server=127.0.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.

这篇关于在docker中使用MySQL数据库设置aspnetcore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆