java - 为什么spring-boot连接不上本地mysql服务器?

标签 java mysql spring-boot

无法让 springboot 连接到我的本地主机 mysql 数据库。

这是我的项目结构:

enter image description here

这是错误日志。

enter image description here

奇怪的是他说:“拒绝用户''@'localhost'的访问(使用密码:NO)”,但在我的application.properties中我写了连接:

server.port=8080
spring.main.banner-mode=off
spring.thymeleaf.cache=false
spring.freemarker.cache=false
spring.groovy.template.cache=false
spring.datasource.url=jdbc:mysql://localhost:3306/user?autoReconnect=true&useSSL=false
spring.datasource.data-username=root
spring.datasource.data-password=stepin
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

我的 build.gradle 文件:

buildscript {
ext {
    springBootVersion = '1.4.1.RELEASE'
}
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-    plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'

jar {
    baseName = 'databaseaccess'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('mysql:mysql-connector-java')
    runtime("com.h2database:h2")
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

我是 springboot 的新手,但是我可以连接到我的本地数据库,当我写正常的 java 连接时,为什么 springboot 不能?

最佳答案

我怀疑这是因为这些属性:

spring.datasource.data-username=root
spring.datasource.data-password=stepin

尝试将它们替换为

spring.datasource.username=root
spring.datasource.password=stepin

可以有 2 个不同的用户来执行 DDLDML操作。您指定了第二个,但没有提供第一个。

引自 https://github.com/spring-projects/spring-boot/blob/master/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc :

spring.datasource.data= # Data (DML) script resource reference.
spring.datasource.data-username= # User of the database to execute DML scripts (if different).
spring.datasource.data-password= # Password of the database to execute DML scripts (if different).
spring.datasource.username=
spring.datasource.password= # Login password of the database.

关于java - 为什么spring-boot连接不上本地mysql服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40108428/

相关文章:

java - Spring 社交和 Facebook API 更改

java - Apache POI HWPF 嵌套列表?

php - 当 MySQL 连接未关闭时会发生什么?

javax 验证不验证 notNull

java - 如何在 Spring Boot 中安排延迟增加的任务?

java - 启动 vscode flutter 调试时无法确定任务 ':app:flutterBuildDebug' 的依赖关系

java - 没有 LogCat 错误的 Android 应用程序 'Stopping'

mysql - 查找停机时间并计算停机时间长度

php - 粘贴到文本区域的文本未插入到数据库中

java - Spring 安全5 : There is no PasswordEncoder mapped for the id "null"