简体   繁体   English

在docker上运行的mysql容器无法与spring boot java api服务连接

[英]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.我有一个在 docker 容器上运行的 mysql。 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 $ 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:本地的spring boot应用程序-我正在尝试在java应用程序和mysql容器之间建立连接:

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引起:java.net.UnknownHostException:云
  2. Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago.引起:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 最后一个数据包成功发送到服务器是 0 毫秒前。 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.尽管我已经通过将 mysql 连接器版本和 mysql 容器版本更改为相同的方式来构建该应用程序。 couldn't make it to success.无法成功。 - ——

pom file configuration - pom文件配置-

  1. spring-boot-starter-data-jpa spring-boot-starter-data-jpa
  2. mysql-connector-java - version 5.1.46, docker mysql:5 mysql-connector-java - 版本 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.当您说 - Spring Boot 应用程序正在本地运行时 - 我认为它在 docker 之外运行。
Try following options尝试以下选项

Step1 : Create mysql container again with exposed port : 3306步骤1:再次创建mysql容器,暴露端口: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.步骤 2:使用 mysql 验证是否在暴露的端口上运行并且您可以访问它。 Take help from MY-SQL GUI tools.从 MY-SQL GUI 工具中获取帮助。

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

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

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