Spring Boot не может подключиться к MySQL и завершить работу в Docker / Docker compose - PullRequest
0 голосов
/ 13 февраля 2019

Ниже мой application.properties файл

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true

Ниже мой docker-compose.yml файл

version: '3'

services: 
  docker-mysql:
    image: mysql:latest
    ports:
      - 3306
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=test
      - MYSQL_PASSWORD=root
  spring-boot-jpa-docker-webapp:
    image: springboot_docker
    depends_on:
      - docker-mysql
    ports:
      - 8080:8080
    environment:
      - DATABASE_HOST=docker-mysql
      - DATABASE_USER=root
      - DATABASE_PASSWORD=root
      - DATABASE_NAME=test  
      - DATABASE_PORT=3306

Приложение закрывается с

Вызывается: java.net.ConnectException: Соединение отклонено (Соединение отклонено)

Причина: java.sql.SQLNonTransientConnectionException: Не удалось создать соединение с сервером базы данных.Попытка переподключения 3 раза.Отказ.

Причина: java.net.ConnectException: Соединение отклонено (Соединение отклонено)

git_spring-boot-jpa-docker-webapp_1 завершен с кодом 1

Обновление 1: новая ошибка после исправления в docker-compose

docker-mysql_1                   | Initializing database
docker-mysql_1                   | 2019-02-13T20:34:44.351044Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
docker-mysql_1                   | 2019-02-13T20:34:44.351123Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.15) initializing of server in progress as process 29
docker-mysql_1                   | 2019-02-13T20:34:44.352587Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
docker-mysql_1                   | 2019-02-13T20:34:44.352592Z 0 [ERROR] [MY-013236] [Server] Newly created data directory /var/lib/mysql/ is unusable. You can safely remove it.
docker-mysql_1                   | 2019-02-13T20:34:44.352638Z 0 [ERROR] [MY-010119] [Server] Aborting
docker-mysql_1                   | 2019-02-13T20:34:44.353948Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.15)  MySQL Community Server - GPL.



  spring-boot-jpa-docker-webapp_1  | 
spring-boot-jpa-docker-webapp_1  | 2019-02-13 21:02:22.562  WARN 1 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 08001
spring-boot-jpa-docker-webapp_1  | 2019-02-13 21:02:22.562 ERROR 1 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : Could not create connection to database server. Attempted reconnect 3 times. Giving up.
spring-boot-jpa-docker-webapp_1  | 2019-02-13 21:02:22.569  WARN 1 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
spring-boot-jpa-docker-webapp_1  | 2019-02-13 21:02:22.573  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
spring-boot-jpa-docker-webapp_1  | 2019-02-13 21:02:22.596  INFO 1 --- [           main] ConditionEvaluationReportLoggingListener : 
spring-boot-jpa-docker-webapp_1  | 
spring-boot-jpa-docker-webapp_1  | Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
spring-boot-jpa-docker-webapp_1  | 2019-02-13 21:02:22.601 ERROR 1 --- [           main] o.s.boot.SpringApplication               : Application run failed
spring-boot-jpa-docker-webapp_1  | 
spring-boot-jpa-docker-webapp_1  | org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
spring-boot-jpa-docker-webapp_1  |  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083) ~[spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:853) ~[spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at com.github.Application.main(Application.java:15) ~[classes!/:1.0-SNAPSHOT]
spring-boot-jpa-docker-webapp_1  |  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
spring-boot-jpa-docker-webapp_1  |  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
spring-boot-jpa-docker-webapp_1  |  at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
spring-boot-jpa-docker-webapp_1  |  at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) ~[spring-boot-docker-1.0.jar:1.0-SNAPSHOT]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) ~[spring-boot-docker-1.0.jar:1.0-SNAPSHOT]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) ~[spring-boot-docker-1.0.jar:1.0-SNAPSHOT]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) ~[spring-boot-docker-1.0.jar:1.0-SNAPSHOT]
spring-boot-jpa-docker-webapp_1  | Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
spring-boot-jpa-docker-webapp_1  |  at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:402) ~[spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) ~[spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  ... 22 common frames omitted
spring-boot-jpa-docker-webapp_1  | Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:48) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:69) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.jdbcStatement(GenerationTargetToDatabase.java:77) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:53) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:375) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:359) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:241) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:154) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:126) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:112) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:144) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:310) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:939) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) ~[spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  ... 26 common frames omitted
spring-boot-jpa-docker-webapp_1  | Caused by: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:905) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:830) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:43) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
spring-boot-jpa-docker-webapp_1  |  ... 42 common frames omitted
spring-boot-jpa-docker-webapp_1  | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
spring-boot-jpa-docker-webapp_1  | 
spring-boot-jpa-docker-webapp_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
spring-boot-jpa-docker-webapp_1  |  at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
spring-boot-jpa-docker-webapp_1  |  at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
spring-boot-jpa-docker-webapp_1  |  at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
spring-boot-jpa-docker-webapp_1  |  at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.NativeSession.connect(NativeSession.java:152) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:849) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  ... 56 common frames omitted
spring-boot-jpa-docker-webapp_1  | Caused by: java.net.ConnectException: Connection refused (Connection refused)
spring-boot-jpa-docker-webapp_1  |  at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
spring-boot-jpa-docker-webapp_1  |  at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na]
spring-boot-jpa-docker-webapp_1  |  at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242) ~[na:na]
spring-boot-jpa-docker-webapp_1  |  at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[na:na]
spring-boot-jpa-docker-webapp_1  |  at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403) ~[na:na]
spring-boot-jpa-docker-webapp_1  |  at java.base/java.net.Socket.connect(Socket.java:591) ~[na:na]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  ... 58 common frames omitted

Ответы [ 2 ]

0 голосов
/ 13 февраля 2019

Вы передаете DATABASE_HOST, DATABASE_PORT, DATABASE_NAME, DATABASE_USER и DATABASE_PASSWORD, но не используете его внутри своего приложения.

Обновите свои свойства следующим образом.(Лучше, если вы создадите профиль, чтобы приложение работало вне докера)

spring.datasource.url=jdbc:mysql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}?autoReconnect=true
spring.datasource.username=${DATABASE_USER}
spring.datasource.password=${DATABASE_PASSWORD}

Если вы действительно хотите использовать localhost в качестве db_url .Вы также можете использовать свойство network_mode: "service:[service name]" docker compose.Единственным недостатком является то, что это свойство нельзя использовать со свойством port.

version: '3'

services: 
  docker-mysql:
    image: mysql:latest
    network_mode: "service:spring-boot-jpa-docker-webapp"
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=test
      - MYSQL_PASSWORD=root
  spring-boot-jpa-docker-webapp:
    image: springboot_docker
    depends_on:
      - docker-mysql
    ports:
      - 8080:8080
      - 3306 #Add this only if you want to expose the mysql to outer world.
    environment:
      - DATABASE_USER=root
      - DATABASE_PASSWORD=root
      - DATABASE_NAME=test

И ваш файл свойств может выглядеть как

spring.datasource.url=jdbc:mysql://localhost:3306/${DATABASE_NAME}?autoReconnect=true
spring.datasource.username=${DATABASE_USER}
spring.datasource.password=${DATABASE_PASSWORD}
0 голосов
/ 13 февраля 2019

Контейнеры в службах compose могут соединяться с другими контейнерами, используя имя, например, попробуйте проверить связь с контейнером базы данных из контейнера начальной загрузки.

Проверьте, можете ли вы пропинговать контейнер базы данных

docker-compose exec spring-boot-jpa-docker-webapp /bin/bash

когда вы находитесь в оболочке (возможно, вам придется установить ping в контейнере с загрузочной пружиной для тестирования только apt-get install iputils-ping или yum install в зависимости от того, на чем основан образ-контейнер)

ping docker-mysql

, если ping успешен, тогдаэто означает, что вы можете подключиться к базе данных

заменить URL базы данных в application.properties на

spring.datasource.url=jdbc:mysql://docker-mysql:3306/test?autoReconnect=true

Минимальный пример для Mysql

image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
  MYSQL_ROOT_PASSWORD: root
volumes:
  - "./volumes/database:/var/lib/mysql"

Попробуйте следующий docker-compose

version: "3.7"
services:
  docker-mysql:
    image: mysql:latest
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=test
    volumes:
      - "./volumes/database:/var/lib/mysql"
    networks:
      - spring_net
  spring-boot-jpa-docker-webapp:
    image: springboot_docker
    ports:
      - "8080:8080"
    networks:
      - spring_net
networks:
  spring_net:
...