简体   繁体   English

Spring Boot无法连接到MySQL并在Docker / Docker Compose中退出

[英]Spring Boot cannot connect to MySQLand exits in Docker/Docker compose

Below is my application.properties file 以下是我的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

Below is my docker-compose.yml file 以下是我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

Application is exited with 申请退出

Caused by: java.net.ConnectException: Connection refused (Connection refused) 引起原因:java.net.ConnectException:连接被拒绝(连接被拒绝)

Caused by: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. 原因:java.sql.SQLNonTransientConnectionException:无法创建与数据库服务器的连接。 Attempted reconnect 3 times. 尝试重新连接3次。 Giving up. 放弃。

Caused by: java.net.ConnectException: Connection refused (Connection refused) 引起原因:java.net.ConnectException:连接被拒绝(连接被拒绝)

git_spring-boot-jpa-docker-webapp_1 exited with code 1 git_spring-boot-jpa-docker-webapp_1退出,代码为1

Update 1: New error after correction to docker-compose 更新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

Containers in compose services can connect to other containers by using name, eg try to ping database container from spring-boot container. 组合服务中的容器可以使用名称连接到其他容器,例如,尝试从spring-boot容器ping数据库容器。

Check if you can ping database container 检查是否可以ping数据库容器

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

once you are in the shell (You may have to install ping in spring-boot container for testing only apt-get install iputils-ping or yum install depending on image container is based on) 一旦进入外壳(您可能必须将ping安装在spring-boot容器中,以仅测试apt-get install iputils-pingyum install具体取决于映像容器的基础)

ping docker-mysql

if ping is successful then it means you can connect to database 如果ping成功,则意味着您可以连接到数据库

replace database url in application.properties with 将application.properties中的数据库URL替换为

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

Minimal Example for Mysql Mysql的最小示例

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

Try following docker-compose 尝试跟随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:

You are passing the DATABASE_HOST , DATABASE_PORT , DATABASE_NAME , DATABASE_USER and DATABASE_PASSWORD but you are not using it inside your app. 您正在传递DATABASE_HOSTDATABASE_PORTDATABASE_NAMEDATABASE_USERDATABASE_PASSWORD但未在应用程序内部使用它。

Update your properties like this. 像这样更新您的属性。 (Better if your create a profile so that you app runs outside of docker) (最好创建一个配置文件,以便您的应用在Docker外部运行)

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

If you really want to use localhost as your db_url . 如果您确实要使用localhost作为db_url You can also use network_mode: "service:[service name]" property of docker compose. 您还可以使用docker compose的network_mode: "service:[service name]"属性。 Only downside is that this property cannot be used with port property. 唯一的缺点是此属性不能与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

And your properties file could look like 您的属性文件可能看起来像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM