【问题标题】:Database can't connect with spring boot数据库无法与spring boot连接
【发布时间】:2020-02-26 19:56:42
【问题描述】:

我尝试使用数据库连接构建 RestFull 网络服务。但这似乎没有与数据库连接。我在控制台中收到此错误。

   com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
        at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.18.jar:8.0.18]
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.18.jar:8.0.18]
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.18.jar:8.0.18]



The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_162]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_162]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_162]


Caused by: java.net.UnknownHostException: db_example
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_162]
    at java.net.InetAddress$2.lookupAllHostAddr(Unknown Source) ~[na:1.8.0_162]

Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275) ~[hibernate-core-5.4.6.Final.jar:5.4.6.Final]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) ~[hibernate-core-5.4.6.Final.jar:5.4.6.Final]

Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100) ~[hibernate-core-5.4.6.Final.jar:5.4.6.Final]
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54) ~[hibernate-core-5.4.6.Final.jar:5.4.6.Final]

这是我的 application.property 文件

server.port=8092

#spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:mysql://db_example:3306/db_example
spring.datasource.username=root
spring.datasource.password=


#spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=create

我不知道这是怎么回事。

【问题讨论】:

  • UnknownHostException: db_example - 您没有使用主机名“db_example”运行的数据库服务器。使用运行数据库的机器的实际主机名,而不是“db_example”

标签: java mysql spring-boot


【解决方案1】:
    spring.datasource.url=jdbc:mysql://localhost:3306/db_example
    spring.datasource.username = root
    spring.datasource.password = root
    spring.jpa.properties.hibernate.dialect =org.hibernate.dialect.MySQL5Dialect

hibernate.dialect=org.hibernate.dialectMySQL5Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
hibernate.ddl-auto=validate

hibernate.connection.url=jdbc:mysql://localhost:3306/axa_india?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
hibernate.connection.username=root
hibernate.connection.password=root

【讨论】:

  • 谢谢!但这仅解决了一个错误。我无法弄清楚其他错误
  • 希望这会有所帮助
  • 你能告诉我其他错误的原因是什么
  • 你需要为数据库连接添加驱动程序,比如hibernate和spring
  • 你是这个意思吗?
【解决方案2】:

我认为这里的主机名有问题。所以请检查主机名,如 localhost(或)您正确的数据库托管地名。

因为它显示了类似unknownHostException的异常。

【讨论】:

    【解决方案3】:
    猜你喜欢
    • 2020-01-11
    • 2019-04-12
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 2018-10-09
    • 1970-01-01
    • 2020-12-07
    相关资源
    最近更新 更多