Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't spring-boot connect to local mysql server?

Cant get springboot connected to my localhost mysql database.

This is my project structure:

enter image description here

This is the error-log.

enter image description here

It is strange that he says: "Access denied for user ''@'localhost' (using password: NO)", but in my application.properties i have written connection:

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

My build.gradle file:

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')
}

I am new with springboot, but i can connect to my local database, when i write normal java connection, so why springboot cant?

like image 604
Anton Styopin Avatar asked Oct 18 '16 12:10

Anton Styopin


People also ask

How does spring boot connect to local database?

To access the Relational Database by using JdbcTemplate in Spring Boot application, we need to add the Spring Boot Starter JDBC dependency in our build configuration file. Then, if you @Autowired the JdbcTemplate class, Spring Boot automatically connects the Database and sets the Datasource for the JdbcTemplate object.

Can not connect to local MySQL server?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.

How do I connect to a local MySQL server?

Step 3: Connect to a Local MySQL ServerEnter mysql.exe -uroot -p , and MySQL will launch using the root user. MySQL will prompt you for your password. Enter the password from the user account you specified with the –u tag, and you'll connect to the MySQL server.


1 Answers

I suspect this is because of these properties:

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

Try to replace them by

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

It's possible to have 2 different users for executing DDL and DML operations. You specified the second and didn't provide the first.

Quote from 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.
like image 125
Slava Semushin Avatar answered Oct 16 '22 21:10

Slava Semushin