简体   繁体   English

将Spring-Boot连接到MySQL

[英]connection Spring-Boot to MySQL

I'm new using Spring Boot and I'm having an error connecting it to MySQL Database, I already read all tutorials but still haven't found the problem. 我是使用Spring Boot的新手,将它连接到MySQL数据库时出错,我已经阅读了所有教程,但仍然没有发现问题。

ERROR 27341 --- [ main] com.zaxxer.hikari.pool.HikariPool 错误27341-[[main] com.zaxxer.hikari.pool.HikariPool
: HikariPool-1 - Exception during pool initialization. :HikariPool-1-池初始化期间发生异常。 com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:无法创建与数据库服务器的连接。 org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta-data; org.springframework.jdbc.support.MetaDataAccessException:无法获取提取元数据的连接; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; 嵌套的异常是org.springframework.jdbc.CannotGetJdbcConnectionException:无法获取JDBC连接。 nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. 嵌套的异常是com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:无法创建与数据库服务器的连接。 at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:328) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE] 在org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:328)〜[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]

application.properties application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/db_predio?useSSL=false
spring.datasource.username = root
spring.datasource.password = 

spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

spring.jpa.show-sql = true

spring.jpa.hibernate.ddl-auto = update

spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

pom.xml 的pom.xml

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-autoconfigure -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.1.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
            <scope>provided</scope>
        </dependency>


    </dependencies>

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
    </plugins>
    </build>
</project>

to run the application 运行应用程序

@SpringBootApplication
@EntityScan("au.basicexpert.predio.entity")
@EnableJpaRepositories("au.basicexpert.predio.repository")
@ComponentScan("au.basicexpert.predio.controller")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Shutdown the local MySQL and run the project again with debug enabled. 关闭本地MySQL,然后在启用调试的情况下再次运行项目。 Please post the full log trace. 请发布完整的日志跟踪。

Why do you want to use the old version of MySQL connector? 为什么要使用旧版本的MySQL连接器? Unless if there is a specific reason behind it go for a new version always. 除非背后有特殊原因,否则请始终选择新版本。 if you are not sure what is a latest version remove the version and let maven manages it for the first time. 如果您不确定什么是最新版本,请删除该版本,并让maven首次对其进行管理。

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

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