简体   繁体   中英

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 | Hibernate: drop table if exists licenses licensingservice_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:32.010 ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool: HikariPool-1 - Exception during pool initialization. licensingservice_1 | licensingservice_1 | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure licensingservice_1 | licensingservice_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. 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]

my 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

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. 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.

This is explained in the Docker-Compose docs .

By default Compose sets up a single network for your app. 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. [...] Networked service-to-service communication uses the CONTAINER_PORT. When HOST_PORT is defined, the service is accessible outside the swarm as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM