当前位置: 首页 > 知识库问答 >
问题:

设置Spring boot web应用程序并与MySQL数据库链接

仉姚石
2023-03-14

我正在尝试启动我的Spring boot web应用程序,它链接到MySQL数据库。该应用程序是在docker容器上启动的,该容器与MySQL数据库容器处于同一网络中。我曾经尝试使用容器名称作为MySQL的主机,但是它导致了名称解析,所以我现在使用docker容器的私有IP地址来连接。

server.port=8081
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database=mysql
spring.datasource.url=jdbc:mysql://172.88.0.0:3306/exampleDB
spring.datasource.username=exampleUser
spring.datasource.password=examplePassword
spring.jpa.properties.hibernate.default_schema=exampleDB
logging.level.root=WARN

我收到一个java.io.eofException:SSL对等点关闭错误错误:

java_bad_app |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
java_bad_app | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
java_bad_app |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
java_bad_app |   '  |____| .__|_| |_|_| |_\__, | / / / /
java_bad_app |  =========|_|==============|___/=/_/_/_/
java_bad_app |  :: Spring Boot ::                (v2.5.4)
java_bad_app |
java_bad_app | 2021-10-06 09:33:42.634 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.
java_bad_app |
java_bad_app | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
java_bad_app |
java_bad_app | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
java_bad_app |  at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-4.0.3.jar!/:na]
java_bad_app |  at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364) ~[HikariCP-4.0.3.jar!/:na]
java_bad_app |  at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-4.0.3.jar!/:na]
java_bad_app |  at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476) ~[HikariCP-4.0.3.jar!/:na]
java_bad_app |  at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-4.0.3.jar!/:na]
java_bad_app |  at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-4.0.3.jar!/:na]
java_bad_app |  at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar!/:na]
java_bad_app |  at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158) ~[spring-jdbc-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116) ~[spring-jdbc-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79) ~[spring-jdbc-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:330) ~[spring-jdbc-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.boot.jdbc.EmbeddedDatabaseConnection.isEmbedded(EmbeddedDatabaseConnection.java:184) ~[spring-boot-2.5.4.jar!/:2.5.4]
java_bad_app |  at org.springframework.boot.autoconfigure.orm.jpa.HibernateDefaultDdlAutoProvider.getDefaultDdlAuto(HibernateDefaultDdlAutoProvider.java:42) ~[spring-boot-autoconfigure-2.5.4.jar!/:2.5.4]
java_bad_app |  at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.lambda$getVendorProperties$1(HibernateJpaConfiguration.java:130) ~[spring-boot-autoconfigure-2.5.4.jar!/:2.5.4]
java_bad_app |  at org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings.getDdlAuto(HibernateSettings.java:41) ~[spring-boot-autoconfigure-2.5.4.jar!/:2.5.4]
java_bad_app |  at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.determineDdlAuto(HibernateProperties.java:143) ~[spring-boot-autoconfigure-2.5.4.jar!/:2.5.4]
java_bad_app |  at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.getAdditionalProperties(HibernateProperties.java:103) ~[spring-boot-autoconfigure-2.5.4.jar!/:2.5.4]
java_bad_app |  at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.determineHibernateProperties(HibernateProperties.java:95) ~[spring-boot-autoconfigure-2.5.4.jar!/:2.5.4]
java_bad_app |  at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.getVendorProperties(HibernateJpaConfiguration.java:132) ~[spring-boot-autoconfigure-2.5.4.jar!/:2.5.4]
java_bad_app |  at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(JpaBaseConfiguration.java:132) ~[spring-boot-autoconfigure-2.5.4.jar!/:2.5.4]
java_bad_app |  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
java_bad_app |  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
java_bad_app |  at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
java_bad_app |  at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
java_bad_app |  at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638) ~[spring-beans-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334) ~[spring-beans-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.9.jar!/:5.3.9]
java_bad_app |  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.4.jar!/:2.5.4]
java_bad_app |  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.4.jar!/:2.5.4]
java_bad_app |  at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.4.jar!/:2.5.4]
java_bad_app |  at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.4.jar!/:2.5.4]
java_bad_app |  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.4.jar!/:2.5.4]
java_bad_app |  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.4.jar!/:2.5.4]
java_bad_app |  at com.stackify.sandbox.SandboxApplication.main(SandboxApplication.java:12) ~[classes!/:0.0.1-SNAPSHOT]
java_bad_app |  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
java_bad_app |  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
java_bad_app |  at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
java_bad_app |  at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
java_bad_app |  at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[sandbox.jar:0.0.1-SNAPSHOT]
java_bad_app |  at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) ~[sandbox.jar:0.0.1-SNAPSHOT]
java_bad_app |  at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[sandbox.jar:0.0.1-SNAPSHOT]
java_bad_app |  at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[sandbox.jar:0.0.1-SNAPSHOT]
java_bad_app | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
java_bad_app |
java_bad_app | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
java_bad_app |  at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
java_bad_app |  at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
java_bad_app |  at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
java_bad_app |  at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
java_bad_app |  at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.protocol.a.NativeProtocol.negotiateSSLConnection(NativeProtocol.java:317) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.protocol.a.NativeAuthenticationProvider.connect(NativeAuthenticationProvider.java:202) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1352) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.NativeSession.connect(NativeSession.java:132) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:948) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  ... 56 common frames omitted
java_bad_app | Caused by: javax.net.ssl.SSLHandshakeException: Remote host terminated the handshake
java_bad_app |  at java.base/sun.security.ssl.SSLSocketImpl.handleEOF(SSLSocketImpl.java:1616) ~[na:na]
java_bad_app |  at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1434) ~[na:na]
java_bad_app |  at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1336) ~[na:na]
java_bad_app |  at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:450) ~[na:na]
java_bad_app |  at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:421) ~[na:na]
java_bad_app |  at com.mysql.cj.protocol.ExportControlled.performTlsHandshake(ExportControlled.java:320) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.protocol.StandardSocketFactory.performTlsHandshake(StandardSocketFactory.java:194) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.protocol.a.NativeSocketConnection.performTlsHandshake(NativeSocketConnection.java:101) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  at com.mysql.cj.protocol.a.NativeProtocol.negotiateSSLConnection(NativeProtocol.java:308) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
java_bad_app |  ... 61 common frames omitted
java_bad_app |  Suppressed: java.net.SocketException: Broken pipe (Write failed)
java_bad_app |          at java.base/java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:na]
java_bad_app |          at java.base/java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:110) ~[na:na]
java_bad_app |          at java.base/java.net.SocketOutputStream.write(SocketOutputStream.java:150) ~[na:na]
java_bad_app |          at java.base/sun.security.ssl.SSLSocketOutputRecord.encodeAlert(SSLSocketOutputRecord.java:81) ~[na:na]
java_bad_app |          at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:380) ~[na:na]
java_bad_app |          at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:292) ~[na:na]
java_bad_app |          at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:462) ~[na:na]
java_bad_app |          ... 66 common frames omitted
java_bad_app | Caused by: java.io.EOFException: SSL peer shut down incorrectly
java_bad_app |  at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:483) ~[na:na]
java_bad_app |  at java.base/sun.security.ssl.SSLSocketInputRecord.readFully(SSLSocketInputRecord.java:461) ~[na:na]
java_bad_app |  at java.base/sun.security.ssl.SSLSocketInputRecord.decodeInputRecord(SSLSocketInputRecord.java:243) ~[na:na]
java_bad_app |  at java.base/sun.security.ssl.SSLSocketInputRecord.decode(SSLSocketInputRecord.java:181) ~[na:na]
java_bad_app |  at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:111) ~[na:na]
java_bad_app |  at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1426) ~[na:na]
java_bad_app |  ... 68 common frames omitted```

Any help would be appreciated!

共有1个答案

周正真
2023-03-14

您忘记将spring.datasource.driverclassname=com.mysql.jdbc.driver放入application.properties中,没有它们,mysql驱动程序不会加载...

以下是修改后的applicatoin.properties:

server.port=8081
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database=mysql
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://172.88.0.0:3306/exampleDB
spring.datasource.username=exampleUser
spring.datasource.password=examplePassword
spring.jpa.properties.hibernate.default_schema=exampleDB
logging.level.root=WARN
 类似资料:
  • 我用Visual Studio制作了一个使用Windows窗体应用程序C++/CLI的小型图书馆管理系统。为了处理学生和书籍的数据库,我正在使用MySQL连接来连接到我计算机上的本地数据库,使用: 下面是我正在使用的名称空间: 现在我需要做一些事情,这样我就可以从一台不同的计算机连接到我给定的数据库。我试着搜索了一些方法,但有些方法需要在两台计算机上都安装MySQL,有些方法需要使用一些我不知道的

  • MySQL连接器是maven依赖项的一部分,所有数据库属性如url、用户名、密码都在application.properties中提到。 获取RuntimeException,例如: 驱动程序com.mysql.jdbc.Driver声称不接受jdbcUrl,jdbc/mysql://10.53.235.141:3306/hms。 请帮忙解决。

  • 问题内容: 我构建了phoneGap应用程序,它是一个Facebook风格的应用程序,其想法是用户可以发布消息,而他们的朋友可以对此消息发表评论。该应用基于html,js和phoneGap。我有一台带有mysql数据库的服务器。我的问题是如何从远程数据库中拉出和推送数据。我是个初学者,所以我不太确定如何做到这一点。即使加载完成后,我也需要实时更新页面 我用Google搜索它,我意识到我需要使用Aj

  • 问题内容: 我已经用create-react- app构建了一个Todo应用。我正在使用的商店基于本地存储(对象窗口的JS属性)。现在,我创建了一个MySQL数据库,并希望连接到该数据库,因此状态将显示数据库中的值,并将通过操作进行更新。 我尝试使用db.js通过“节点”控制台连接到数据库并输出值。有用。 是否可以使用此脚本将应用程序的状态连接到数据库? 问题答案: 您无法直接连接它们。 在网络浏

  • application.yml: 我尝试从运行mysql的命名空间上的另一个pod访问该服务,因为它已经预先安装了mysql-client,并从主机访问该服务。两人都有访问数据库的权限。我还在运行应用程序的pod上tring ping。它发现服务有任何问题。 然后我尝试使用NodePort而不是ClusterIP。什么都没变。 我完全卡住了,不知道出了什么问题。如有任何帮助,不胜感激。