【问题标题】:Why doesn't spring-boot connect to local mysql server?为什么 spring-boot 不连接到本地 mysql 服务器?
【发布时间】:2017-02-27 17:43:11
【问题描述】:

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

这是我的项目结构:

这是错误日志。

奇怪的是他说:“用户''@'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 Boot。您尚未授予在 MySQL 中连接的权限。
  • 您是否检查过是否将属性正确加载到持久性单元中?
  • 试试spring.datasource.usernamespring.datasource.password
  • 天哪...尼古拉斯谢谢您...现在可以使用...

标签: java mysql spring-boot


【解决方案1】:

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

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.

【讨论】:

  • 真的吗? ...它有效,我已经用谷歌搜索了很多。这个小错误……嗯,谢谢!
猜你喜欢
  • 2019-09-09
  • 2017-04-20
  • 2014-05-10
  • 2010-11-20
  • 2018-12-04
  • 2014-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多