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

Mysql Connection Error In Docker with Spriing boot(Docker 中的 Mysql 连接错误与 Spriing 启动)
本文介绍了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 启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

  • 本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

    相关文档推荐

    Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
    Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
    Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
    auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
    Sort by ID DESC(按ID代码排序)
    SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)