【问题标题】:Can't connect springboot to mysql in docker无法在docker中将springboot连接到mysql
【发布时间】:2019-11-06 06:48:44
【问题描述】:

我有一个需要 docker 化的 springboot 应用程序。如果应用程序使用 application.properties 中的以下 datasource.url 在本地连接到 mysql,我可以使用“./mvnw 包”构建 jar 文件

      spring.datasource.url= jdbc:mysql://localhost:3306/test
      spring.datasource.username=root
      spring.datasource.password=root

但是如果我把datasource.url改成这个,

      spring.datasource.url= jdbc:mysql://mysql-test:3306/test

其中 mysql-test 是 docker 中的 mysql 镜像名称,它无法构建 jar 文件。谁能告诉我这里有什么问题?

【问题讨论】:

  • 我不是 docker 专家,但我知道为了让容器“看到”彼此,它们必须在同一个网络中。您是否在 docker 中创建了网络并在同一网络中运行容器?
  • 如何启动mysql-test 图像?
  • 我是 java springboot 的新手,当我在网上参考一些文章时,他们所做的是他们最初创建 jar 文件,然后创建一个 Dockerfile 来复制 jar 并运行它。我被困在最初的步骤本身。我无法通过在 application.properties 中提供 docker-mysql-image 的详细信息来构建 jar
  • @Rashin 我在后台运行 mysql-test 映像
  • 这不是您可以“烘焙”到您的 jar 文件/图像中的配置。假设我的 MySQL 在 Amazon RDS 中外部运行,它不是 localhost 或 Docker Compose 名称;如何在部署时进行配置?

标签: java mysql spring-boot docker


【解决方案1】:

我向您发送了我的 Spring Boot 应用程序属性示例;

spring.datasource.url=jdbc:mysql://localhost:3306/mobildev?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=false&serverTimezone=Turkey
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

您应该根据您的项目更改属性

【讨论】:

  • 你能告诉我如何将springboot连接到docker中的mysql而不是localhost
  • 我向您展示了如何使用休眠序列连接 mysql 和 spring boot。我没有使用 docker,但我发现了一些你应该尝试的东西;在你的 docker run 命令中使用 --network="host",然后你的 docker 容器中的 127.0.0.1 将指向你的 docker 主机。
猜你喜欢
  • 2019-08-04
  • 2018-10-06
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-04
  • 2018-10-02
  • 2021-06-15
  • 2020-08-11
相关资源
最近更新 更多