简体   繁体   中英

mysql container which is running on docker can't able to connect with spring boot java api service

  1. I have a mysql which is running on docker container. Using this command to pull and run container

  2. $ docker container run --name cloud --network employee -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=bootdb -d mysql:5.7

  3. spring boot application which is at local - i'm trying to establish a connection between java application and mysql container:

Configuration Properties :

spring.datasource.url=jdbc:mysql://cloud/bootdb?serverTimezone=UTC&characterEncoding=UTF-8&useUnicode=true&allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.platform=mysql
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto = create
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect

Error:

  1. Caused by: java.net.UnknownHostException: cloud
  2. Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

although I have done everything to build that application by changing mysql connector version and mysql container version the same. couldn't make it to success. -

pom file configuration -

  1. spring-boot-starter-data-jpa
  2. mysql-connector-java - version 5.1.46, docker mysql:5

when you say - Spring Boot app is running on your local - I assume it is running outside of docker.
Try following options

Step1 : Create mysql container again with exposed port : 3306

docker run --name cloud --network employee -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=bootdb -d mysql:5.7

Step2 : Verify using that mysql is running on the exposed port and you can access it. Take help from MY-SQL GUI tools.

Step3 : Update spring-boot configuration files.
spring.datasource.url=jdbc:mysql://localhost:3306/bootdb?serverTimezone=UTC&characterEncoding=UTF-8&useUnicode=true&allowPublicKeyRetrieval=true&useSSL=false

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