数据库无法通过Spring Boot连接

时间:2019-10-31 12:44:52

标签: java mysql spring-boot

我试图用数据库连接构建RestFull Web服务。但这似乎未与数据库连接。而且我在控制台中收到此错误。

   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

我不知道这是怎么回事。

3 个答案:

答案 0 :(得分:0)

    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

答案 1 :(得分:0)

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

因为它显示了像unknownHostException这样的异常。

答案 2 :(得分:0)