<bdo id='3g6s4'></bdo><ul id='3g6s4'></ul>
  • <small id='3g6s4'></small><noframes id='3g6s4'>

      <i id='3g6s4'><tr id='3g6s4'><dt id='3g6s4'><q id='3g6s4'><span id='3g6s4'><b id='3g6s4'><form id='3g6s4'><ins id='3g6s4'></ins><ul id='3g6s4'></ul><sub id='3g6s4'></sub></form><legend id='3g6s4'></legend><bdo id='3g6s4'><pre id='3g6s4'><center id='3g6s4'></center></pre></bdo></b><th id='3g6s4'></th></span></q></dt></tr></i><div id='3g6s4'><tfoot id='3g6s4'></tfoot><dl id='3g6s4'><fieldset id='3g6s4'></fieldset></dl></div>
      <legend id='3g6s4'><style id='3g6s4'><dir id='3g6s4'><q id='3g6s4'></q></dir></style></legend>
    1. <tfoot id='3g6s4'></tfoot>

        Docker 中的 Mysql 连接错误与 Spriing 启动

        时间:2024-04-16
          <tfoot id='kS4FC'></tfoot>

          1. <small id='kS4FC'></small><noframes id='kS4FC'>

              <legend id='kS4FC'><style id='kS4FC'><dir id='kS4FC'><q id='kS4FC'></q></dir></style></legend>
                  <tbody id='kS4FC'></tbody>

                  <bdo id='kS4FC'></bdo><ul id='kS4FC'></ul>
                  <i id='kS4FC'><tr id='kS4FC'><dt id='kS4FC'><q id='kS4FC'><span id='kS4FC'><b id='kS4FC'><form id='kS4FC'><ins id='kS4FC'></ins><ul id='kS4FC'></ul><sub id='kS4FC'></sub></form><legend id='kS4FC'></legend><bdo id='kS4FC'><pre id='kS4FC'><center id='kS4FC'></center></pre></bdo></b><th id='kS4FC'></th></span></q></dt></tr></i><div id='kS4FC'><tfoot id='kS4FC'></tfoot><dl id='kS4FC'><fieldset id='kS4FC'></fieldset></dl></div>

                  本文介绍了Docker 中的 Mysql 连接错误与 Spriing 启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经开始使用 docker 并且正在创建一些基本示例.当我使用 docker-copmose.yml 文件时,我遇到了 spring boot 和 mysql 连接之间的问题.

                  I have started with docker and I am creating some basic examples. I am facing problem between spring boot and mysql connection when I am using docker-copmose.yml file.

                  奇怪的是它在 jdbc:mysql 给出错误.它正在考虑作为主机的数据库连接的第二个参数.我不知道为什么?

                  当我分别手动启动 mysql 和 springboot 容器时,同样的连接配置也有效.我只有在使用 docker-compose 时才会遇到这个问题

                  This same connection confg works when I manually start mysql and springboot container individually. I am facing this problem only when I use docker-compose

                  application.properties

                  application.properties

                  spring.datasource.username=root
                  spring.datasource.password=mypass
                  spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
                  spring.jpa.hibernate.ddl-auto=update
                  spring.jpa.show-sql=true
                  spring.datasource.url=jdbc:mysql://mysql-db:3306/docker_test
                  

                  Dockerfile

                  FROM openjdk:8u212-jre-alpine
                  ADD target/docker-demo-1.0.jar docker-demo-1.0.jar
                  EXPOSE 8080
                  ENTRYPOINT ["java","-jar","docker-demo-1.0.jar"]
                  

                  docker-compose.yml

                  docker-compose.yml

                  version: '3'
                  services:
                      mysql-db:
                          image: mysql
                          container_name: mysqldbhost
                          restart: always
                          environment:
                              MYSQL_DATABASE: docker_test
                              MYSQL_ROOT_PASSWORD: mypass
                          ports:
                              - 3306:3306
                      web-app:
                          build:
                              context: .
                              dockerfile: Dockerfile
                          image: 
                              kshitij23/docker-demo:1.3
                          ports:
                              - "8080:8080"
                          depends_on:
                              - mysql-db
                  

                  使用docker-compose up"时出错

                  Error when I use "docker-compose up"

                  web-app_1   | Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
                  web-app_1   |   at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:112) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final] 
                  web-app_1   |   at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:69) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.tool.schema.internal.exec.ImprovedExtractionContextImpl.getJdbcConnection(ImprovedExtractionContextImpl.java:60) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.tool.schema.internal.exec.ImprovedExtractionContextImpl.getJdbcDatabaseMetaData(ImprovedExtractionContextImpl.java:67) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.getTables(InformationExtractorJdbcDatabaseMetaDataImpl.java:333) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl.getTablesInformation(DatabaseInformationImpl.java:120) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:65) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:207) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:314) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1237) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
                  web-app_1   |   at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
                  web-app_1   |   at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) 
                  ~[spring-orm-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
                  web-app_1   |   ... 28 common frames omitted
                  web-app_1   | Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
                  web-app_1   |
                  web-app_1   | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
                  web-app_1   |   at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]      
                  web-app_1   |   at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
                  web-app_1   |   at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
                  web-app_1   |   at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
                  web-app_1   |   at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
                  web-app_1   |   at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
                  web-app_1   |   at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.2.jar!/:na]
                  web-app_1   |   at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:354) ~[HikariCP-3.4.2.jar!/:na]
                  web-app_1   |   at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202) ~[HikariCP-3.4.2.jar!/:na]
                  web-app_1   |   at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473) ~[HikariCP-3.4.2.jar!/:na]
                  web-app_1   |   at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:554) ~[HikariCP-3.4.2.jar!/:na]
                  web-app_1   |   at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.4.2.jar!/:na]
                  web-app_1   |   at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.4.2.jar!/:na]
                  web-app_1   |   at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:43) ~[hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
                  web-app_1   |   ... 43 common frames omitted
                  web-app_1   | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
                  web-app_1   |
                  web-app_1   | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
                  web-app_1   |   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_212]
                  web-app_1   |   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_212]
                  web-app_1   |   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_212]
                  web-app_1   |   at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_212]
                  web-app_1   |   at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
                  web-app_1   |   at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
                  web-app_1   |   at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
                  web-app_1   |   at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
                  web-app_1   |   at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]      
                  web-app_1   |   at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
                  web-app_1   |   at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
                  web-app_1   |   at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
                  web-app_1   |   ... 56 common frames omitted
                  web-app_1   | Caused by: java.net.UnknownHostException: mysql
                  web-app_1   |   at java.net.InetAddress.getAllByName0(InetAddress.java:1281) ~[na:1.8.0_212]
                  web-app_1   |   at java.net.InetAddress.getAllByName(InetAddress.java:1193) ~[na:1.8.0_212]
                  web-app_1   |   at java.net.InetAddress.getAllByName(InetAddress.java:1127) ~[na:1.8.0_212]
                  web-app_1   |   at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:132) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
                  web-app_1   |   at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]      
                  web-app_1   |   ... 59 common frames omitted
                  

                  推荐答案

                  web-app 应用程序正在尝试连接到尚不可用的 mysql-db(数据库仍在加载)您可以根据自己的喜好使用以下任何一种解决方案.

                  web-app application is trying to connect to mysql-db which is not yet available(database is still loading) you can use any of the following solutions depending on your preference.

                  1. 重启失败

                  1. Restart-on failure

                  修改docker-compose文件为

                     web-app:
                       image: 
                           kshitij23/docker-demo:1.3
                       restart:on-failure
                  

                • 睡觉

                • sleep

                  修改环境下的docker-compose文件

                    web-app:
                     image:
                         kshitij23/docker-demo:1.3
                     environment:
                      -SLEEP_LENGTH=5
                  

                • wait-for-it.sh 文件

                • wait-for-it.sh file

                  wait-for-it.sh

                  这种方法需要你修改你的 Dockerfile 以及 docker-compose文件

                  this approach needs you to modify your Dockerfile as well as docker-compose file

                  这篇关于Docker 中的 Mysql 连接错误与 Spriing 启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                • 上一篇:“无法连接到本地 MySQL 服务器"在 docker-compose 中 下一篇:如何在 sql server 2012 中部署现有的 SSIS 包?

                  相关文章

                      <bdo id='BDVmg'></bdo><ul id='BDVmg'></ul>
                    <tfoot id='BDVmg'></tfoot>

                    1. <small id='BDVmg'></small><noframes id='BDVmg'>

                    2. <legend id='BDVmg'><style id='BDVmg'><dir id='BDVmg'><q id='BDVmg'></q></dir></style></legend>
                      <i id='BDVmg'><tr id='BDVmg'><dt id='BDVmg'><q id='BDVmg'><span id='BDVmg'><b id='BDVmg'><form id='BDVmg'><ins id='BDVmg'></ins><ul id='BDVmg'></ul><sub id='BDVmg'></sub></form><legend id='BDVmg'></legend><bdo id='BDVmg'><pre id='BDVmg'><center id='BDVmg'></center></pre></bdo></b><th id='BDVmg'></th></span></q></dt></tr></i><div id='BDVmg'><tfoot id='BDVmg'></tfoot><dl id='BDVmg'><fieldset id='BDVmg'></fieldset></dl></div>