简体   繁体   English

无法从 spring 启动 Z05B6053C41A2140AFD6FC3BE6Z 容器连接 mysql docker 容器

[英]not able to connect mysql docker container from spring boot docker container

i am getting the following error我收到以下错误

2020-12-26 23:17:30.499 INFO 1 --- [ main] org.hibernate.dialect.Dialect: HHH000400: Using dialect: org.hibernate.dialect.MySQL57Dialect licensingservice_1 | 2020-12-26 23:17:30.499 INFO 1 --- [main] org.hibernate.dialect.Dialect:HHH000400:使用方言:org.hibernate.dialect.MySQL Hibernate: drop table if exists licenses licensingservice_1 | Hibernate:如果存在许可许可服务_1,则删除表 | 2020-12-26 23:17:31.006 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource: HikariPool-1 - Starting... licensingservice_1 | 2020-12-26 23:17:31.006 信息 1 --- [主] com.zaxxer.hikari.HikariDataSource:HikariPool-1 - 开始... licenseservice_1 | 2020-12-26 23:17:32.010 ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool: HikariPool-1 - Exception during pool initialization. 2020-12-26 23:17:32.010 错误 1 --- [main] com.zaxxer.hikari.pool.HikariPool: HikariPool-1 - 池初始化期间出现异常。 licensingservice_1 |许可服务_1 | licensingservice_1 |许可服务_1 | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure licensingservice_1 | com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链路故障许可service_1 | licensingservice_1 |许可服务_1 | The last packet sent successfully to the server was 0 milliseconds ago.最后一个成功发送到服务器的数据包是 0 毫秒前。 The driver has not received any packets from the server.驱动程序没有收到来自服务器的任何数据包。 licensingservice_1 |许可服务_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.22.jar:8.0.22] licensingservice_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.22.jar:8.0.22] licensingservice_1 | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.22.jar:8.0.22] at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.22.jar:8.0.22]

my docker-compose yml我的 docker-compose yml

version : '3'
services:
  licensingservice:
    image: licensing/licensing-service-ms:0.0.1-SNAPSHOT
    ports:
      - "8080:8080"
    networks:
      - my-network
    volumes:
      - .:/vol/development
    depends_on:
      - mysqldbserver
      
  mysqldbserver:
    image: mysql:5.7
    ports:
      - "3307:3306"
    networks:
      - my-network
    environment:
      MYSQL_DATABASE: license
      MYSQL_ROOT_PASSWORD: Spartans@123 
    container_name: mysqldb
networks:
  my-network:
    driver: bridge

and my application.properties和我的 application.properties

spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:mysql://mysqldb:3307/license
spring.datasource.username=root
spring.datasource.password=Spartans@123
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true

Try connecting to port 3306 instead.尝试连接到端口 3306。 You're exposing port 3306 on the database container to the host machine on port 3307, but that doesn't change anything for communication between services inside the same network.您将数据库容器上的端口 3306 暴露给端口 3307 上的主机,但这不会改变同一网络内服务之间的通信。

This is explained in the Docker-Compose docs . Docker-Compose 文档对此进行了解释。

By default Compose sets up a single network for your app.默认情况下,Compose 会为您的应用程序设置一个网络。 Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.服务的每个容器都加入默认网络,并且可以被该网络上的其他容器访问,并且可以通过与容器名称相同的主机名被它们发现。

Additionally, you can choose to expose these ports to the outside world by defining a mapping between the host port and container port.此外,您可以通过定义主机端口和容器端口之间的映射来选择将这些端口公开给外界。 However, this has no effect on communication between services inside the same network:但是,这对同一网络内的服务之间的通信没有影响:

It is important to note the distinction between HOST_PORT and CONTAINER_PORT.注意 HOST_PORT 和 CONTAINER_PORT 之间的区别很重要。 [...] Networked service-to-service communication uses the CONTAINER_PORT. [...] 网络服务到服务通信使用 CONTAINER_PORT。 When HOST_PORT is defined, the service is accessible outside the swarm as well.当 HOST_PORT 被定义时,服务也可以在 swarm 之外访问。

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

相关问题 在docker上运行的mysql容器无法与spring boot java api服务连接 - mysql container which is running on docker can't able to connect with spring boot java api service 如何在Spring Boot Docker容器中访问MySQL Docker容器 - how to access mysql docker container in spring boot docker 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容器连接到docker容器中的mysql - Unable to connect to mysql in docker container from another docker container 无法使用Docker容器名称连接到MySQL,但可以使用localhost连接 - Not able to connect to MySQL with Docker container name but can connect with localhost Spring Boot jar docker 部署抛出与 MySQL 容器的连接异常 - Spring boot jar docker deployment throwing Connection Exception with MySQL Container 将Spring Boot Docker容器连接到MongoDB Docker容器 - Connecting Spring Boot Docker container to MongoDB docker container Spring Boot连接到mysql docker - Spring Boot Connect to mysql docker 从容器连接到 docker 主机 - Connect to docker host from container docker容器中的spring boot对连接没有反应 - spring boot in docker container not reacting to connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM