[SOLVED] Mysql Connection Error In Docker with Spring boot

Issue

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.

The weird thing is that it’s giving error at jdbc:mysql. It’s considering second argument of database connection as host. I am not sure why ?

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

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

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

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

Solution

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. Restart-on failure

    modify docker-compose file to

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

    modify docker-compose file under environment

      web-app:
       image:
           kshitij23/docker-demo:1.3
       environment:
        -SLEEP_LENGTH=5
    
  3. wait-for-it.sh file

    wait-for-it.sh

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

Answered By – malvern dongeni

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.