【问题标题】:Spring boot app not able to connect to mysql database running in docker container while running as a containerSpring Boot 应用程序在作为容器运行时无法连接到在 docker 容器中运行的 mysql 数据库
【发布时间】:2020-05-08 11:18:41
【问题描述】:
server.port = 8080
spring.datasource.url= jdbc:mysql://192.168.99.100:3307/user_info
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
#spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.dialect.storage_engine=innodb
spring.jpa.database-platform: org.hibernate.dialect.MySQL5InnoDBDialect


#eureka.client.service-url.default.zone = http://localhost:8761/eureka
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false
#eureka.instance.hostname=localhost

我已经使用 docker 为 spring boot 应用程序创建了一个容器,并且为 mysql 创建了一个容器,但是 sboot 应用程序无法连接到 mysql 但是当我尝试在本地运行时它很容易连接,我已经把我的 application.properties 文件放好了告诉应该做什么来解决这个问题 docker 运行在 192.168.99.100

【问题讨论】:

    标签: spring-boot docker


    【解决方案1】:

    使用您的主机 ip 和发布的端口 3307。 如果您使用的是内部 IP 地址,请使用私有端口 3306

    spring.datasource.url= jdbc:mysql://192.168.99.100:3306/user_info
    

    但最好与主机名一起使用

    spring.datasource.url= jdbc:mysql://mysql-dev:3306/user_info
    

    【讨论】:

    • 谢谢,您的帮助问题是由于在 docker 上部署了旧的 jar 文件。
    【解决方案2】:

    实际上问题是我在 docker 上部署了一个旧 jar,我重新创建了 jar 并进行了部署,一切正常。

    【讨论】:

      猜你喜欢
      • 2015-11-19
      • 2017-11-03
      • 2020-10-15
      • 1970-01-01
      • 2018-11-10
      • 2018-07-11
      • 2019-10-25
      • 2021-08-02
      • 2021-04-22
      相关资源
      最近更新 更多