Spring Boot无法连接到MySQL并在Docker/Docker Compose中退出 [英] Spring Boot cannot connect to MySQLand exits in Docker/Docker compose

查看:190
本文介绍了Spring Boot无法连接到MySQL并在Docker/Docker Compose中退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的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:连接被拒绝(连接 拒绝)

Caused by: java.net.ConnectException: Connection refused (Connection refused)

由以下原因引起:java.sql.SQLNonTransientConnectionException:无法 创建与数据库服务器的连接.尝试重新连接3次. 放弃.

Caused by: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

由以下原因引起:java.net.ConnectException:连接被拒绝(连接 拒绝)

Caused by: java.net.ConnectException: Connection refused (Connection refused)

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

git_spring-boot-jpa-docker-webapp_1 exited with code 1

更新1:更正docker-compose后的新错误

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

推荐答案

撰写服务中的容器可以使用名称连接到其他容器,例如尝试从spring-boot容器ping数据库容器.

Containers in compose services can connect to other containers by using name, e.g. try to ping database container from spring-boot container.

检查是否可以ping数据库容器

Check if you can ping database container

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

一旦进入外壳(您可能必须在spring-boot容器中安装ping才能仅测试apt-get install iputils-pingyum install,具体取决于图像容器所基于的容器)

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 docker-mysql

如果ping成功,则意味着您可以连接到数据库

if ping is successful then it means you can connect to database

在application.properties中将数据库URL替换为

replace database url in application.properties with

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:

这篇关于Spring Boot无法连接到MySQL并在Docker/Docker Compose中退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆