在docker中使用MySQL数据库设置aspnetcore

编程入门 行业动态 更新时间:2024-10-23 23:29:00
本文介绍了在docker中使用MySQL数据库设置aspnetcore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm trying to set up a docker-compose file with a container for asp-core, mysql database and phpmyadmin. There is no problem setting up my mysql-server, I can access it with phpmyadmin. Also my asp-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

本文发布于:2023-11-14 15:24:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587821.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   docker   MySQL   aspnetcore

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!