Docker-Compose无法连接到任何指定的MySQL主机 [英] Docker-Compose Unable to connect to any of the specified MySQL hosts
问题描述
我有一个现有项目(API-门户-Mysql)我使用了没有dockerfile的Docker-compose我发布了API-Portal,并将它们全部放入文件夹中,然后由Docker组成
我可以通过获取本地值来访问api但是,如果我尝试使用邮递员通过API通过API访问mysql,即使我打开前端网站,它也无法正常工作
ConnectionString:"ConnectionString":服务器= xmysql;端口= 4406;数据库= sbs_hani;用户ID = hani;密码= 123456; persistsecuritysecurity == True;字符集= utf8; TreatTinyAsBoolean = false;"
这是我的docker-compose文件:
版本:"3"服务:xmysql的:container_name:xmysql主机名:xmysql图片:mysql:latest重启:总是环境:MYSQL_ROOT_PASSWORD:"123456"MYSQL_DATABASE:"sbs_hani"MYSQL_USER:"hani"MYSQL_PASSWORD:"123456"端口:-"3306:4406"网络:-xnetwork数量:-数据量:/var/lib/mysql-./hanimysql/sbs_hani.sql:/docker-entrypoint-initdb.d/sbs_hani.sqlxapi:container_name:xapi主机名:xapi图片:microsoft/dotnet:latest#重启:总是tty:是的命令:["dotnet","/var/lib/volhaniapi/hani.APIs.dll"]端口:-"8081:80"-"8444:443"网络:-xnetwork链接:-xmysql:xmysql取决于:-xmysql数量:-./haniapi/:/var/lib/volhaniapi/xportal:container_name:xportal主机名:xportal图片:microsoft/dotnet:latest#重启:总是tty:是的命令:["dotnet","/var/lib/volhaniportal/hani.Portal.dll"]端口:-"8083:80"-"8446:443"网络:-xnetwork链接:-xmysql:xmysql取决于:-xmysql数量:-./haniportal/:/var/lib/volhaniportal/xfront:container_name:xfront主机名:xfront图片:nginx:stable-alpine#重启:总是端口:-"8082:80"-"4445:443"网络:-xnetwork链接:-xapi:xapi取决于:-xapi数量:-./hanifront/://usr/share/nginx/html数量:数据量:{}#xvolmysql:#驱动程序:本地"#xvolmongo:#驱动程序:本地"#xvolrabbitmq:#驱动程序:本地"#xvolstarapi:#驱动程序:本地"网络:xnetwork:司机:桥
当您从一个Docker容器连接到另一个Docker容器时,您始终会连接到该容器中的服务实际上正在侦听的端口.任何 ports:
映射都将被忽略(实际上,如果您不希望从Docker容器空间之外访问该服务,则不需要 ports:
)./p>
在您的示例中,您需要将连接字符串中的端口号更改为默认的MySQL端口3306.
(考虑删除所有 container_name:
, hostname:
, networks:
和 links:
块您应该有一个具有相同功能的等效容器堆栈;最明显的两个区别是,如果直接使用 docker
命令,则容器名称将以目录名称作为前缀,并且Docker内部网络将被命名为 default
.您仍然可以使用服务代码名称(例如 xmysql
作为主机名).
i have an existing Project ( API - portal - Mysql ) i have used Docker-compose without dockerfile i publish the API - Portal and put them all in folder and then Docker-compose up
i can reach the api by getting the local values in it but if i tried to reach mysql trough the API using postman its not working even when i open the frontend website
ConnectionString:
"ConnectionString": "server=xmysql;port=4406;Database=sbs_hani;User ID=hani;Password=123456; persistsecurityinfo=True;Charset=utf8; TreatTinyAsBoolean=false;"
This is my docker-compose file :
version: '3'
services:
xmysql:
container_name: xmysql
hostname: xmysql
image: mysql:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: "123456"
MYSQL_DATABASE: "sbs_hani"
MYSQL_USER: "hani"
MYSQL_PASSWORD: "123456"
ports:
- "3306:4406"
networks:
- xnetwork
volumes:
- data-volume:/var/lib/mysql
- ./hanimysql/sbs_hani.sql:/docker-entrypoint-initdb.d/sbs_hani.sql
xapi:
container_name: xapi
hostname: xapi
image: microsoft/dotnet:latest
# restart: always
tty: true
command: ["dotnet", "/var/lib/volhaniapi/hani.APIs.dll"]
ports:
- "8081:80"
- "8444:443"
networks:
- xnetwork
links:
- xmysql:xmysql
depends_on:
- xmysql
volumes:
- ./haniapi/:/var/lib/volhaniapi/
xportal:
container_name: xportal
hostname: xportal
image: microsoft/dotnet:latest
# restart: always
tty: true
command: ["dotnet", "/var/lib/volhaniportal/hani.Portal.dll"]
ports:
- "8083:80"
- "8446:443"
networks:
- xnetwork
links:
- xmysql:xmysql
depends_on:
- xmysql
volumes:
- ./haniportal/:/var/lib/volhaniportal/
xfront:
container_name: xfront
hostname: xfront
image: nginx:stable-alpine
# restart: always
ports:
- "8082:80"
- "4445:443"
networks:
- xnetwork
links:
- xapi:xapi
depends_on:
- xapi
volumes:
- ./hanifront/:/usr/share/nginx/html
volumes:
data-volume: {}
# xvolmysql:
# driver: "local"
# xvolmongo:
# driver: "local"
# xvolrabbitmq:
# driver: "local"
# xvolstarapi:
# driver: "local"
networks:
xnetwork:
driver: bridge
When you make a connection from one Docker container to another, you always connect to the port the service inside the container is actually listening on. Any ports:
mappings are ignored (and in fact you don't need ports:
if you don't want the service to be accessible from outside Docker container space).
In your example, you need to change the port number in the connection string to the default MySQL port 3306.
(Consider removing all of the container_name:
, hostname:
, networks:
, and links:
blocks in the file. You should have an equivalent container stack with the same functionality; the two most observable differences are that if you directly use docker
commands then the container names will be prefixed with the directory names, and the Docker-internal network will be named default
. You can still use the service block names like xmysql
as host names.)
这篇关于Docker-Compose无法连接到任何指定的MySQL主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!