简体   繁体   English

应用容器无法连接到同一网络中的mysql容器

[英]App container cannot connect to mysql container in the same network

I have a problem with the docker and containerized apps not connecting to mysql database that is running in a container within the same docker network.我的 docker 和容器化应用程序无法连接到在同一 docker 网络内的容器中运行的 mysql 数据库时遇到问题。 My docker-compose.yml looks like this:docker-compose.yml看起来像这样:

version: '3.8'

services:

  mysqlserver:
    image: mysql:5.7.24
    container_name: mysql_app
    networks:
      - somenetwork
    ports:
      - "5647:3306"
    environment:
      MYSQL_DATABASE: database
      MYSQL_USER: USER
      MYSQL_PASSWORD: PASS
      MYSQL_ROOT_PASSWORD: PASS
    command:
      - --max_connections=1000
    restart: always
    volumes:
     - persistent:/var/lib/mysql

  app:
    image: app/appserver:latest
    container_name: app_server
    networks:
      - somenetwork
    ports:
      - "8080:8080"
    depends_on:
      - mysqlserver
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://mysqlserver:5647/database?verifyServerCertificate=false&useSSL=false&requireSSL=false&autoReconnect=true

networks:
  somenetwork:
    name:
      somenetwork_share_net

volumes:
  persistent:

And looking at the logs from the app container, I found errors like this:查看应用程序容器中的日志,我发现了如下错误:

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago.引起:com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure 最后一个数据包成功发送到服务器是 0 毫秒前。 The driver has not received any packets from the server.驱动程序没有收到来自服务器的任何数据包。

and

Caused by: java.net.ConnectException: Connection refused (Connection refused) at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]引起:java.net.ConnectException:在 java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na] 的连接被拒绝(连接被拒绝)

Is there something wrong with my docker-compose.yml ?docker-compose.yml有什么问题吗? Why the app container cannot connect为什么应用容器无法连接

You have to use the exposed port on container side 3303 and not the public one 5647 .您必须使用容器端3303上的公开端口,而不是公共端口5647

Just try你试一试

SPRING_DATASOURCE_URL: jdbc:mysql://mysqlserver:3306/database?verifyServerCertificate=false&useSSL=false&requireSSL=false&autoReconnect=true

The port in local network stays the original exposed port.本地网络中的端口保持原来的暴露端口。 In port section you just define the public ports on the host machine.在端口部分,您只需定义主机上的公共端口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法连接到mongo容器 - Cannot connect to mongo container Spring 启动 2 应用程序在 docker-maven-plugin 触发的 docker 容器内运行时无法连接到 mysql - Spring Boot 2 app cannot connect to mysql while run inside docker container triggered by docker-maven-plugin Docker:springboot容器无法连接到PostgreSql容器 - Docker: Springboot container cannot connect to PostgreSql Container "无法从另一个容器连接到 docker 中的 Mysql 服务器" - Cannot connect to Mysql server in docker from another container 当 Docker 容器中的 Java MVC 应用程序尝试连接到另一个容器中的 MySQL 时,表不存在错误 - Table Doesn't Exist Error When Java MVC app in Docker Container Try to Connect to MySQL in Another Container 如何从主机外部(同一网络)连接到在 docker 容器内运行的 activemq 代理 [Windows] - How to connect to an activemq broker which is running inside a docker container from outside the host (same network) [Windows] Spring Boot 容器无法连接到 My SQL 容器 - Spring Boot container cannot connect to My SQL container 无法从 spring 启动 Z05B6053C41A2140AFD6FC3BE6Z 容器连接 mysql docker 容器 - not able to connect mysql docker container from spring boot docker container 无法从另一个docker容器连接到docker容器中的mysql - Unable to connect to mysql in docker container from another docker container Java 容器无法使用 docker-compose 连接到 MYSQL 容器 - Java container cant connect to MYSQL container with docker-compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM